Thread: Linked List help

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

    Linked List help

    So I created this code and I am trying to figure out why my insertback function won't insert a node in the back, instead it just repeats the first node.
    Code:
    #include "StringList.h"#include<iostream>
    
    
    using std::cout;
    using std::endl;
    
    
    
    
    StringList::~StringList()
    {
        ListNode *nodePtr;
        ListNode *nextNode;
        nodePtr = head;
        while (nodePtr != nullptr)
        {
            nextNode = nodePtr->next;
            delete nodePtr;
            nodePtr = nextNode;
        }
    }
    
    
    void StringList::insertFront(std::string word)
    {
        ListNode *temp = new ListNode;
        temp->value = word;
        temp->next = head;
        head = temp;
    }
    
    
    void StringList::insertBack(std::string r)
    {
        ListNode *temp = new ListNode;
        temp->value = r;
        temp->next = NULL;
        if (head == NULL)
        {
            head = temp;
        }
        else
        {
            ListNode *temp = head;
            while (temp->next != NULL)
            {
                temp = temp->next;
            }
        }
    }
    
    
    void StringList::deleteFront()
    {
        ListNode *temp = head;
        head = head->next;
        delete(temp);
    }
    
    
    void StringList::deleteBack()
    {
        ListNode *temp, *prev;
        prev = nullptr;
        if (head->next == NULL)
        {
            temp = head;
            head = NULL;
            delete(temp);
        }
        else {
            temp = head;
            while (temp->next != NULL)
            {
                prev = temp;
                temp = temp-> next;
            }
            prev->next = NULL;
            delete(temp);
        }
    }
    
    
    void StringList::displayList() const
    {
        ListNode *nodePtr = head;
        
        while (nodePtr)
        {
            cout << nodePtr->value << endl;
            nodePtr = nodePtr->next;
        }
    }

  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
    Be careful of your variable naming.
    Code:
    $ g++ -Wall -Wshadow -c foo.cpp
    foo.cpp: In member function ‘void StringList::insertBack(std::__cxx11::string)’:
    foo.cpp:54:19: warning: declaration of ‘temp’ shadows a previous local [-Wshadow]
             ListNode *temp = head;
                       ^
    foo.cpp:45:15: note: shadowed declaration is here
         ListNode *temp = new ListNode;
                   ^
    You have two variables called temp in two nested scopes.

    Code:
    void StringList::insertBack(std::string r)
    {
        ListNode *newNode = new ListNode;
        newNode->value = r;
        newNode->next = NULL;
        if (head == NULL)
        {
            head = newNode;
        }
        else
        {
            ListNode *temp = head;
            while (temp->next != NULL)
            {
                temp = temp->next;
            }
            temp->next = newNode;  // actually append it
        }
    }
    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
    Nov 2015
    Posts
    119
    Can you put out all the code, or project, I'm on my'll compile?
    Last edited by Dmy; 07-24-2017 at 05:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  2. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread