尝试打印链表时出现运行时错误

Run time error when I try printing the linked list

本文关键字:运行时错误 链表 打印      更新时间:2023-10-16

这是我的代码:

#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.
}