Thread: homework help regarding linked list

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    7

    homework help regarding linked list

    StringList.cpphomework help regarding linked list-capture-jpg

    I keep getting the same error for my linked list

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while (temp->next != NULL)
    Imagine that this loop never executes.

    In reality, it has to execute once because you've already taken care of the special case here
    > if (head->next == NULL)

    You can get the compiler to shut up by simply initialising prev = NULL;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    insertBack doesn't work for any but the first element added. After that, you search for the end of the list, but don't actually add the new element! (And you use the same variable name for two different things.)

    If you want to be able to add and remove things from both the front and back of the list, then you should probably have a "tail" pointer in StringList that always points to the last element. That way you don't have to constantly loop through the list just to find the end.

    Don't use free() in C++. delete is used to deallocate memory allocated with new.

    It's best not to dump the std namespace into your code. It really doesn't gain you much. If you really want to add symbols from the std namespace into your global namespace, then pick and choose:
    Code:
        using std::cout;
        using std::endl;
        using std::string;
    It looks like ListNode could use a ctor:
    Code:
        ListNode(std::string v, ListNode *n = nullptr)
            : value(v), next(n) {}
    Then insertFront could be:
    Code:
        void StringList::insertFront(std::string word) { 
            head = new ListNode(word, head); 
        }
    Use nullptr instead of NULL (that's what it's for!).

    Initialize your variables when you declare them, not in a separate statement.
    I.e., instead of this:
    Code:
        ListNode *nodePtr; 
        nodePtr = head;
    do this:
    Code:
        ListNode *nodePtr = head;

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    7
    Thanks I got it to work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-24-2014, 05:40 PM
  2. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  3. please help..linked list homework assignment due tomorrow..
    By rocketman03 in forum C Programming
    Replies: 2
    Last Post: 11-23-2008, 06:32 PM
  4. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread