[英]Doubly linked list c++ crashing after 3rd node
因此,這是一個雙向鏈接的列表,應該包含名稱,地址和電話號碼並打印出來。 它適用於前三個節點,然后在第三個節點上輸入電話號碼后突然崩潰。 我相信這些指針有問題,但是我已經嘗試了所有我能想到的東西。
#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;
}
有幾個問題。
如果使用addFrontNode()
添加第一個節點,則必須設置tail
。 更改此:
v->next = head; head = v;
對此:
v->next = NULL; head = v; tail = v;
您的函數addNode()
未正確添加到列表中,請嘗試調用print
,您將看到此函數未添加任何節點。 更改此:
v->next = tail; tail = v; tail->next = NULL;
對此:
tail->next = v; v->next = NULL; tail = v;
在main()
只需使用addFrontNode()
首先添加,然后使用addNode()
添加所有其他。 之后,您的代碼按預期工作。
不了解變量nextloc
含義,可能是問題的根源。
總體建議:創建一個功能以添加節點
該代碼確實需要重寫,但是我不想重復第一個答案的建議。 但是,我認為問題在於墜機的原因。 這是因為在復制粘貼代碼時,您向addNode2添加了兩行:
void linkedList::addNode2(node* nextloc, const string &e)
{
...
nextloc = head -> next;
v->next = nextloc;
...
}
至少先注釋掉它們 ,它不會再崩潰(但這實際上並不能使它變得更好)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.