
linked list
I have created a simple linked list that contains an unordered list of integers.
i.e. 5, 4, 3, 2, UNordered
The problem is, after I have sorted them,
to 2, 3, 4, 5 ORDERED
the the original header node which was 5 is now 2
and so lose the original unordered list in the process.
I know I'm destroying the original header pointer, cannot figure out how to keep from doing so!
Question: How do I go about keeping both the unordered and ordered linked lists ?
/*The sortOnode Function below sorts an unordered linked list of integers referenced by the oldheader parameter. It points to the first node of the unordered linked list . Unfortunately, after it has sorted the integers the original unordered list is deleted and all that remains is the new Ordered linked list.
How do keep track of the unordered linked list*/
Here's what I got so far!
void sortOnode (nodePtrType & oldheader, nodePtrType newheader)
{
int swap = 1;
int temp;
nodePtrType prevptr, curptr;
newheader = oldheader; // Using unordered header or starting node
prevptr = newheader;
curptr = newheader;
curptr = curptr>link;
while (swap > 0 )
{
swap = 0;
while (curptr!= NULL)
{
if ( curptr>nnum > prevptr>nnum)
{
prevptr = curptr;
curptr = curptr>link;
} //end of while loop
else
{
temp = prevptr>nnum;
prevptr>nnum= curptr>nnum;
curptr>nnum= temp;
prevptr = curptr;
curptr = curptr>link;
swap = 1;
}//end else
}//end while curptr!= 0
prevptr =NULL;
curptr = newheader;
prevptr = curptr;
curptr = curptr>link;
}//end while swap >0 ;
} // end SortOnode

make two linked lists
the original and the ordered one

or you could add an extra pointer in the node to keep track of the original thing it pointed to.
then you'd have one linked list
node
value
next node
original node
sort of like a double linked list