/* name: 停车厂管理算法————数据结构 written by: 1jjk email:lingjiujianke@gmail.com */ #include<stdio.h> #include<malloc.h> #include<stdlib.h> /*include header file*/ #define stacksize 10 #define sqstack_maxsize 30 /*define*/ typedef struct sqstack { int data[stacksize]; int top; }sqstacktp; typedef struct linked_queue { int data; struct linked_queue * next; }lqueuetp; typedef struct queueptr { lqueuetp *front, *rear; }queuetp; /*typedef new struct*/ /*init a stack*/ int initstack(sqstacktp *sq) { sq->top=0; return 1; } /*push in stack*/ int push(sqstacktp *sq,int x) { if(sq->top == sqstack_maxsize) { return 0; } else { sq->top++; sq->data[sq->top]=x; return 1; } } /*pop from stack*/ int pop(sqstacktp *sq, int *x) { if(sq->top==0) { printf("stack out\n"); return 0; } else { *x=sq->data[sq->top]; sq->top--; return 1; } } /*is the stack empty?*/ int emptystack(sqstacktp sq) { if(sq.top==0) { return 1; } else { return 0; } } /*is it the top of the stack?*/ int gettop(sqstacktp *sq,int *x) { if(sq->top==0) { return 0; } else { *x=sq->data[sq->top]; return 1; } } /*is it the init queue*/ int initqueue(queuetp *lq) { lqueuetp *p; p=(lqueuetp *)malloc(sizeof(lqueuetp)); lq->front=p; lq->rear=p; (lq->front)->next=NULL; return 1; } /*in queue*/ void enqueue(queuetp *lq, int x) { lqueuetp *p; p=(lqueuetp *)malloc(sizeof(lqueuetp)); p->data=x; p->next=NULL; (lq->rear)->next=p; lq->rear=p; } /*out queue*/ int outqueue(queuetp *lq,int *x) { lqueuetp *s; if(lq->front==lq->rear) { printf("queue out\n"); return 0; } else { s=(lq->front)->next; *x=s->data; (lq->front)->next=s->next; if(s->next==NULL) { lq->rear=lq->front; } free(s); return 1; } } /*is the queue empty?*/ int emptyqueue(queuetp lq) { if(lq.rear==lq.front) { return 1; } else { return 0; } } /*get the head of the queue*/ int gethead(queuetp lq,int *x) { lqueuetp *p; if(lq.rear==lq.front) { return 0; } else { p=lq.front->next; *x=p->data; return 1; } } /*the main function*/ int main() { sqstacktp ps,ts; queuetp lq; int out,number,temp=0; char ch; initstack(&ps); initstack(&ts); initqueue(&lq); /*init the stack and the queue*/ out=0; /*get a char,if the char is q,or Q then out,if not then get a number*/ scanf("%c",&ch); if(ch=='Q'||ch=='q') return 0; scanf("%d",&number); /*loop from here,if the number max than 0 then loop */ while(number>0) { switch(ch) { /*if the char is a or A then do it:*/ case 'A': case 'a': if(ps.top==sqstack_maxsize-1) { enqueue(&lq,number); printf("it full in %d\n", number); } else { push(&ps,number); printf("the num %d in \n", number); } break; /*if the char is Dor d then do it:*/ case 'D': case 'd': while(!emptystack(ps)) { pop(&ps,&temp); if(temp!=number) { push(&ts,temp); } else { printf("the num %d out\n",number); out=1; break; } } if(out==0) { printf("the num %d is not in\n",number); } while(!emptystack(ts)) { pop(&ts,&temp); push(&ps,temp); } if(out&&!emptyqueue(lq)) { outqueue(&lq,&temp); printf("the num %d is car stop room\n",temp); push(&ps,temp); } out=0; break; } /*get the char and the number again*/ scanf("%c",&ch); if(ch=='Q'||ch=='q') return 0; scanf("%d",&number); } return 0; }
|