i tried for sorting the link list for weeks already, and yet still couldnt get the output as what i want.
i search through the forum and i found the coding "algorithm" (in blue color) for sorting...but it seems tooo alit bit toooooooooooo long.
so far, i was trying to implement the same way as bubble sorting, but still...can not get answer :sad: . i tried using the elements, just like in bubble sort,
for example:
void abc::sorting()
{
Node *cur = head;
Node *pvr = head;
cur = cur->next;
int hold;
for( int pass = 0 ; pass < ( 8 - 1 ) ; pass++ )
for( ; cur != NULL; pvr = pvr -> next , cur=cur->next)
// in this case i put cur != NULL because it came to NULL first than pvr
{
if(pvr->num > cur->num)
{
hold = pvr->num;
pvr->num = cur->num;
cur->num = hold;
}
}
}
still can not also.. i hope that some experts out there could help me in this problem....giving me advice(s). i really headache already...couldn't figure out anything.... thanks
below here is my coding:
#include <iostream>
using namespace std;
class abc
{
private:
struct Node
{
int num;
Node *next;
};
Node *head;
Node *ptr;
public:
abc() { head = NULL; }
void display();
void add( int );
void sorting();
void sort();
};
void abc::add( int a )
{
if( head == NULL )
{
head = new Node;
head->num = a;
head->next = NULL;
ptr = head;
}
else
{
ptr->next = new Node;
ptr = ptr->next;
ptr->num = a;
ptr->next = NULL;
}
}
void abc::display()
{
for(Node *cur = head ; cur != NULL ; cur = cur->next)
cout << cur->num << " ->";
cout << endl;
}
void abc::sorting()
{
Node *cur = head;
Node *pvr = head;
cur = cur->next;
int hold;
for( ; pvr!= NULL ; pvr=pvr->next )
for( ; cur != NULL; cur=cur->next)
{
if(pvr->num > cur->num)
{
hold = pvr->num;
pvr->num = cur->num;
cur->num = hold;
}
}
}
void main()
{
abc f;
f.add(9);
f.add(8);
f.add(7);
f.add(6);
f.add(5);
f.add(4);
f.add(3);
f.add(2);
cout << endl;
f.display();
//f.sorting();
f.sort();
cout << endl;
f.display();
}
/*****************************************************/
void abc::sort()
{
Node *ptr1,*ptr2,*temp,*Imithead1,*Imithead2,*before;
if (head != NULL) {
ptr2 = head;
ptr1 = head->next;
//First Step Set the smallest value to head
while(ptr1 != NULL ) {
if (ptr2->num > ptr1->num ) {
temp = ptr2;
while(temp->next != ptr1)
temp = temp->next;
temp->next = ptr1->next;
ptr1->next = ptr2;
head = ptr1;}
ptr1 = ptr1->next;
ptr2 = head;}
////////////////////////////////////////////
////////////////////////////////////////////
before = head;
Imithead1 = Imithead2 = head->next;
while (Imithead1 != NULL) {
while (Imithead2 != NULL ) {
ptr1 = ptr2 =Imithead2;
while(ptr1 != NULL ) {
if (ptr2->num > ptr1->num ) {
temp = ptr2;
while(temp->next != ptr1)
temp = temp->next;
before->next = ptr1;
temp->next = ptr1->next;
ptr1->next = ptr2;
Imithead2 = ptr1;
}
ptr1 = ptr1->next;
ptr2 = Imithead2;
}
before = Imithead2;
Imithead2 = Imithead2->next;
}
Imithead1 = Imithead1->next;
Imithead2 = Imithead1;
}
}
}
/*************************************************/
<< moderator edit: added [code][/code] tags >>