1.编写及实现顺序栈的定义及基本操作函数,完成教材上机实验题1。
2.编写及实现环形队列的定义及基本操作函数完成教材上机实验题3。
3.利用顺序栈及环形队列编写停车场管理程序。
具体代码如下:
1.
#include<iostream>
using namespace std;
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int top; //栈指针
} SqStack; //声明顺序栈类型
void InitStack(SqStack *&s) //初始化顺序栈
{
s = (SqStack *)malloc(sizeof(SqStack));
s->top = -1;
}
void DestroyStack(SqStack *&s) //销毁顺序栈
{
free(s);
}
bool StackEmpty(SqStack *s) //判断栈空否
{
return (s->top == -1);
}
bool Push(SqStack *&s, ElemType e) //进栈
{
if (s->top == MaxSize - 1) //栈满的情况,即栈上溢出
return false;
s->top++;
s->data[s->top] = e;
return true;
}
bool Pop(SqStack *&s, ElemType &e) //出栈
{
if (s->top == -1) //栈为空的情况,即栈下溢出
return false;
e = s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s, ElemType &e) //取栈顶元素
{
if (s->top == -1) //栈为空的情况,即栈下溢出
return false;
e = s->data[s->top];
return true;
}
int main()
{
ElemType e;
SqStack *s;
InitStack(s);//初始化栈s
printf(" 成功初始化栈\n");
if(StackEmpty(s))
printf("栈为空 ");
else
printf("栈为非空 ");
printf(" 依次进栈元素1,2,3,4,5\n");
Push(s, '1');
Push(s, '2');
Push(s, '3');
Push(s, '4');
Push(s, '5');
if(StackEmpty(s))
printf("栈为空 ");
else
printf("栈为非空 ");
printf(" 出栈序列:");
while (!StackEmpty(s))
{
Pop(s, e);
printf("%c ", e);
}
printf("\n");
printf(" 成功销毁栈\n");
DestroyStack(s);
return 0;
}
2.
#include<iostream>
using namespace std;
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int front,rear; //队首和队尾指针
} SqQueue;
void InitQueue(SqQueue *&q) //初始化队列q
{ q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q) //销毁队列q
{
free(q);
}
bool QueueEmpty(SqQueue *q) //判断队q是否空
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e) //进队
{ if ((q->rear+1)%MaxSize==q->front) //队满溢出
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e) //出队
{ if (q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
int main()
{
ElemType m;
SqQueue *q;
printf(" 成功初始化队列q\n");
InitQueue(q);
printf(" 依次进队列元素1,2,3\n");
enQueue(q,'1');
enQueue(q,'2');
enQueue(q,'3');
if(QueueEmpty(q))
printf(" 队为空\n");
else
printf(" 队为非空\n");
if (deQueue(q,m)==0)
printf("队空,不能出队\n");
else
printf(" 出队一个元素%c\n",m);
printf(" 出队列:");
while (!QueueEmpty(q))
{ deQueue(q,m);
printf("%c ",m);
}
printf("\n");
if(QueueEmpty(q))
printf(" 队为空\n");
else
printf(" 队为非空\n");
printf(" 销毁队列\n");
DestroyQueue(q);
return 0;
}
3.
#include <stdio.h>
#include <malloc.h>
#define N 3 //停车场内最多的停车数
#define M 4 //候车场内最多的停车数
#define Price 2 //每单位停车费用
typedef struct
{
int CarNo[N]; //车牌号
int CarTime[N]; //进场时间
int top; //栈指针
} SqStack; //声明顺序栈类型
typedef struct
{
int CarNo[M]; //车牌号
int front,rear; //队首和队尾指针
} SqQueue; //声明环形队列类型
//以下为栈的运算算法
void InitStack(SqStack *&s) //初始化栈
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
bool StackEmpty(SqStack *s) //判断栈空
{
return(s->top==-1);
}
bool StackFull(SqStack *s) //判断栈满
{
return(s->top==N-1);
}
bool Push(SqStack *&s,int e1,int e2) //进栈
{
if (s->top==N-1)
return false;
s->top++;
s->CarNo[s->top]=e1;
s->CarTime[s->top]=e2;
return true;
}
bool Pop(SqStack *&s,int &no) //出栈
{
if (s->top==-1)
return false;
no=s->CarNo[s->top];
//e2=s->CarTime[s->top];
s->top--;
return true;
}
void DispStack(SqStack *s) //显示栈中元素
{
for (int i=s->top;i>=0;i--)
printf("车牌号为:%d ",s->CarNo[i]);
printf("\n");
}
//以下为队列的运算算法
void InitQueue(SqQueue *&q) //初始化队
{
q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
bool QueueEmpty(SqQueue *q) //判断队空
{
return(q->front==q->rear);
}
bool QueueFull(SqQueue *q) //判断队满
{
return ((q->rear+1)%M==q->front);
}
bool enQueue(SqQueue *&q,int e) //进队
{
if ((q->rear+1)%M==q->front) //队满
return false;
q->rear=(q->rear+1)%M;
q->CarNo[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,int &e) //出队
{
if (q->front==q->rear) //队空的情况
return false;
q->front=(q->front+1)%M;
e=q->CarNo[q->front];
return true;
}
void DispQueue(SqQueue *q) //显示队中元素
{
int i=(q->front+1)%M;
printf("车牌号为:%d ",q->CarNo[i]);
while ((q->rear-i+M)%M>0)
{
i=(i+1)%M;
printf("%d ",q->CarNo[i]);
}
printf("\n");
}
int main()
{
int comm,i,j;
int no,e1,time,e2;
SqStack *St,*St1;
SqQueue *Qu;
InitStack(St);
InitStack(St1);
InitQueue(Qu);
SqStack SS;
do
{
printf(">输入指令(1:到达 2:离开 3:停车场情况 4:候车场情况 0:退出):");
scanf("%d",&comm);
switch(comm)
{
case 1: //汽车到达*/
if(StackFull(St)&&QueueFull(Qu))
{
printf(" 停车场和候车场中无车位\n");
}
if(!StackFull(St)&&!QueueFull(Qu))
{
printf(" 请输入车牌号和进车时间\n");
scanf("%d %d",&no,&e2);
Push(St,no,e2);
}
else {
printf("停车场已满,即将进入候车场");
printf(" 请输入车牌号\n");
scanf("%d",&no);
enQueue(Qu,no);
printf("输入完成\n");
}
break;
case 2: //汽车离开
printf(" 请输入车牌号和离开时间\n");
scanf("%d %d",&no,&e2);
if(no==St->CarNo[St->top]){
Pop(St,no);}
e1=St->CarTime[St->top];
//printf("%d",e1);
time=e1*Price;
printf("please pay for %d money\n",time);
break;
case 3: //显示停车场情况
if (!StackEmpty(St))
{
printf(" 停车场中的车辆:"); //输出停车场中的车辆
DispStack(St);
}
else
printf(" 停车场中无车辆\n");
break;
case 4: //显示候车场情况
if (!QueueEmpty(Qu))
{
printf(" 候车场中的车辆:"); //输出候车场中的车辆
DispQueue(Qu);
}
else
printf(" 候车场中无车辆\n");
break;
case 0: //结束
if (!StackEmpty(St))
{
printf(" 停车场中的车辆:"); //输出停车场中的车辆
DispStack(St);
}
if (!QueueEmpty(Qu))
{
printf(" 候车场中的车辆:"); //输出候车场中的车辆
DispQueue(Qu);
}
break;
default: //其他情况
printf(" 输入的命令错误\n");
}
}while(comm!=0);
return 1;
}