Linked List Problems

This is a discussion on Linked List Problems within the C++ Programming forums, part of the General Programming Boards category; Tuesday of this past week I decided I would give myself a long over due refresher course in C++. So ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    12

    Linked List Problems

    Tuesday of this past week I decided I would give myself a long over due refresher course in C++. So I started with the tutorials and when I got to Linked Lists I got stuck and a practice program I was writing.

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct node
    {
           char *Inv;
           node *pNext;
           node *pPrev;
    };
    
    node *pHead;
    node *pTail;
    
    void AddNode( node *pNode);
    
    int main()
    {
        char *temp;
        node *list;
    
        while (temp != "done")
        {
            cout<<"Enter inventory item: ";
            cin>>temp;
            cin.ignore();
            list = new node;
            list->Inv=temp;
            AddNode(list);
        }
        cout<<"\n\n\n...Ready to print inventory\n\n";
    
        for (list = pHead;list != NULL;list = list->pNext)
        {
            cout<<list->Inv;
        }
    }
    
    void AddNode( node *pNode)
    {
    	if ( pHead == NULL )
        {
    		pHead = pNode;
    		pNode->pPrev = NULL;
    	}
        else
        {
    		pTail->pNext = pNode;
    		pNode->pPrev = pTail;
    	
        }
    	pTail = pNode;
    	pNode->pNext = NULL;
    }
    The program compiles and runs, but when I type the word done in it doesn't continue on to printing out the list it asks for another inventory item.

    Any ideas?

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    temp != "done"
    You can't compare c-strings this way, use strcmp() or use c++ strings as defined in <string>

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Oops! hehe thanks for the info I'll try that when I get home.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Using C++ strings will solve your problems, but if you decide to stick with the C style strings you have to make room for the string. As you have it, temp is a pointer that points off to some random location. You need to allocate space for your character array before you use it. If you use the string class then you wouldn't have to worry about that.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Yeah I changed the char to string, too away the * and added #include <string> and it worked! ^_^

    I ust needed to add in an IF statement so that the word 'done' wasn't added to the list.

    Thanks for all your help guys!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help!
    By mbk in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 03:54 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

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