Thread: Doubly linked list

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    1

    Doubly linked list

    In the following code, I've implemented a double linked list in which each node is a structure with the following fields:
    firstName, seciondName, CNP, Email;
    When I call the function ptr->DeleteNODE for the fifth time to delete the last node left in the list the code is no longer running.
    Deleting nodes is done from the beginning of the list.
    I do not understand why ?
    Can you help me ?

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    
    struct Persons
    {
        string firstName;
        string secondName;
        string CNP;
        string Email;
    };
    
    
    class NODE
    {
    private:
        NODE *next;
        NODE *previous;
    public:
        Persons *box = new Persons();
        NODE(string firstName, string secondName, string CNP, string Email)
        {
            box->firstName = firstName;
            box->secondName = secondName;
            box->CNP = CNP;
            box->Email = Email;
        }
        void SetNext(NODE *next)
        {
            this->next = next;
        }
        NODE* GetNext()
        {
            return next;
        }
        void SetPrevious(NODE *previous)
        {
            this->previous = previous;
        }
        NODE *GetPrevious()
        {
            return previous;
        }
    };
    
    
    class DoublyLinkedList
    {
    private:
        NODE *head;
    public:
        DoublyLinkedList()
        {
            head = NULL;
        }
        bool isEmpty()
        {
            return head == NULL;
        }
        void AddNODE(NODE *newNode)
        {
            if (isEmpty())
            {
                newNode->SetPrevious(NULL);
                head = newNode;
            }
            else
            {
                NODE *temp = head;
                while (temp->GetNext() != NULL)
                    temp = temp->GetNext();
                temp->SetNext(newNode);
                newNode->SetPrevious(temp);
            }
            newNode->SetNext(NULL);
        }
        void DeleteNODE()
        {
            if (isEmpty())
                cout << "\n List is Empty." << endl;
            NODE *temp = head;
            if (head->GetNext() != NULL)
            {
                head = head->GetNext();
                head->SetPrevious(NULL);
            }
            else
                head = NULL;
            delete temp;
        }
        void Print()
        {
            NODE *temp = head;
            while (temp != NULL)
            {
                cout << "\n First Name  : " << temp->box->firstName;
                cout << "\n Second Name : " << temp->box->secondName;
                cout << "\n CNP         : " << temp->box->CNP;
                cout << "\n Email       : " << temp->box->Email;
                temp = temp->GetNext();
                cout << endl;
            }
        }
    };
    
    
    int main()
    {
        DoublyLinkedList *ptr = new DoublyLinkedList();
    
    
        NODE obj1("Dragu", "Stelian", "1911226284570", "[email protected]");
        NODE obj2("Dragu", "Mircea", "1891226284462", "[email protected]");
        NODE obj3("David", "Adrian", "1971226284462", "[email protected]");
        NODE obj4("Afrem", "Dragos", "1981246627446", "[email protected]");
        NODE obj5("Sandu", "Marius", "1984225774462", "[email protected]");
    
    
        ptr->AddNODE(&obj1);
        ptr->AddNODE(&obj2);
        ptr->AddNODE(&obj3);
        ptr->AddNODE(&obj4);
        ptr->AddNODE(&obj5);
    
    
        ptr->Print();
    
    
        ptr->DeleteNODE();
        ptr->DeleteNODE();
        ptr->DeleteNODE();
        ptr->DeleteNODE();
        ptr->DeleteNODE();
    
    
    
    
        ptr->Print();
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    does first in first out ring a bell, or last in first out, depending on what you mean by beginning of list.
    this function
    ( never mind, I'll leave you with this)

    Delete a Doubly Linked List node at a given position - GeeksforGeeks
    Last edited by userxbw; 09-22-2017 at 07:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubly linked list
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-26-2007, 03:41 AM
  2. doubly linked list
    By bahareh in forum C++ Programming
    Replies: 7
    Last Post: 03-28-2007, 01:31 PM
  3. Doubly-Linked List
    By jgs in forum C Programming
    Replies: 7
    Last Post: 04-18-2005, 01:39 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. Doubly Linked List.. please Help!!
    By ProgrammingDlux in forum C++ Programming
    Replies: 8
    Last Post: 10-24-2004, 08:27 PM

Tags for this Thread