数据结构 循环队列的基本操作
【摘要】 #include <stdio.h>#include <stdlib.h> #define MAXQSIZE 1000#define OK 1#define ERROR 0 #define OVERFLOW -2 typedef int QElemType; typedef struct{ QElemType *base; int front; i...
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
#define MAXQSIZE 1000
-
#define OK 1
-
#define ERROR 0
-
#define OVERFLOW -2
-
-
typedef 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;
-
}
文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq523176585/article/details/17249153
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)