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)
到node
的返回值存储在displayList( Node *node );
中作为node = insertNode(node)
,否则您将失去insertNode
的if
和else if
情况下新增的节点
相关文章:
- 试图修复一个错误,该错误不会让我开始编程其余部分
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 我正在尝试一个傻瓜 C++ 练习,我遇到了一个错误,说类 'GraduateStudent' 没有任何名为 'advisor' 的字段
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"
- 在 C++ 中使用 "transform" 会给出一个错误,指出这未在作用域中声明
- 在Cython中使用C库时,我遇到了一个错误
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 我试图用c++编写递归fibonacci序列,但当我编译时,我遇到了一个错误
- 从system()调用G++会返回一个错误
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 我收到一个错误无效的操作数,类型为 const char [42] 和二进制"运算符+"的双倍数
- 使用声明:GCC 和 Clang 的另一个错误?
- 全球免费给出一个错误.调试器不解释
- 相对于继承的构造函数,gcc 编译器是否还有一个错误?
- 为什么直接传递"this"指针来存档是一个错误,而另一个相同类型的指针是可以的?
- 为什么第三板有一个错误
- 如何编写一个错误结构,该结构可以包含不同的强键枚举作为错误代码
- 使用本地类型声明的G lambda被使用但从未定义 - 确实是一个错误
- 我需要在 Android Studio 中构建 NDK. 但它返回一个错误