萌新第一次发blog,简单停车场出入程序。
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node* next;
}Node, * LinkStack;
typedef struct
{
Node * front;
Node * rear;
int length;
}LinkQueue;
LinkStack CreatStack()//初始化栈;
{
LinkStack L = (Node*)malloc(sizeof(Node));
L->next = NULL;
return L;
}
void Push(LinkStack L, ElemType x)//进栈
{
Node* p = (Node*)malloc(sizeof(Node));
p->data=x;
p->next = L->next;
L->next = p;
}
ElemType Pop(LinkStack L)//出栈
{
ElemType e;
if (L->next == NULL)
{
printf("栈为空!\n");
}
else
{
e = L->next->data;
L->next = L->next->next;
return e;
}
}
void DisplayS(LinkStack L)//打印栈
{
Node* p = L->next;
while (p != NULL)
{
printf("%d\n", p->data);
p = p->next;
}
printf("\n");
}
LinkQueue CreateQueue()//初始化队
{
LinkQueue L;
L.front = (Node*)malloc(sizeof(Node));
L.front->next = NULL;
L.rear = L.front;
return L;
}
void EnQueue(LinkQueue *L,ElemType x)//入队
{
Node* p = (Node*)malloc(sizeof(Node));
p->data =x;
p->next = NULL;
L->rear->next = p;
L->rear = p;
}
ElemType DeQueue(LinkQueue* L)//出队
{
ElemType e;
if (L->length == 0)
{
printf("队为空!\n");
}
else
{
if (L->front->next == L->rear)
{
e = L->rear->data;
L->front->next = NULL;
L->rear = L->front;
L->length--;
return e;
}
else
{
e = L->front->next->data;
L->front->next = L->front->next->next;
L->length--;
return e;
}
}
}
void DisplayQ(LinkQueue L)//打印队
{
Node * q = L.front->next;
while (q != NULL)
{
printf("%d\n", q->data);
q = q->next;
}
printf("\n");
}
int main()
{
int y,q;
int i=0;
int x;
LinkStack Lt=CreatStack();
LinkQueue Lh = CreateQueue();
LinkStack Ld = CreatStack();
printf(" @@@@@@@@@@@@@@@ \n");
printf("************************欢迎来到成都中医药大学停车场************************\n");
printf(" /***车位容量为5,经费不足!!!***/\n");
printf("输入数字选择服务:\n");
printf("1)开始停车 2)停车结束 3)显示当前停车场状况 4)显示待停区 5)显示候车区 6)选择出库车号\n");
int z=0;
while (1)
{
scanf("%d", &z);
switch (z)
{
case 1:
while (i < 5)
{
printf("请输入车号或输入7离开:");
scanf("%d", &x);
if (x == 7)
break;
Push(Lt, x);
printf("1)开始停车 2)停车结束 3)显示当前停车场状况 4)显示待停区 5)显示候车区 6)选择出库车号\n");
i++;
}
if (i == 5)
{
printf("车位已满!后续车辆等待中\n");
printf("车辆候车区查等待(输入0以结束进车,不要一来就输入0哦)\n");
}
while (x != 0&&x!=7)
{
scanf("%d", &x);
if (x == 0)
break;
EnQueue(&Lh, x);
}
printf("1)开始停车 2)停车结束 3)显示当前停车场状况 4)显示待停区 5)显示候车区 6)选择出库车号\n");
break;
case 2:
printf("欢迎下次停车!!!\n");
break;
case 3:
printf("停车场的状况:\n");
DisplayS(Lt);
printf("1)开始停车 2)停车结束 3)显示当前停车场状况 4)显示待停区 5)显示候车区 6)选择出库车号\n");
break;
case 4:
case 5:
printf("候车区状况:\n");
DisplayQ(Lh);
printf("1)开始停车 2)停车结束 3)显示当前停车场状况 4)显示待停区 5)显示候车区 6)选择出库车号\n");
break;
case 6:
printf("输入需出场车号:\n");
scanf("%d", &x);
while (1)
{
y=Pop(Lt);
Push(Ld,y);
if (y == x)
break;
}
printf("停车场车辆分布:\n");
DisplayS(Lt);
printf("待停区车辆分布:\n");
DisplayS(Ld);
default:
printf("请您按照提示输入\n");
printf("1)开始停车 2)停车结束 3)显示当前停车场状况 4)显示待停区 5)显示候车区 6)选择出库车号\n");
}
}
return 0;
}