Thread: linked list

  1. #1

    Cool linked list

    I have created a simple linked list that contains an unordered list of integers.

    i.e. 5, 4, 3, 2, UN-ordered

    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



    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

  2. #2
    make two linked lists
    the original and the ordered one

  3. #3
    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
    -next node
    -original node

    sort of like a double linked list

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM