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

时间:2017-03-25 11:49:36

标签: c++ 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 program\n";
    cout << "2. Print Memory\n";
    cout << "3. Exit\n";
    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。

编辑:我没有改变任何东西,现在一旦我调用插入功能,程序就会停止运行

3 个答案:

答案 0 :(得分:0)

关于switch案例,您需要在break;之后添加case 1:

答案 1 :(得分:0)

您的break;声明缺少break声明。由于案例1没有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. } 语句,编译器继续通过案例1和案例2,因为案例2遵循案例1。

以下是来自Tutorialspoint的更清晰的说明:

  

当打开的变量等于大小写时,该大小写后的语句将一直执行,直到达到break语句

     

当达到break语句时,交换机终止,控制流跳转到switch语句后面的下一行。

     

并非每个案例都需要包含休息时间。如果没有出现中断,则控制流将落入后续案例,直到达到中断为止。

请参阅此代码:

dict.get()

答案 2 :(得分:0)

“错误在插入函数中”

那你为什么不解决它呢?

我同意,您的插入功能存在缺陷。下面我标记了可能导致代码问题的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;
           }
       }
   }

}