在C++中使用链表模拟客户线

Simulating a customer line using linked list in C++

我正在尝试模拟显示客户编号、等待时间等的客户线路。我有三个用户输入:最大模拟时间、客户输入之间的最大间隔和最大服务时间。

最终,我想生成如下所示的输出:

Line simulation output

到目前为止,这是我的代码,但我只能生成会议记录,而不能生成其余的输出。

#include "stdafx.h"
#include <iostream>
using namespace std;


class Node {
public:
    Node() {};
    Node(int, int, int) {};
    //Node(int,int, int){}
    void SetCustEntryMinute(int custNum, int entryMinute, int servTimeRem) {

        entryMin = entryMinute;
        custNo = custNum;
        serviceTimeRemain = servTimeRem;
        next = NULL;
    }
    void SetNext(Node* aNext) { next = aNext; }
    int CustEntryMinute() { return entryMin; }
    int CustNum() { return custNo; }
    int ServTmRm() { return serviceTimeRemain; }
    Node* Next() { return next; }

    int custNo;
    int entryMin;
    int serviceTimeRemain;
    Node* next;
};


//List class
class List {
public:
    List() { /*head = NULL; tail = NULL; */};
    //void Print();
    void enqueue(int custNum, int entryMinute, int servTimeRem);
    void dequeue();
    Node *getHead() { return head; };
    Node* head;
    Node *tail;
};



/* Append a node to the linked list*/
void List::enqueue(int custNum, int entryMinute, int servTimeRem)
{

    // Create a new node
    Node* newNode = new Node(custNum, entryMinute, servTimeRem);

    // Create a temp pointer
    if (tail)
    {
        tail->next = newNode;
    }
    else
    {
        head = newNode;
    }
    tail = newNode;

}



/* Serve a node from the list*/
void List::dequeue() {

    // Create a temp pointer
    Node *tmp = head;
    //head = head->Next();

    // No nodes
    if (tmp == NULL)
    {
        //nothing to delete
        return;
    }
    if (head == tail)//only one node
    {
        head = NULL;
        tail = NULL;
    }
    else
    {
        head = head->next;
    }
    delete tmp;
}



int main()
{
    List list;
    int serviceTimeMax;
    int maxInterval;
    int timeCount = 0;
    int timeMax;
    int entryMinute = 0;

    cout << "Enter max time: ";
    cin >> timeMax;

    cout << "Enter max interval between two services: ";
    cin >> maxInterval;

    cout << "Enter max service time: ";
    cin >> serviceTimeMax;

    int custNum = 1;
    Node* personAhead = NULL;
    cout << "Minute \tCustomer\t Entry\t Service Time\t Wait time\t Minutes remaining\n";
    cout << "Number \tNumber  \tMinute\t Remaining\t Remaining\t until service is completed\n\n";
    int nextJobTime = 1;
    for (int i = 1; i < timeMax + 1; i++) {
        if (i == nextJobTime)
        {
            list.enqueue(custNum, i, rand() % serviceTimeMax);
                nextJobTime = i + rand() % maxInterval;
        }
        Node *head = new Node;
        while (head != NULL) {
            if (head->serviceTimeRemain == 0)
            {
                list.dequeue();
                head = list.getHead();
            }

            Node *p = head;
            int wait_time = 0;
            cout << i << "\n";
            while (p != NULL)
            {
                wait_time += p->serviceTimeRemain;
                cout << "\t" << p->custNo << "\t" << p->entryMin << "\t" << wait_time << "\t" << wait_time + p->serviceTimeRemain << "\n";
                p->serviceTimeRemain--;
            }
        }


    }


    system("pause");
    return 0;
}

此时,它只生成用户输入的时间。

您陷入了无限循环

while (p != NULL)
{
    wait_time += p->serviceTimeRemain;
    cout << "\t" << p->custNo << "\t" << p->entryMin << "\t" << wait_time << "\t" << wait_time + p->serviceTimeRemain << "\n";
    p->serviceTimeRemain--;
}

您绝不会更改 p,这意味着您永远不会退出 while 循环。