您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验之栈与队列十一:refresh的停车场

2017-10-15 22:44 363 查看
/*栈对应停车场的车辆,队列对应便道的车辆*/
#include <iostream>
#include <cstring>
#include <malloc.h>
#define MAX_SIZE 50000

using namespace std;

//栈
typedef struct SNode
{
char Num[MAX_SIZE][15];//采用二维字符数组存储车牌
int top, StackSize;
}*Stack;

//队列
typedef struct QNode
{
char Num[MAX_SIZE][15];
int Front;
int Rear;
int Size;//当前队列中元素数量
int QueueSize;//队列大小
}*Queue;

bool IsEmptyS(Stack S)
{
return S->top == -1;
}

bool IsFullS(Stack S)
{
return S->top == S->StackSize - 1;
}

void MakeEmptyS(Stack S)
{
S->top = -1;
}

Stack CreatS(int N)
{
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
S->StackSize = N;
MakeEmptyS(S);
return S;
}

void Push(Stack S, char num[])
{
if(!IsFullS(S))
{
strcpy(S->Num[++S->top], num);
}
else
return ;
}

bool Pop(Stack S)
{
if(!IsEmptyS(S))
{
S->Num[S->top--];
return true;
}
else
return false;
}

bool IsEmptyQ(Queue Q)
{
return Q->Size  == 0;
}

bool IsFullQ(Queue Q)
{
return Q->Rear == Q->QueueSize - 1;
}

void MakeEmptyQ(Queue Q)
{
Q->Front = 0;
Q->Rear = -1;
Q->Size = 0;
}

Queue CreatQ()
{
Queue Q;
Q = (Queue)malloc(sizeof(struct QNode));
Q->QueueSize = MAX_SIZE;
MakeEmptyQ(Q);
return Q;
}

void EnQueue(Queue Q, char num[])
{
if(!IsFullQ(Q))
{
strcpy(Q->Num[++Q->Rear], num);
Q->Size++;
}
else
return ;
}

bool DeQueue(Queue Q)
{
if(!IsEmptyQ(Q))
{
Q->Front++;
Q->Size--;
return true;
}
else
return false;
}

int main()
{
int N, M;
while(cin>>N>>M)
{
Stack S;
Queue Q;
S = CreatS(N);
Q = CreatQ();
bool flag = true;//是否输出Error
while(M--)
{
char str[4];
cin>>str;
if(strcmp(str, "Add") == 0)
{
char num[15];
cin>>num;
if(!IsFullS(S))
{
Push(S, num);
}
else
{
EnQueue(Q, num);
}
}
else if(strcmp(str, "Del") == 0)
{
if(Pop(S))
{
if(!IsEmptyQ(Q))
{
Push(S, Q->Num[Q->Front]);
DeQueue(Q);
}
}
else
flag = false;
}
else if(strcmp(str, "Out") == 0)
{
if(IsEmptyQ(Q))
flag = false;
else
DeQueue(Q);
}
}
if(!flag)
cout<<"Error"<<endl;
else
for(int i=S->top; i>=0; i--)
{
cout<<S->Num[i];
if(i >= 0)
cout<<"\n";
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构