linked list questions...

This is a discussion on linked list questions... within the C++ Programming forums, part of the General Programming Boards category; why is my linked list printing out backwards? Code: #include <iostream.h> #include <stdlib.h> using namespace std; typedef struct node { ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    linked list questions...

    why is my linked list printing out backwards?

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    using namespace std;
    
    typedef struct node
    {
        int a;
        node *next;
    }NODE;
    
    int main()
    {
        NODE *current = NULL;
    
       for (int i = 0; i < 5; i++)
       {
            node *newnode = new node;
            cout << "input a value: ";
                cin >> newnode->a;
                newnode->next = current;
                current = newnode;
    
       }
    
       node *show = current;
       while (show != NULL)
       {
            cout << show->a << endl;
            show = show->next;
       }
    
        system("PAUSE");
        return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Because you are adding your new nodes to the beginning of the list instead of at the end. When you start to output your list you end up with the beginning being the last node you added.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    newbie question:
    how would u change that around?

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    ah, its suppose to be printing like that...
    now to figure out the linked list sort...

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Originally posted by revelation437
    newbie question:
    how would u change that around?
    instead of setting the node your inserting's next to null, set it to the head -> next...

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    Originally posted by Jamsan
    instead of setting the node your inserting's next to null, set it to the head -> next...
    i dont fully understand

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    	if (m_Head == NULL)     //If the list is empty (head is null)
    	{
    		m_Head = T; 
    		T -> next = NULL;  
    	}
    
    	else               
    	{
    		T -> next = m_Head;         
    		m_Head = T;            
    	}
    basically, if there's nothing in the list, just set the header to the current node, and then the current -> next to NULL....if theres something in the list, you need to set the current header node, to the node your adding's next.....so basically you have

    Head -> node0 -> node 1....now you wanna add node 2 to the beginning....so you have to, Head -> node2 -> node0 -> node1....you have to set node0 to node2-> next, so the list isnt broken, and then the head to the new node.
    Last edited by Jamsan; 04-22-2003 at 04:45 PM.

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. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM

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