EatRocks
EatRocks

Reputation: 31

Doubly linked list c++ crashing after 3rd node

So this is a doubly linked list that is supposed to hold names, address, and phone numbe and print them out. It works for the first 3 nodes then suddenly crashes after the the phone number entry on the third node. something is wrong with the pointers I believe but I have tried everything I can think of.

    #include <iostream>
    using namespace std;

class node
{
private:
    string elem;
    node* next;
    node* prev;
    string firstName;
    string lastName;
    string address;
    string phoneNumber;
    friend class linkedList;
};

//Linked list
class linkedList
{
public:
linkedList();
void addFrontNode(const string& e);
void addNode(const string& e);
void addNode2(node* nextloc, const string& e);
void addNode3(node* nextloc, const string& e);
void addNode4(node* nextloc, const string& e);
void print();
void search();
node* nextloc;
private:
node* head;
node* tail;


};

void linkedList::addFrontNode(const string &e)
{
node* v = new node;
string firstNameEntry;
string lastNameEntry;
string addressEntry;
string phoneNumberEntry;

cout << "Enter first name: ";
cin >> firstNameEntry;

cout << "Enter last name: ";
cin >> lastNameEntry;

cout << "Enter the address ";
cin.ignore();
getline(cin, addressEntry);

cout << "Enter a phone number ";
cin >> phoneNumberEntry;

v->elem = firstNameEntry;
v->lastName = lastNameEntry;
v->address = addressEntry;
v->phoneNumber = phoneNumberEntry;
v->next = head;
head = v;


}
void linkedList::addNode(const string &e)
{
node* v = new node;
string firstNameEntry;
string lastNameEntry;
string addressEntry;
string phoneNumberEntry;

cout << "Enter first name: ";
cin >> firstNameEntry;

cout << "Enter last name: ";
cin >> lastNameEntry;

cout << "Enter the address ";
cin.ignore();
getline(cin, addressEntry);

cout << "Enter a phone number ";
cin >> phoneNumberEntry;
v->elem = firstNameEntry;
v->lastName = lastNameEntry;
v->address = addressEntry;
v->phoneNumber = phoneNumberEntry;

v->next = tail;
tail = v;
tail->next = NULL;
}

void linkedList::addNode2(node* nextloc, const string &e)
{
node* v = new node;
string firstNameEntry;
string lastNameEntry;
string addressEntry;
string phoneNumberEntry;

cout << "Enter first name: ";
cin >> firstNameEntry;

cout << "Enter last name: ";
cin >> lastNameEntry;

cout << "Enter the address ";
cin.ignore();
getline(cin, addressEntry);

cout << "Enter a phone number ";
cin >> phoneNumberEntry;
v->elem = firstNameEntry;
v->lastName = lastNameEntry;
v->address = addressEntry;
v->phoneNumber = phoneNumberEntry;

nextloc = head -> next;
v->next = nextloc;

v->next = nextloc;
v->prev = nextloc->prev;
nextloc->prev = v;
}
void linkedList::addNode3(node* nextloc, const string &e)
{
node* v = new node;
string firstNameEntry;
string lastNameEntry;
string addressEntry;
string phoneNumberEntry;

cout << "Enter first name: ";
cin >> firstNameEntry;

cout << " Enter last name: ";
cin >> lastNameEntry;

cout << " Enter the address ";
cin.ignore();
getline(cin, addressEntry);

cout << " Enter a phone number ";
cin >> phoneNumberEntry;
v->elem = firstNameEntry;
v->lastName = lastNameEntry;
v->address = addressEntry;
v->phoneNumber = phoneNumberEntry;

v->next = nextloc;
v->prev = nextloc->prev;
nextloc->prev = v;
}

void linkedList::addNode4(node* nextloc, const string &e)
{
node* v = new node;
string firstNameEntry;
string lastNameEntry;
string addressEntry;
string phoneNumberEntry;

cout << "Enter first name: ";
cin >> firstNameEntry;

cout << " Enter last name: ";
cin >> lastNameEntry;

cout << " Enter the address ";
cin.ignore();
getline(cin, addressEntry);

cout << " Enter a phone number ";
cin >> phoneNumberEntry;
v->elem = firstNameEntry;
v->lastName = lastNameEntry;
v->address = addressEntry;
v->phoneNumber = phoneNumberEntry;

v->next = nextloc;
v->prev = nextloc->prev;
nextloc->prev->next = v;
nextloc->prev = v;
}
linkedList::linkedList() :head(NULL) {}

void linkedList::print()
{
node* v = new node;
v = head;
while (v != NULL)
{
    cout << v->elem << " ";
    cout << v->lastName << " ";
    cout << v->address << " ";
    cout << v->phoneNumber;
    v = v->next;
}
}
void linkedList::search()
{
node* v = new node;
v = tail;
string lastNameSearch;
cout << "Enter a last name to search ";
cin >> lastNameSearch;
while (v != NULL)
{
    if (v->lastName == lastNameSearch)
    {
        cout << v->elem;
        cout << v->address;
        cout << v->phoneNumber;

    }
    v = v->prev;
}
}
int main()
{
string node1;
string node2;
string node3;
string node31;
string node4;
string node5;

linkedList list;
list.addFrontNode(node1);
list.addNode(node2);
list.addNode2(list.nextloc, node3);
list.addNode3(list.nextloc, node4);
list.addNode4(list.nextloc, node5);
list.print();
return 0;
}

Upvotes: 0

Views: 92

Answers (2)

isp-zax
isp-zax

Reputation: 3873

The code is indeed in need of re-write, but I don't want to repeat recommendations from the first answer. I however, believe that the question was about the reasons of the crash. It's because while copy-pasting the code, you added 2 extra lines to your addNode2:

void linkedList::addNode2(node* nextloc, const string &e)
{
...
nextloc = head -> next;
v->next = nextloc;
...
}

Comment out at least first of them and it won't crash any more (but this wouldn't make it better, really).

Upvotes: 0

Kajienk
Kajienk

Reputation: 111

There are several issues.

  1. If you use addFrontNode() to add first node, you must set your tail. Change this:

    v->next = head;
    head = v;
    

    To this:

    v->next = NULL;
    head = v;
    tail = v;
    
  2. Your function addNode() doesn't add to list correctly, try calling print and you will see, no node is added by this function. Change this:

    v->next = tail;
    tail = v;
    tail->next = NULL;
    

    To this:

    tail->next = v;
    v->next = NULL;
    tail = v;
    
  3. In main() just use addFrontNode() to add first and then use addNode() to add all others. After this your code worked as expected.

  4. Didn't understand meaning of variable nextloc, might be the source of problems.

Overall recommendation: create one function to add node

Upvotes: 1

Related Questions