I've got a test program for a linked list that I intend to use in a large program, and so far I can get it to insert and delete where I want. But I can get it to sort properly, could someone help? MY list is singly linked btw.
I set it up so that I have two lists, one that holds to original values and another that holds the sorted values.
thanks in advance for the helpCode:#include <iostream.h> class Employee{ public: int data; }; class Node{ public: Employee Rec; Node *next; }; int main(){ Node *Root = new Node; Node *Iterator = new Node; Node *Sort = new Node; Root->Rec.data=0; Root->next=NULL; Iterator=Root; Sort=Iterator; for(int i=1; i<10; i++){ Iterator->next = new Node; Sort->next = new Node; Iterator=Iterator->next; Sort=Sort->next; Iterator->Rec.data=i; Sort->Rec.data=i; Iterator->next=NULL; Sort->next=NULL; } Iterator=Root; while(Iterator!=NULL){ cout<<Iterator->Rec.data<<endl; Iterator=Iterator->next; if(Iterator->Rec.data==5){ Node *New = new Node; New->Rec.data=12; New->next=Iterator->next; Iterator->next=New; } if(Iterator->Rec.data==2){ Iterator->next=Iterator->next->next; } } Iterator=Root; Sort=Root; while(Sort!=NULL){ if(Sort->next->Rec.data > Iterator->Rec.data){ Iterator->next=Sort->next; Sort->next=Iterator; } } Sort=Root; while(Sort!=NULL&&Iterator!=NULL){ cout<<Sort->Rec.data<<" "<<Iterator->Rec.data<<endl; Sort=Sort->next; Iterator=Iterator->next; } delete Root, Iterator, Sort; return 0; }



LinkBack URL
About LinkBacks


