c ++链表在开始时插入一个错误节点
c++ Linked list inserts a false node at start
我一直在使用链表编写程序,但出于某种原因,我的程序在程序开始时创建了一个错误节点,该节点混淆了其他所有内容
这个文件控制头文件中的函数
Node *createNode() {
Node *newNode = new Node;
cout << "Enter in Students last Name" << endl;
cin >> newNode->lastName;
cout << "Enter in Students first Name" << endl;
cin >> newNode->firstName;
cout << "Enter in Students id" << endl;
cin >> newNode->idNumber;
return newNode;
}
Node *insertNode(Node *ptr) {
Node *newNode = createNode();
if( ptr == NULL ) {
ptr = newNode;
} else if ( ptr->idNumber > newNode->idNumber ) {
newNode->next = ptr;
ptr = newNode;
} else {
Node *temp = ptr;
while( temp->next != NULL &&
temp->next->idNumber < newNode->idNumber )
temp = temp->next;
if( temp->next == NULL ) {
temp->next = newNode;
} else {
newNode->next = temp->next;
temp->next = newNode;
}
}
return ptr;
}
Node *searchNode(Node *ptr, int id) {
Node *temp;
Node *found;
bool isfound = false;
temp = ptr;
if(temp == NULL) {
cout << "There are no students in the list" << endl;
isfound = true;
}
while( isfound != true && temp->next != NULL ) {
if( temp->idNumber == id ) {
found = temp;
isfound = true;
cout << found->lastName << ", " << found->firstName
<< ": " << found->idNumber << endl;
} else {
temp = temp->next;
}
}
return found;
}
void printList( Node *ptr ){
Node *temp = ptr;
while( temp != NULL ) {
cout << temp->lastName << ", "
<< temp->firstName<< ": "
<< temp->idNumber << endl;
temp = temp->next;
}
}
这是执行程序的主文件
int main() {
Node *list = new Node();
displayList(list);
return 0;
}
void displayList( Node *node ) {
int option = -1;
cout << node->idNumber << endl;
while( option != 5 ) {
cout << endl;
cout << "What do you want to do?" << endl;
cout << "1: Add a student" << endl;
cout << "2: Search for student" << endl;
cout << "3: Delete Student" << endl;
cout << "4: Print Student List" << endl;
cout << "5: Exit" << endl;
cout << "Enter a number for the choice: ";
cin >> option;
if( cin.fail() || option > 5 || option < 0 ) {
cin.clear();
cin.ignore();
option = -1;
}
switch( option ) {
case 1:{
insertNode(node);
}
break;
case 2:{
if(node != NULL){
cout<<"Enter the id you want to find: ";
int sid;
cin >> sid;
searchNode(node, sid);
} else {
cout<<"No Ids in the list"<<endl;
}
}
break;
case 4: printList(node);
break;
case 5: cout<<"GoodBye."<<endl ;
break;
default: cout<<"you have entered a invalid character"<<endl;
}
}
}
这是在创建新节点之前正在创建的内容
What do you want to do?
1: Add a student
2: Search for student
3: Delete Student
4: Print Student List
5: Exit
Enter a number for the choice: 4
, : 0
Node *list = new Node();
因为您在 main 中创建了一个新的 Node
并将有效的 Node
地址传递给 displayList( Node *node );
然后传递给 printList( Node *ptr )
,它打印了零初始化 Node
数据。
您可以在 main()
中将 Node *list = new Node();
更改为 Node *list = NULL
删除 displayList( Node *node );
中的 cout << node->idNumber << endl;
否则您将遇到分段错误或空解引用。
在Node *createNode()
中添加newNode->next = 0;
必须将新创建的Node
的next
指针设置为NULL
。
在Node *searchNode(Node *ptr, int id)
中将while( isfound != true && temp->next != NULL )
改为while( isfound != true && temp != NULL )
,否则不会搜索最后一个节点。
将 insertNode(node)
的 return 值存储到 displayList( Node *node );
中的 node
为 node = insertNode(node)
否则你将失去新添加的节点 if
和 else if
例 insertNode
我一直在使用链表编写程序,但出于某种原因,我的程序在程序开始时创建了一个错误节点,该节点混淆了其他所有内容
这个文件控制头文件中的函数
Node *createNode() {
Node *newNode = new Node;
cout << "Enter in Students last Name" << endl;
cin >> newNode->lastName;
cout << "Enter in Students first Name" << endl;
cin >> newNode->firstName;
cout << "Enter in Students id" << endl;
cin >> newNode->idNumber;
return newNode;
}
Node *insertNode(Node *ptr) {
Node *newNode = createNode();
if( ptr == NULL ) {
ptr = newNode;
} else if ( ptr->idNumber > newNode->idNumber ) {
newNode->next = ptr;
ptr = newNode;
} else {
Node *temp = ptr;
while( temp->next != NULL &&
temp->next->idNumber < newNode->idNumber )
temp = temp->next;
if( temp->next == NULL ) {
temp->next = newNode;
} else {
newNode->next = temp->next;
temp->next = newNode;
}
}
return ptr;
}
Node *searchNode(Node *ptr, int id) {
Node *temp;
Node *found;
bool isfound = false;
temp = ptr;
if(temp == NULL) {
cout << "There are no students in the list" << endl;
isfound = true;
}
while( isfound != true && temp->next != NULL ) {
if( temp->idNumber == id ) {
found = temp;
isfound = true;
cout << found->lastName << ", " << found->firstName
<< ": " << found->idNumber << endl;
} else {
temp = temp->next;
}
}
return found;
}
void printList( Node *ptr ){
Node *temp = ptr;
while( temp != NULL ) {
cout << temp->lastName << ", "
<< temp->firstName<< ": "
<< temp->idNumber << endl;
temp = temp->next;
}
}
这是执行程序的主文件
int main() {
Node *list = new Node();
displayList(list);
return 0;
}
void displayList( Node *node ) {
int option = -1;
cout << node->idNumber << endl;
while( option != 5 ) {
cout << endl;
cout << "What do you want to do?" << endl;
cout << "1: Add a student" << endl;
cout << "2: Search for student" << endl;
cout << "3: Delete Student" << endl;
cout << "4: Print Student List" << endl;
cout << "5: Exit" << endl;
cout << "Enter a number for the choice: ";
cin >> option;
if( cin.fail() || option > 5 || option < 0 ) {
cin.clear();
cin.ignore();
option = -1;
}
switch( option ) {
case 1:{
insertNode(node);
}
break;
case 2:{
if(node != NULL){
cout<<"Enter the id you want to find: ";
int sid;
cin >> sid;
searchNode(node, sid);
} else {
cout<<"No Ids in the list"<<endl;
}
}
break;
case 4: printList(node);
break;
case 5: cout<<"GoodBye."<<endl ;
break;
default: cout<<"you have entered a invalid character"<<endl;
}
}
}
这是在创建新节点之前正在创建的内容
What do you want to do?
1: Add a student
2: Search for student
3: Delete Student
4: Print Student List
5: Exit
Enter a number for the choice: 4
, : 0
Node *list = new Node();
因为您在 main 中创建了一个新的 Node
并将有效的 Node
地址传递给 displayList( Node *node );
然后传递给 printList( Node *ptr )
,它打印了零初始化 Node
数据。
您可以在 main()
Node *list = new Node();
更改为 Node *list = NULL
删除 displayList( Node *node );
中的 cout << node->idNumber << endl;
否则您将遇到分段错误或空解引用。
在Node *createNode()
中添加newNode->next = 0;
必须将新创建的Node
的next
指针设置为NULL
。
在Node *searchNode(Node *ptr, int id)
中将while( isfound != true && temp->next != NULL )
改为while( isfound != true && temp != NULL )
,否则不会搜索最后一个节点。
将 insertNode(node)
的 return 值存储到 displayList( Node *node );
中的 node
为 node = insertNode(node)
否则你将失去新添加的节点 if
和 else if
例 insertNode