链表实现循环队列的插入(入队)、删除(出队)和遍历。
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int Status;
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q){
Q.front=Q.rear=new QNode;
Q.front->next=NULL;
return OK;
}
Status EnQueue(LinkQueue &Q,QElemType e){
QNode *p;
p=new QNode;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e){
QNode *p;
p=new QNode;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
printf("出队元素为:");
printf("%d\n",e);
delete p;
return OK;
}
QElemType GetHead(LinkQueue Q){
if(Q.front!=Q.rear)
return Q.front->next->data;
}
Status QueueTraverse(LinkQueue Q){
QNode *p;
p=Q.front->next;
if(p==NULL)
printf("队列为空\n");
else{
printf("当前队列中的元素为:\n");
for(p=Q.front->next;p!=NULL;p=p->next)
printf("%d ",p->data);
printf("\n");
}
return OK;
}
int main(){
LinkQueue q;
InitQueue(q);
int c;
int n,m;
printf("1.入队2.出队3.遍历0.退出\n");
while(scanf("%d",&c)!=EOF){
switch(c){
case(1):
printf("请输入入队元素个数:");
scanf("%d",&n);
printf("请输入入队元素:");
while(n--){
scanf("%d",&m);
EnQueue(q,m);
}
printf("\n");
break;
case(2):
DeQueue(q,m);
printf("\n");
break;
case(3):
QueueTraverse(q);
printf("\n");
break;
case(0):
exit(0);
}
}
return 0;
}
该博客详细介绍了如何利用C语言通过链表来实现循环队列,包括入队、出队和队列遍历等基本操作。
2064

被折叠的 条评论
为什么被折叠?



