Click here to Skip to main content
15,886,362 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Syntax error Pin
Vaclav_28-Sep-17 7:10
Vaclav_28-Sep-17 7:10 
Questionuser name in system service Pin
john563220-Sep-17 19:31
john563220-Sep-17 19:31 
AnswerRe: user name in system service Pin
Jochen Arndt20-Sep-17 21:25
professionalJochen Arndt20-Sep-17 21:25 
QuestionTCF Eclipse users please reply Pin
Vaclav_20-Sep-17 13:39
Vaclav_20-Sep-17 13:39 
AnswerRe: TCF Eclipse users please reply Pin
Richard MacCutchan20-Sep-17 21:31
mveRichard MacCutchan20-Sep-17 21:31 
GeneralRe: TCF Eclipse users please reply Pin
Vaclav_21-Sep-17 13:06
Vaclav_21-Sep-17 13:06 
GeneralRe: TCF Eclipse users please reply Pin
Richard MacCutchan21-Sep-17 21:00
mveRichard MacCutchan21-Sep-17 21:00 
QuestionDoubly Linked List Pin
kinderu16-Sep-17 23:17
kinderu16-Sep-17 23:17 
In the following code, I've implemented a doubly linked list in which each node is a structure with the following fields:
firstName, secondName, CNP, Email;
When I call the function ptr->DeleteNODE for the fifth time to delete the last node left in the list the code crashes.
Deleting nodes is done from the beginning of the list.
I don't understand why it's crashes.


C++
#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", "dragu_stelian@yahoo.com");
    NODE obj2("Dragu", "Mircea", "1891226284462", "mircead.personal@yahoo.com");
    NODE obj3("David", "Adrian", "1971226284462", "david_adrian@yahoo.com");
    NODE obj4("Afrem", "Dragos", "1981246627446", "afrem_dragos@yahoo.com");
    NODE obj5("Sandu", "Marius", "1984225774462", "sandu.marius@yahoo.com");
 
 
    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;
}

AnswerRe: Doubly Linked List Pin
Richard MacCutchan17-Sep-17 1:49
mveRichard MacCutchan17-Sep-17 1:49 
AnswerRe: Doubly Linked List Pin
leon de boer17-Sep-17 4:42
leon de boer17-Sep-17 4:42 
AnswerRe: Doubly Linked List Pin
Jochen Arndt17-Sep-17 21:45
professionalJochen Arndt17-Sep-17 21:45 
QuestionSolution access internet by proxy server c++ Pin
Con Cop Con13-Sep-17 19:22
Con Cop Con13-Sep-17 19:22 
AnswerRe: Solution access internet by proxy server c++ Pin
jschell18-Sep-17 12:32
jschell18-Sep-17 12:32 
QuestionLinker error Pin
Vaclav_12-Sep-17 15:53
Vaclav_12-Sep-17 15:53 
AnswerRe: Linker error Pin
Richard MacCutchan12-Sep-17 19:12
mveRichard MacCutchan12-Sep-17 19:12 
AnswerRe: Linker error Pin
CPallini13-Sep-17 1:25
mveCPallini13-Sep-17 1:25 
GeneralRe: Linker error Pin
Vaclav_13-Sep-17 3:14
Vaclav_13-Sep-17 3:14 
GeneralRe: Linker error Pin
leon de boer13-Sep-17 4:02
leon de boer13-Sep-17 4:02 
GeneralRe: Linker error Pin
jschell13-Sep-17 8:23
jschell13-Sep-17 8:23 
AnswerRe: Linker error Pin
Jochen Arndt13-Sep-17 4:14
professionalJochen Arndt13-Sep-17 4:14 
GeneralRe: Linker error Pin
Vaclav_13-Sep-17 9:05
Vaclav_13-Sep-17 9:05 
GeneralRe: Linker error Pin
Jochen Arndt14-Sep-17 3:38
professionalJochen Arndt14-Sep-17 3:38 
GeneralRe: Linker error Pin
leon de boer15-Sep-17 22:21
leon de boer15-Sep-17 22:21 
GeneralRe: Linker error Pin
Vaclav_16-Sep-17 3:02
Vaclav_16-Sep-17 3:02 
GeneralRe: Linker error Pin
leon de boer16-Sep-17 3:16
leon de boer16-Sep-17 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.