C++ 使用链表堆栈实现交集。

C++ 使用链表堆栈实现交集。,c++,stack,linked-list,C++,Stack,Linked List,嗨,我有一个家庭作业,我需要在两个小时内完成两条单行道的交叉口。我需要调整相位,使每个队列的车辆数量理想地少于5辆,9辆也是可以接受的 这一切都是可行的,除了我的分阶段实施方式有点不对劲,我似乎无法解决这个问题。我能得到的绝对最佳结果是一个队列为0或1,另一个队列为40+。我似乎不能让他们两个都在9岁以下。我已经把问题归结到了我的阶段检查,但想不出一个办法来解决它。我知道我想稍微偏爱Q1,因为汽车到达的速度比Q2快一点 提前感谢您的帮助 #include <stdio.h> #inc

嗨,我有一个家庭作业,我需要在两个小时内完成两条单行道的交叉口。我需要调整相位,使每个队列的车辆数量理想地少于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
  }
}
#包括
#包括
#包括
使用名称空间std;
结构节点{
int数据;
节点*下一步;
};
类队列{
私人:
节点*列表指针;
公众:
队列();
~Queue();
空推(内推);
void pop();
int-top();
bool是空的();
int queueCount();
void Queue::popTwo();
bool队列::两个或更多();
};
队列::队列(){
//建造师
listpointer=NULL;
}
队列::~Queue(){
//析构函数
}
无效队列::推送(int newthing){
//将新事物放在队列的顶部
节点*温度;
temp=新节点;
温度->数据=newthing;
temp->next=listpointer;
listpointer=temp;
}
无效队列::pop(){
//从队列中删除顶部项目
节点*p;
p=列表指针;
if(listpointer!=NULL){
listpointer=listpointer->next;
删除p;
}
}
int队列::top(){
//返回顶部项目的值
返回listpointer->data;
}
bool队列::isEmpty(){
//如果队列为空,则返回true
if(listpointer==NULL){
返回true;
}
返回false;
}
int Queue::queueCount(){
节点*温度;
整数计数=0;
temp=列表指针;
while(temp!=NULL){
++计数;
温度=温度->下一步;
}
返回计数;
删除临时文件;
}
void队列::popTwo(){
//从堆栈中移除2个顶部项目
节点*p;
p=列表指针;
if(listpointer!=NULL){
listpointer=listpointer->next;
删除p;
}
p=列表指针;
if(listpointer!=NULL){
listpointer=listpointer->next;
删除p;
}
}
bool队列::两个或更多(){
//如果堆栈至少有两项,则返回true
if(listpointer==NULL | | listpointer->next==NULL)返回false;
否则返回true;
}
//实现/复制上述队列结构和功能
//然后,声明两个实例:
//队列Q1、Q2;
//如果需要,可以使用单独的函数来更改
//队列之间的信号(绿色或红色)
//当信号改变时,只允许一个队列删除元素
队列Q1、Q2;
int Q1相位=30//初步尝试
相位=60//初步尝试
常数=18//固定的
常数int=22//固定的
常数int leave_率=10//修正了,每10秒有一辆车离开队列
int car_id=0;
int时钟=0;
布尔Q1_绿色,Q2_绿色//指示打开了哪个队列,一次只能打开一个队列
int main(int argc,字符**argv){
//如果(argc!=3){printf(“需要:Q1阶段Q2阶段\n”);退出(0);}
//Q1phase=atoi(argv[1]);
//Q2phase=atoi(argv[2]);
如果(Q1phase<30 | | Q2phase<30){printf(“每个队列关闭的最短时间为30秒”);退出(0);}
时钟=0;
车辆识别号=0;
Q1_绿色=真;
Q2_绿色=假;
整数长度_Q1,长度_Q2;
长度_Q1=0;
长度_Q2=0;
同时(时钟<7200){
时钟++;
如果(时钟%Q1到达==0){
汽车id++;
//汽车id加入Q1
Q1.推送(轿厢id);
长度_Q1=Q1.queueCount();
}
如果(时钟%Q2到达==0){
汽车id++;
//或汽车id加入Q2
Q2.推送(车号);
长度_Q2=Q2.queueCount();
}
如果((时钟%Q1相位==0)| |(时钟%Q2相位==0)){
如果(Q1_绿色==真){
Q1_绿色=假;
Q2_绿色=真;
}否则{
Q1_绿色=真;
Q2_绿色=假;
}
}
如果(时钟百分比离开率==0){
如果(Q1_绿色==真){
Q1.pop();
长度_Q1=Q1.queueCount();
}
如果(Q2_绿色==真){
Q2.pop();
长度_Q2=Q2.queueCount();
}
}
//ChangeSignal();//每隔一秒钟,检查是否是更改信号的时间(相位调整很重要!)
//信号改变后:
//验证打开了哪个队列
//Q1或Q2都有机会删除一个元素(Q.Leave())
//
printf(“在时间%d:\nQ1的当前长度为%d\n”,时钟,长度为\u Q1);
printf(“Q2的当前长度为%d\n”,长度为_Q2);
//在模拟结束时,两个队列都应该只有很少的车辆
}
}

您的总到达率超过了休假率,因此车辆将不得不积压

总到达率为
1/22+1/18=~0.1010
cars/sec。这超过了每秒
0.1辆
车的离开率

该灯每30秒(
Q1phase
)改变一次,因为
Q2phase
Q1phase
的倍数。因此,队列基本上具有相等的占空比

车辆从每个队列中的排放量为总排放量的一半:一个队列的排放量为0.05,另一个队列的排放量为0.05(1/20)

请注意,1/20的休假率小于1/18。因此,到达时间为1/18的队列将积压。1/20的休假率大于1/22,因此到达率为1/22的队列不会积压

这个细微的差别其实并不微小!到达率超过休假率与不超过休假率之间存在巨大差异


哦,下面是如何计算积压队列中的车辆:

阿里夫