Thread: Linked List questions

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    1

    Linked List questions

    I'm having trouble with a linked list. I keep getting a seg fault when I hit the del function. I'm not sure if my linked list is bad, or if my del function is bad... or if it's the destructor.

    Any help is appreciated!!!

    app.cpp:

    Code:
    #include <iostream>
    #include "linkedlist.h"
    
    
    using namespace std;
    
    
    void find(LinkedList& list, char ch)
    {
            if (list.find(ch))
                    cout << "found ";
            else
                    cout << "did not find ";
            cout << ch << endl;
    }
    
    
    int main()
    {
        LinkedList      list;
    
    
        list.add('x');
        list.add('y');
        list.add('z');
        cout << list;
        find(list, 'y');
    
    
        list.del('y');
        cout << list;
        find(list, 'y');
    
    
        list.del('x');
        cout << list;
        find(list, 'y');
    
    
        list.del('z');
        cout << list;
        find(list, 'y');
    
    
        return 0;
    }
    linkedlist.h:
    Code:
    #ifndef _LINKED_LIST_#define _LINKED_LIST_
    
    
    #include <ostream>
    
    
    class LinkedList
    {
    public:
            LinkedList();
            ~LinkedList();
    
    
            void add(char ch);
            bool find(char ch);
            bool del(char ch);
    
    
            friend std::ostream& operator<<(std::ostream& out, LinkedList& list);
    
    
    private:
        char letter; //spot to store our letter
        LinkedList *next; //pointer to next list elemenent
    
    
    };
    
    
    #endif // _LINKED_LIST_
    linkedlist.cpp:
    Code:
    #include "linkedlist.h"
    using namespace std;
    
    
    //default data constructor
    LinkedList::LinkedList()
    {
        //initialize letters pointer to null
        letter = '\n';
        next = nullptr;
    }
    
    
    //default destructor
    LinkedList::~LinkedList()
    {
        LinkedList *forward = nullptr;
        LinkedList *current = this;
        //iterate through list, deleting each element as we go
        while (current != nullptr)
        {
                //set next pointer to current's next
                forward = current->next;
                delete current; //delete the current memory
                current = forward; //reset current to next's pointer
        }
    }
    
    
    //FUNCTION - add letter (single) to letters list
    void LinkedList::add(char ch)
    {
        LinkedList *current = this;
        LinkedList *prev = nullptr;
        LinkedList *new_node = new LinkedList();
        if (current == nullptr) //check if it's first item in the list
        {
            new_node->letter = ch;
            new_node->next = nullptr;
            current = new_node;
            return;
        }
        while (current != nullptr)
        {
            prev = current;
            current = current->next;
        }
    
    
        new_node->letter = ch;
        new_node->next = nullptr;
        prev->next = new_node;
        return;
    }
    
    
    //FUNCTION - find a letter in our letters list
    bool LinkedList::find(char ch)
    {
        LinkedList *temp = this;
        while (temp != nullptr)
        {
            if (temp->letter == ch) //if ch is found in our list, return true
                return true;
            else
                temp = temp->next;
        }
        return false; //if ch is not found
    }
    
    
    //FUNCTION - //delete a letter from our letters list
    bool LinkedList::del(char ch)
    {
        LinkedList *current = this;
        LinkedList *previous = nullptr;
    
    
        if (current == nullptr) //if current is empty
            return false;
        
        if (current->letter == ch) //if the first spot is the correct spot to delete
        {
            previous = current;
            current = current->next;
            delete previous;
            return true;
        }
        
        while (current->letter != ch && current != nullptr) //find spot to delete at
        {
            previous = current;
            current = current->next;
        }
        if (current == nullptr) //it didn't find the ch
            return false;
        else
        {
            if (current->next == nullptr) //if at end of list
            {
                previous->next = nullptr;
                delete current;
                return true;
            }
            else
            {
                previous->next = current->next;
                delete current;
                return true;
            }
        }
    }
        
    //overload friend << operator
    std::ostream& operator<<(std::ostream& out, LinkedList& list)
    {
        LinkedList *temp = &list;
        while (temp != nullptr)
        {
            out << temp->letter;
            temp = temp->next;
        }
        out << endl;
        return out;
    }

    What am I doing wrong??
    So far, with the code I have here, I get the first three letters added and printed out, then it hangs up on the del function it appears. I'm thinking it's something to do with the destructor...but I'm not sure.

    I should also point out, I'm not to change anything in the main function. Only the linkedlist.cpp file and the private variables in the linkedlist.h file.
    Last edited by stumbaup; 04-07-2020 at 12:58 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    966
    Your basic design is seriously flawed. It doesn't really make sense. Look at this:
    Code:
    void LinkedList::add(char ch)
    {
        LinkedList *current = this;
        // ...
        if (current == nullptr) //check if it's first item in the
    Do you really think that this will ever be nullptr? How could you have called a member function with a null this pointer?

    Instead you should have a List class and a Node class.
    Code:
    struct Node
    {
        char  data;
        Node* next;
     
        Node(char d, Node* n = nullptr) : data(d), next(n) {}
    };
     
    class List
    {
        Node *head = nullptr;
        Node *tail = nullptr;
    public:
        List() = default;
        ~List() { clear(); }
        void clear()
        {
            while (head)
            {
                auto to_del = head;
                head = head->next;
                delete to_del;
            }
            tail = nullptr;
        }
        void insert(char data)
        {
            auto node = new Node(data);
            if (tail)
                tail->next = node;
            else
                head = node;
            tail = node;
        }
        void print() const
        {
            for (auto node = head; node; node = node->next)
                std::cout << node->data << ' ';
            std::cout << '\n';
        }
    };
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yet more Pointers and Linked List Questions
    By (^Burt^) in forum C Programming
    Replies: 3
    Last Post: 10-22-2013, 08:33 AM
  2. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  3. Newbie questions about pointers and linked list
    By koplersky in forum C Programming
    Replies: 4
    Last Post: 10-07-2012, 08:43 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. linked list questions...
    By revelation437 in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2003, 04:42 PM

Tags for this Thread