#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
//定义队列结构
typedef struct Queue
{
int *data;
int rear;
int front;
}squeue;
//初始化100个数字的队列
void Initqueue(squeue *q)
{
q->data=(int *)malloc(MAX*sizeof(int));
q->front=q->rear=0;
}
//返回队列长度
void queueL(squeue *q)
{
printf("此队列的长度为:%d\n",(q->rear-q->front+MAX)%MAX);
}
//插入结点(入队)
void Insert(squeue *q,int e)
{
if((q->rear+1)%MAX==q->front)
printf("队列已满!\n");
else
{
q->data[q->rear]=e;
q->rear=(q->rear+1)%MAX;
}
}
//删除结点(出队)
void Delete(squeue *q)
{
if(q->rear==q->front)
printf("队列已空!\n");
else
{
q->front=(q->front+1)%MAX;
printf("出队/删除 成功!\n");
}
}
//打印(遍历)队列
void Print(squeue q)
{
printf("________________________________________________________\n");
while(q.front!=q.rear)
{
printf("%d ",q.data[q.front]);
q.front=(q.front+1)%MAX;
}
printf("\n________________________________________________________\n");
}
int main()
{
squeue l;
int i,n,x,e;
Initqueue(&l);//初始化队列
while(1)
{
printf("\n***********************************************");
printf("\n 1.建立队列");
printf("\n 2.打印队列");
printf("\n 3.入队");
printf("\n 4.出队");
printf("\n 0.退出");
printf("\n***********************************************");
printf("\n请输入操作数:");
scanf("%d",&n);
switch(n)
{
case 0:
return 0;
case 1:
Initqueue(&l);
printf("\n请输入队列长度:");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
printf("\n请输入第%d个结点值:",i);
scanf("%d",&e);
Insert(&l,e);
}
printf("\n操作成功!");
break;
case 2:
Print(l);
break;
case 3:
printf("\n请输入需要入队的值:");
scanf("%d",&e);
Insert(&l,e);
Print(l);
break;
case 4:
Delete(&l);
Print(l);
break;
default:
printf("\n您的输入有误!");
break;
}
}
}
关于循环队列的入队、出队、遍历的操作
最新推荐文章于 2024-10-05 21:10:34 发布