第七周 项目3
来源:互联网 发布:360手机文件夹加密软件 编辑:程序博客网 时间:2024/05/12 14:14
- #include <stdio.h>
- #include <malloc.h>
- #include "sqqueue.h"
-
- int main()
- {
- ElemType a,x;
- SqQueue *qu;
- InitQueue(qu);
- while (1)
- {
- printf("输入a值(输入正数进队,负数出队,0结束):");
- scanf("%d", &a);
- if (a>0)
- {
- if (!enQueue(qu,a))
- printf(" 队列满,不能入队\n");
- }
- else if (a<0)
- {
- if (!deQueue(qu, x))
- printf(" 队列空,不能出队\n");
- }
- else
- break;
- }
- return 0;
- }
- <span style="color:#ff0000;">sqqueue.cpp</span>
- #include <stdio.h>
- #include <malloc.h>
- #include "sqqueue.h"
-
- void InitQueue(SqQueue *&q)
- {
- q=(SqQueue *)malloc (sizeof(SqQueue));
- q->front=q->rear=0;
- }
- void DestroyQueue(SqQueue *&q)
- {
- free(q);
- }
- bool QueueEmpty(SqQueue *q)
- {
- return(q->front==q->rear);
- }
-
-
- int QueueLength(SqQueue *q)
- {
- return (q->rear-q->front+MaxSize)%MaxSize;
- }
-
- 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;
- }
- <span style="color:#ff0000;">sqqueue.h</span>
- #ifndef SQQUEUE_H_INCLUDED
- #define SQQUEUE_H_INCLUDED
-
- #define MaxSize 5
- typedef char ElemType;
- typedef struct
- {
- ElemType data[MaxSize];
- int front,rear;
- } SqQueue;
-
-
- void InitQueue(SqQueue *&q);
- void DestroyQueue(SqQueue *&q);
- bool QueueEmpty(SqQueue *q);
- int QueueLength(SqQueue *q);
- bool enQueue(SqQueue *&q,ElemType e);
- bool deQueue(SqQueue *&q,ElemType &e);
-