使用C ++中的链接列表模拟客户行

时间:2017-11-24 19:02:47

标签: c++ linked-list queue simulation

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

最终,我想生成一个看起来像这样的输出:

Line simulation output

这是我的代码sofar,但我只能生成分钟而不是输出的其余部分。

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

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

1 个答案:

答案 0 :(得分:1)

你陷入无限循环

SHOW PROCESSLIST

你决不改变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--; } ,这意味着你永远不会退出while循环。