链接列表作业数组-运行时错误 [英] Array of Linked List Homework - Runtime Error

查看:91
本文介绍了链接列表作业数组-运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做这项家庭作业,我觉得我已经尽力了,但是这实在令人沮丧.基本上,该程序应该模拟一个监视计算机实验室的人的管理控制台.主菜单上有3个选项,除了注销"功能外,我所有的选项都可以使用.

I've been doing this homework assignment and I feel like I've tried anything that I could do, but this is just so frustrating. Basically this program is supposed to simulate the admin console of someone who monitors computers labs. There are 3 options at the main menu, and I've got all of them to work except for the "logoff" function.

此程序的大问题是,实验室应该是阵列,而位于特定计算机站的ID号应该存储在链接列表中.我最努力的部分是链接列表.

The big issue with this program is that the labs are supposed to be arrays and the ID Numbers that are located at a particular computer station are supposed to be stored in a linked list. The part that I am struggling the most with is the linked list.

运行程序时,模拟登录,然后尝试注销程序崩溃.因此,我尝试使用Visual Studio对其进行调试,它告诉我在第121行有一个未处理的异常(注释如下,该异常位于注销功能中).我尝试多次与老师联系,但他没有回应.如果有人能指出我正确的方向,那就太好了.

When I run the program, simulate a login, and then try to logoff my program crashes. So I tried debugging it using Visual Studio, and it's telling me that there is an exception that is unhandled on line 121 (commented below it's located in the logoff function). I tried contacting my teacher multiple times, but he hasn't responded. It would be great if someone could point me in the right direction.

我注意到删除功能可能会引起问题.尽管似乎删除了列表中的节点,但是当我看一下调试器时,似乎在一个节点上还有一些垃圾数据.删除节点时是否应该进行某种初始化?像删除节点时将变量设置为NULL或nullptr一样?

edit: I notice that the remove function could be causing issues. Although it seems to remove the node in the list it seems like there is some garbage data left in one node when I take a look at the debugger. Am I supposed to do some sort of initialization when I remove a node? Like setting variables to NULL or nullptr when I remove a node?

edit 2:我缩短了代码.这是我得到的例外: 引发未处理的异常:读取访问冲突. iter 为nullptr.

edit 2: I shortened the code. Here is the exception I'm getting: Unhandled exception thrown: read access violation.iter was nullptr.

#include <iostream>
using namespace std;

struct User
{
    int IDNumber;
    int stationNumber;
    User *link;
};

typedef User* NodePtr;

const int COMPUTER_LABS = 4; 
const int COMPUTERLAB_SIZES[] = { 5, 6, 4, 3 }; number of labs

void head_insert(NodePtr& head, int IDNum, int stationNum)
{
    NodePtr temp_ptr;
    temp_ptr = new User;

    temp_ptr->IDNumber = IDNum;
    temp_ptr->stationNumber = stationNum;

    temp_ptr->link = head;

    head = temp_ptr;
}

void remove(NodePtr& head, int position)
{

    if (head == NULL)
        return;

    NodePtr temp = head;

    if (position == 0)
    {
        head = temp->link;
        delete temp;
        return;
    }

    for (int i = 0; temp != NULL && i<position - 1; i++)
        temp = temp->link;

    if (temp == NULL || temp->link == NULL)
        return;

    NodePtr next = temp->link->link; 


    delete temp->link; 

    temp->link = next;

}

//This function will take in our multidimensional array and prompt the user for data on simulating a login
//At the end, the spot will be assigned a number
void login(NodePtr labs[])
{
    int IDNum, labNum, stationNum;
    cout << "Enter the 5 digit ID number of the user logging in: " << endl;
    cin >> IDNum;
    cout << "Enter the lab number the user is logging in from (1-4): " << endl;
    cin >> labNum;
    cout << "Enter computer station number the user is logging in to (1-6): " << endl;
    cin >> stationNum;

    if (labs[labNum - 1]->link == NULL && labs[labNum - 1]->stationNumber == NULL)
    {
        labs[labNum - 1]->stationNumber = stationNum;
        labs[labNum - 1]->IDNumber = IDNum;
    }
    else
        head_insert(labs[labNum - 1], IDNum, stationNum);

}

void showLabs(NodePtr labs[])
{
    for (int i = 0; i < COMPUTER_LABS; i++)
    {
        cout << i + 1 << ": ";
        for (int j = 0; j<COMPUTERLAB_SIZES[i]; j++)
        {
            for (NodePtr iter = labs[i]; iter != NULL; iter = iter->link)
            {
                if (iter->stationNumber == (j + 1))
                {
                    cout << j + 1 << ": " << (iter->IDNumber) << " ";
                    break;
                }
                else if ((iter->link) == NULL && (iter->stationNumber) != (j + 1))
                    cout << j + 1 << ": empty ";
            }
        }
        cout << endl;
    }

}

//This function will simulate a logoff by looking for the ID number that was typed in
//It will then clear that computer station and log the user off
void logoff(NodePtr labs[])
{
    int IDNumber;
    bool isBreak = false;
    cout << "Enter 5 digit ID number of the user to find: " << endl;
    cin >> IDNumber;

    for (int i = 0; i < COMPUTER_LABS; i++)
    {
        int j = 0;
        for (NodePtr iter = labs[i]; iter != NULL; iter = iter->link, j++) //This is where it says exception unhandled
        {
            if (iter->IDNumber == IDNumber)
                remove(iter, j);
        }
    }

    cout << "User " << IDNumber << " is logged off." << endl << endl;
}


int main()
{
    NodePtr computerLabs[COMPUTER_LABS];
    for (int i = 0; i < COMPUTER_LABS; i++)//Initialize nodes in array so that we don't get any undefined behavior from not initializing
    {
        computerLabs[i] = new User;
        computerLabs[i]->stationNumber = NULL;
        computerLabs[i]->IDNumber = NULL;
        computerLabs[i]->link = NULL;
    }

    int userChoice; //This is for making a choice at the main menu
    do
    {
        cout << "LAB STATUS" << endl;
        cout << "Lab # Computer Stations" << endl;

        showLabs(computerLabs); //Show the current computer labs and their statuses

        cout << endl << "MAIN MENU" << endl;
        cout << "0) Quit" << endl;
        cout << "1) Simulate login" << endl;
        cout << "2) Simulate logoff" << endl;

        cin >> userChoice; // ask user for choice 

        if (userChoice == 1)
            login(computerLabs);
        else if (userChoice == 2)
            logoff(computerLabs);
    } while (userChoice != 0);
    return 0;
}

推荐答案

这段代码几乎是完全错误的.要修复您的代码,让我们从不要求用户输入开始.使用预定值可以使程序更易于调试.了解基本知识后,您可以添加用户输入.

This code is almost completely wrong. To fix your code, let's start by not asking for user input. Use predetermined values to make the program easier to debug. You can add user input after you get the basics.

不要使用声明typedef User* NodePtr没什么问题,但是它隐藏了指针,可能会使您感到困惑.

Don't use the declaration typedef User* NodePtr There is nothing wrong with it, but it hides the pointer and it can be confusing at your level.

链接列表最初应为空,如下所示:

Linked list should initially be empty, as follows:

for(int i = 0; i < COMPUTER_LABS; i++)
    computerLabs[i] = NULL;

删除节点时,如果节点是否为head,则需要不同的条件.这是一个简单的版本:

When removing a node, you need different conditions if the node is head or not. Here is a simple version:

#include <iostream>
using namespace std;

struct User
{
    int id;
    User *next;
};

const int COMPUTER_LABS = 4; 

void login(User *labs[], int lab, int id)
{
    User *p = new User;
    p->id = id;
    p->next = labs[lab];
    labs[lab] = p;
}

void logoff(User *labs[], int lab, int id)
{
    User *prev = nullptr;
    User* walk = labs[lab];
    while(walk)
    {
        if(walk->id == id)
        {
            User *next = walk->next;
            delete walk;
            if(prev)
                prev->next = next;
            else
                labs[lab] = next;
            break;
        }
        prev = walk;

        walk = walk->next;
    }

}

void showLabs(User *labs[])
{
    for(int i = 0; i < COMPUTER_LABS; i++)
    {
        cout << "Lab number " << i << ": ";
        User* walk = labs[i];
        while(walk)
        {
            cout << "id: " << walk->id << ", ";
            walk = walk->next;
        }
        cout << endl;
    }
}

int main()
{
    User *labs[COMPUTER_LABS];
    for(int i = 0; i < COMPUTER_LABS; i++)
    {
        //linked list is initially empty, this is "head"
        labs[i] = NULL;
    }

    login(labs, 0, 100);
    login(labs, 0, 101);
    login(labs, 1, 200);
    login(labs, 1, 201);
    showLabs(labs);
    logoff(labs, 0, 100);
    showLabs(labs);

    return 0;
}

这篇关于链接列表作业数组-运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆