尝试打印链表时出现运行时错误
Run time error when I try printing the linked list
这是我的代码:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct node
{
string program;
node *next;
}
bool isEmpty(node *head)
{
if (head == NULL)
return true;
else
return false;
}
void insertAsFirstElement(node *&head, node *&tail, string program)
{
node *temp = new node;
temp->program = program;
temp->next = NULL;
head = temp;
tail = temp;
}
void initialize(node *&head, node *&tail, string program)
{
if (isEmpty(head))
insertAsFirstElement(head, tail, program);
else
{
node* temp = new node;
temp->program = program;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
}
void insert(node *& head, node *& tail, string program, int num)
{
if (isEmpty(head))
insertAsFirstElement(head, tail, program);
else
{
string free ("FREE");
int i = 0;
while (head != NULL)
{
while (head->program.compare(free) != 0)
head = head->next;
while (head->program.compare(free) == 0)
{
head->program = program;
tail->next = head;
tail = head;
i++;
if (i == (num-1))
return;
}
}
}
}
void showList(node *current)
{
if (isEmpty(current))
cout << "The list is empty. n";
else
{
int i = 0;
cout << "The list contains: n";
while(current != NULL)
{
cout << current->program << " ";
if ((i + 1) % 8 == 0)
cout << "n";
current = current->next;
i++;
}
}
}
int main()
{
cout << "Menu";
cout << "n1. Add programn";
cout << "2. Print Memoryn";
cout << "3. Exitn";
node *head = NULL;
node *tail = NULL;
int choice;
string name;
int memory;
int numPages;
for (int i = 0; i <= 31; i++)
{
initialize(head, tail, "FREE");
}
showList(head);
do
{
cout << "choice - ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Program name - ";
cin >> name;
cout << "Program size - ";
cin >> memory;
if (memory % 4 == 0)
numPages = memory / 4;
else if (memory % 4 != 0)
numPages = memory / 4 + 1;
insert(head, tail, name, numPages);
cout << "Program " << name << " added succesfully.n";
case 2:
showList(head);
}
} while (choice!=3);
return 0;
}
错误出在插入函数中,因为当我在调用插入函数后尝试打印链表时,它永远不会停止打印,但我不明白我的错误。同样在主开关中,当插入 2 作为选择时,它只运行案例 2,但当我插入 1 作为选择时,它同时运行案例 1 和案例 2。
编辑:我没有改变任何东西,现在一旦我调用插入函数,程序就会停止运行
关于switch
情况,您需要在case 1:
之后添加break;
"错误出在插入函数中">
那你为什么不修复它呢?
我同意,您的插入功能有缺陷。 下面我标记了 3 行可能导致代码问题。
关键思想:将第二个和后续项插入链表时,代码应仅修改"head"或"tail",而不应同时修改两者。
插入头部时,尾部不应改变。在尾部插入时,头部不应改变。
vv vv
void insert(node *& head, node *& tail, string program, int num)
{
if (isEmpty(head))
insertAsFirstElement(head, tail, program);
else
{
string free ("FREE");
int i = 0;
while (head != NULL)
{
while (head->program.compare(free) != 0)
head = head->next; <<<<<<<<<<<<<<<<<<<<<
while (head->program.compare(free) == 0)
{
head->program = program; <<<<<<<<<<<<<<<<<<<<
tail->next = head;
tail = head; <<<<<<<<<<<<<<<<<<<<
i++;
if (i == (num-1))
return;
}
}
}
}
您的switch
语句缺少break;
语句。由于案例 1 没有break
语句,编译器将继续处理案例 1 和 2,因为案例 2 紧随案例 1 之后。
以下是教程点中更清晰的描述:
当打开的变量等于大小写时,该大小写后面的语句将执行,直到到达 break 语句。
当到达中断语句时,开关终止,控制流跳转到 switch 语句后面的下一行。
并非每个案例都需要包含中断。如果未出现中断,则控制流将下降到后续案例,直到达到中断。
请参阅此代码:
switch (choice)
{
case 1:
// ... Add the rest of your code here
break // <-- this is required so that the switch is termintated after completing the appropriate case instead of continuing on to the next case
case 2:
showList(head); // there are no more cases after this, so only this case runs if switch(2) occurs.
}
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- std :: list(双重链接列表)未知运行时错误
- 双链表的运行时错误
- 对双向链表进行排序会产生运行时错误
- 尝试打印链表时出现运行时错误
- 尝试拆分循环链表.显示输出但崩溃.在 IDEone 中显示运行时错误
- 第15次循环后出现链表运行时错误
- 类链接列表模板运行时错误
- 我想在我的代码中实现一个双重循环链表 - 运行时错误
- 链表重载运算符 = 运行时错误
- GLPK OpenBLAS 运行时 dll 链接错误
- 使用nVIDIA编译器为我的GNU编译器生成要链接的共享库时发生运行时错误
- 为什么链接库的顺序会导致运行时错误
- 链表中出现运行时错误