簡體   English   中英

實現與鏈表堆棧的交集。

[英]Implementing an intersection with linked-list stacks.

嗨,我有一項家庭作業,需要在兩個小時的時間內實現兩條單車道街道的交叉點。 我需要調整階段,以使每個隊列中的車輛少於5輛,也可以接受9輛。

一切都可行,除了在實現定相的方式上有毛病之外,我似乎無法解決這個問題。 我能得到的絕對最佳結果是一個隊列為0或1,另一個隊列為40+。 我似乎都無法將兩者都控制在9歲以下。我已經將問題歸結為我的階段檢查,但想不出解決辦法。 我知道我想稍微偏愛Q1,因為汽車到達的速度比Q2快一點。

在此先感謝您的幫助。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

struct Node {
    int data;
    Node *next;
};

class Queue {
private:                         
    Node *listpointer;
public:                          
    Queue();
    ~Queue();
    void push(int newthing);
    void pop();
    int top();
    bool isEmpty();
    int queueCount();
    void Queue::popTwo();
    bool Queue::twoOrMore();
};

Queue::Queue() {
//constructor
    listpointer = NULL;
}

Queue::~Queue() {
//destructor

}

void Queue::push(int newthing) {
//place the new thing on top of the Queue
    Node *temp;
    temp = new Node;             
    temp->data = newthing;
    temp->next = listpointer;
    listpointer = temp;
}

void Queue::pop() {
//remove top item from the Queue
    Node *p;
    p = listpointer;
    if (listpointer != NULL) {
        listpointer = listpointer->next;
        delete p;  
  }
}

int Queue::top() {
//return the value of the top item
    return listpointer->data;
}

bool Queue::isEmpty() {
//returns true if the Queue is empty
    if (listpointer == NULL) {
        return true;
    }
    return false;
}

int Queue::queueCount() {
    Node *temp;
    int count = 0;
    temp = listpointer;
    while (temp != NULL) {
        ++count;
        temp = temp->next;
    }
    return count;
    delete temp;
}

void Queue::popTwo() {
// remove the 2 top items from the stack
    Node *p;
    p = listpointer;
    if (listpointer != NULL) {
        listpointer = listpointer->next;
        delete p;
    }
    p = listpointer;
    if (listpointer != NULL) {
        listpointer = listpointer->next;
        delete p;                
    }
}

bool Queue::twoOrMore() {
// return true if the stack has at least two items
    if(listpointer==NULL || listpointer->next==NULL) return false;
    else return true;
}

//implement/copy your queue structure and functions above
//then, declare two instances:
//Queue Q1, Q2;
//if you want, make a separate function to change the 
//signals between the queues (either green or red)
//When the signal changes, one queue only is allowed to delete elements

Queue Q1, Q2;

int Q1phase = 30; //initial attempt
int Q2phase = 60; //initial attempt
const int Q1arrive = 18; //fixed 
const int Q2arrive = 22; //fixed
const int leave_rate = 10; //fixed, one car leaves either queue every 10 seconds

int car_id=0;
int clock=0;
bool Q1_green, Q2_green; //indicates which queue is opened, only one at a time

int main(int argc, char **argv) {
    //if(argc!=3) {printf("needs: Q1phase Q2phase\n"); exit(0); }
    //Q1phase=atoi(argv[1]);
    //Q2phase=atoi(argv[2]);
    if(Q1phase < 30 || Q2phase < 30) {printf("Minimum time for each queue to be closed is 30 seconds\n"); exit(0);}
    clock = 0;
    car_id = 0;
    Q1_green = true;
    Q2_green = false;
    int length_Q1, length_Q2;
    length_Q1 = 0;
    length_Q2 = 0;

    while (clock < 7200) {
        clock++;
        if (clock % Q1arrive == 0) {
            car_id++;
            //car_id join Q1
            Q1.push(car_id);
            length_Q1 = Q1.queueCount();
        }
        if (clock % Q2arrive == 0) {
            car_id++;
            //or car_id join Q2
            Q2.push(car_id);
            length_Q2 = Q2.queueCount();
        }

        if ((clock % Q1phase == 0) || (clock % Q2phase == 0)) {
            if (Q1_green == true) {
                Q1_green = false;
                Q2_green = true;
            } else {
                Q1_green = true;
                Q2_green = false;
            }
        }

        if (clock % leave_rate == 0) {
            if (Q1_green == true) {
                Q1.pop();
                length_Q1 = Q1.queueCount();
            }

            if (Q2_green == true) {
                Q2.pop();
                length_Q2 = Q2.queueCount();
            }
        }

    //ChangeSignal();//every second, check if it is time to change signals (phasing is important!)
    //After the signal change:
    //verify which queue is opened
    //either Q1 or Q2 will have the chance to delete one element (Q.Leave())
    //
    printf("at time %d:\nthe current length of Q1 is %d\n",clock,length_Q1);
    printf("the current length of Q2 is %d\n", length_Q2);
    //at the end of the simulation, both queues should have few cars
  }
}

您的總到達率超過請假率,因此汽車必須積壓。

總到達速度為1/22 + 1/18 =~ 0.1010汽車/秒。 這超過了每秒0.1輛車的離開率。

因為Q2phaseQ2phase的倍數, Q1phase光線每30秒更改一次( Q1phase )。 因此,基本上隊列具有相等的占空比。

汽車從每個隊列中流失的總速率為一半:一個隊列中的0.05,其他隊列中的0.05(1/20)。

請注意,1/20的離開率小於1/18。 因此,1/18到達的隊列將積壓。 1/20的離開率大於1/22,因此1/22到達率的隊列不會積壓。

這種細微的差別並不是真正的細微! 到達率超過休假率或不超過休假率之間存在巨大的差異。


哦,這是如何計算積壓隊列中的汽車:

到達率:1/18。 離開率:1/20(一半為1/10)總時間:7200秒:

7200 *(1/18)-7200 *(1/20)== ????

:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM