Creating a linked list while sorting it

This is a discussion on Creating a linked list while sorting it within the C++ Programming forums, part of the General Programming Boards category; Oh man! This stuff is giving me a headache! The concept is so simple, but I just can't seem to ...

  1. #1
    Unregistered
    Guest

    Creating a linked list while sorting it

    Oh man! This stuff is giving me a headache! The concept is so simple, but I just can't seem to write the code. Can someone please show me how?

    void createlistKB(Node * &First)
    {
    int NewID = 0;
    cout << "Please Input Your List:\n";
    cout << "Enter -999 to stop\n";
    cout << "-----------------------\n";
    Node *Temp1, *Temp2, *NewNode;
    First = new Node;
    cin >> First->ID;
    First->Next=NULL;
    Temp1 = First;
    Temp2 = Temp1;
    do
    {
    NewNode = new Node;
    cin >> NewID;
    if(NewID != -999)
    {
    NewNode->ID = NewID;
    cout << First->Next;
    if(NewNode->ID < First->ID)
    {
    NewNode->Next = First;
    First = NewNode;
    cout << First->Next->Next;
    }
    do
    {
    if(NewNode->ID < Temp1->ID)
    {
    Temp2->Next = NewNode;
    NewNode->Next = Temp1;
    }
    Temp2 = Temp1;
    Temp1 = Temp1->Next;
    cout << Temp1->Next;
    }while(Temp1->Next != NULL);



    }
    }while(NewID != -999);
    cout << "THANK YOU!!!\n\n";
    }



    that's the junk i have so far, i can't seem to figure it out...

    here is the Node:
    struct Node
    {
    int ID;
    Node *Next;
    };

  2. #2
    Unregistered
    Guest
    btw: I'm trying to sort it from lowest to highest. Thanks guys

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    It might be easier if you break the create list function up. Here's one solution, but it would be even clearer if my InsertNode function was broken up into functions to check for inserting before/after and whether Node has to go at the head of the list.

    Code:
    void InsertNode(Node* &First, Node* &toInsert)
    {
        Node* Iterator = First;
    
    
        while(toInsert->ID>Iterator->ID&& Iterator->Next!=NULL)
            Iterator=Iterator->Next;
    
        if(toInsert->ID>Iterator->ID)   //Insert after Iterator
        {
            Node* Temp = Iterator->Next;
            Iterator->Next=toInsert;
            toInsert->Next=Temp;
        }
        else //Insert before Iterator
        {
            if(Iterator==First)
            {
                Node* Temp = First;
                First=toInsert;
                toInsert->Next=Temp;
                return;
            }
    
            Node* Prev = First;
            
            while(Prev->Next!=Iterator)
                Prev=Prev->Next;
    
            Prev->Next=toInsert;
            toInsert->Next=Iterator;
        }
    
    
    }
    
            
    
    void createlistKB(Node * &First) 
    { 
    
        First = new Node;
        First->Next=NULL;
        cout << "Enter ID (999 to quit): ";
        cin >> First->ID;
        Node* Temp;
    
        for(;; )
        {
            int input=0;
            cout << "Enter ID(999 to quit):  ";
            cin>>input;
            if(input==999)break;
    
            Temp = new Node;
            Temp->ID=input;
            Temp->Next=NULL;
            
            InsertNode(First,Temp);
        }
    }

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, 11:03 AM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 12:11 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21