Thread: Linked List Problems

  1. #1
    Registered User
    Join Date
    Sep 2005

    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.

    #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: ";
            list = new node;
        cout<<"\n\n\n...Ready to print inventory\n\n";
        for (list = pHead;list != NULL;list = list->pNext)
    void AddNode( node *pNode)
    	if ( pHead == NULL )
    		pHead = pNode;
    		pNode->pPrev = NULL;
    		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
    Cayman Islands
    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
    Oops! hehe thanks for the info I'll try that when I get home.

  4. #4
    Registered User
    Join Date
    Jan 2005
    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
    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, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM