数据结构 循环队列的基本操作

来源:互联网 发布:铁岭金石网络会所 编辑:程序博客网 时间:2024/06/04 05:39
#include <stdio.h>#include <stdlib.h>#define MAXQSIZE  1000#define OK        1#define ERROR     0 #define OVERFLOW  -2typedef int QElemType;typedef struct{QElemType *base;int front;int rear;}SqQueue;int InitQueue(SqQueue &Q){Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));if (!Q.base){exit(OVERFLOW);}Q.front = Q.rear = 0;return OK;}int QueueLength(SqQueue Q){return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;}int EnQueue(SqQueue &Q, QElemType e){if ((Q.rear +1) % MAXQSIZE == Q.front){return ERROR;}Q.base[Q.rear] = e;Q.rear = (Q.rear +1) % MAXQSIZE;return OK;}int DeQueue (SqQueue &Q, QElemType &e){if (Q.front == Q.rear ){return ERROR;}e = Q.base [Q.front];Q.front = (Q.front+1) % MAXQSIZE;return OK;}int isQueueEmpty(SqQueue Q){//队列是否为空if (Q.front == Q.rear ){return 1;}return 0;}int isQueueFull(SqQueue Q){if ((Q.rear + 1 ) % MAXQSIZE == Q.front){return OK;}return ERROR;}void Print_Q(SqQueue Q){int i;if ((Q.rear +1) % MAXQSIZE == Q.front){return;}for (i=0; i<QueueLength(Q); i++){printf("%d ",Q.base[Q.front+i]);}printf("\n");}int GetHead(SqQueue &Q, QElemType &e){if (Q.front == Q.rear ){return ERROR;}e = Q.base[Q.front];return OK;}int main(){SqQueue Q;int i,n;InitQueue(Q);scanf("%d",&n);for (i=1; i<=n; i++){EnQueue(Q,i);}Print_Q(Q);DeQueue(Q,n);DeQueue(Q,n);DeQueue(Q,n);DeQueue(Q,n);Print_Q(Q);GetHead(Q,i);printf("%d\n",i);return 0;}

0 0
原创粉丝点击