数据结构-QS-应用-停车场

来源:互联网 发布:西门子plc编程软件下载 编辑:程序博客网 时间:2024/05/15 11:26
//停车场很窄,入时一个一个入,出时需要后进的车进入临时停车处,如果停车场满,再停车需要停到便道//Seqstack.hconst int stacksize=40;typedef struct SQstack{int data[stacksize];int top;}SeqStk;//顺序栈运算int InitStack(SeqStk *SQ){SQ->top=0;return 1;}int EmptyStack(SeqStk SQ){if(SQ.top==0)return 1;else return 0;}int Push(SeqStk *SQ,int x){if (SQ->top==stacksize-1)return 0;else{SQ->top++;SQ->data[SQ->top]=x;return 1;}}int Pop(SeqStk *SQ){if EmptyStack(SQ){error("underflow!");return 0;}else{SQ->top--;return 1;}}int GetTop (SeqStk *SQ){if (EmptyStack(SQ))return -1;elsereturn SQ->data[SQ->top];}//Lkqueue.htypedef struct LinkQueueNode{int data;struct LinkQueueNode *next;}LkQueNode;typedef struct LkQueue{LkQueNode *front,*rear;}LkQue;//基本运算void InitQueue(LkQue *LQ){LkQueNode *p;p=(LkQueNode *)malloc(sizeof(LkQueNode))LQ->front=p;LQ->rear=p;(LQ->front)->next=NULL;}int EmptyQueue(LkQue LQ){if (LQ.rear==LQ.front)return 1;elsereturn 0;}void EnQueue(LkQue *LQ;int x){LkQueNode *p;p=(LkQueNode *)malloc(sizeof(LkQueNode));p->data=x;p->next=NULL;(LQ->rear)->next=p;LQ->rear=p;}int OutQueue(LkQue *LQ){LkQueNode *s;if (EmptyQueue(LQ)){error("Queue empty");return 0;}else{s=(LQ->front)->next;(LQ->front)->next=s->next;if(s->next==NULL)LQ->rear=LQ->front;free(s);return 1;}}int GetHead (LkQue LQ){LkQueNode *p;if (EmptyQueue(LQ))return -1;else{p=LQ.front->next;return p->data;}}//A为进停车场,D为开出停车场,当车号为0不管读什么命令程序结束//程序#include <stdio.h>#include <alloc.h>#include "Seqstack.h"#include "Lkqueue.h"void main(){SeqStk ps,ts;LkQue LQ;int out,number,temp;char ch;InitStack(&ps);InitStack(&ts);InitQueue(&LQ);out=0;//标记,找不到车号为0,找到为1printf("Enter command:\n");scanf("%c",&ch);printf("Enter number of car");scanf("%d",&number);while (number>0){switch(ch){case 'A':if(ps.top==stacksize-1){EnQueue(&LQ,number);printf("停车场满!第%d号车入便道\n",number);}else{Push (&ps,number);printf("第%d号车进入停车场\n",number);}break;case 'D'while (!EmptyStack(ps)){temp=GetTop(ps);Pop(&ps);if (temp!=number)Push(&ts,temp);else{printf("第%d号车离开停车场!\n",number);out=1;break;}}while (!EmptyStack(ts)){temp=GetTop(ts);pop(&ts);Push(&ps,temp);}if(out&&!EmptyQueue(LQ)){temp=GetHead(&LQ);OutQueue(&LQ);printf("第%d号车便道->停车场!\n");Push(&ps,temp);}out=0;break;}scanf("%c",&ch);scanf("%d",&number);}}


 

0 0