Printing Node Info in Linked List

This is a discussion on Printing Node Info in Linked List within the C++ Programming forums, part of the General Programming Boards category; Hi All, I'm trying to print information from each node in a linked list. Here's the code for the print: ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Printing Node Info in Linked List

    Hi All,

    I'm trying to print information from each node in a linked list.

    Here's the code for the print:

    Code:
    if (preloc == NULL)
            cout << start->next << " ";
    else
            cout << preloc->next << " ";
    Here's an example of the output. It's print the address space:

    00673174
    28
    Press any key to continue . . .

    00673184
    29
    Press any key to continue . . .

    How do I get it to print the info in the start->next node or preloc->next node?

    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I don't fully understand what you are asking.

    If you are asking how to itterate through the linked list elements, do something like this:

    Code:
    current = start;
    
    while(current != NULL)
    {
      cout << current;
      current=current->next;
    }
    Best Regards,

    Bonkey

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You are printing the address, but I guess you want to print the actual data in the list. Well, that depends on what variables you use. Say you use a string Name and an integer Age:
    Code:
    NodeE* CurrentNode = FirstNode;
    
    while(CurrentNode != NULL)
    {
       cout << CurrentNode->Name << " " << CurrentNode->Age << endl;
       CurrentNode = CurrentNode->NextNode;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    The code prints the address space and not the information in the node. How do deallocate in order to print the node's data rather than the node's address space.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    I figured it out. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 02:48 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. I need help~~Hash~~THX!
    By freefallin in forum C Programming
    Replies: 5
    Last Post: 04-22-2004, 08:50 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  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