求一个C语言 循环队列的插入 完整程序
1个回答
展开全部
(1)编写一个程序,实现顺序环形队列的各种基本运算,并在此基础上设计一个主程序完成如下功能:
(1)初始化队列q;
(2)判断队列q是否非空;
(3)依次进队元素100、909、44、8;
(4)出队一个元素,输出该元素;
(5)输出队列q的元素个数;
(6)依次进队元素-67、55、99、70;
(7)输出队列q的元素个数;
#include<stdio.h>
#include<malloc.h>
#define QUEUE_INIT_SIZE 100
#define QUEUEINCREMENT 10
#define OK 1
#define TURE 1
#define FALSE 0
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q)
{
Q.base=(QElemType *)malloc
(QUEUE_INIT_SIZE*sizeof(QElemType));
if(!Q.base)
exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
int QueueNumElem(SqQueue Q)
{
return (Q.rear-Q.front)%QUEUE_INIT_SIZE;
}
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%QUEUE_INIT_SIZE==Q.front)
return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%QUEUE_INIT_SIZE;
return OK;
}
SqQueue DeQueue(SqQueue Q,int e)
{
int i;
if(Q.front==Q.rear)
printf("队为空!\n");
for(i=e;i<Q.rear;i++)
Q.base[i]=Q.base[i+1];
Q.rear--;
return Q;
}
int main()
{
SqQueue Q,Q1;
static int qele=0;
int i,j=0,k=0,m=0;
static int frontd=Q.front;
i=InitQueue(Q);
if(i==1)
printf("队初始化成功!!\n");
else
printf("队初始化失败!!\n");
if(Q.front!=Q.rear)
printf("队不为空!!\n");
else
printf("队为空L!!\n");
printf("输入数据(END of '9999'):");
scanf("%d",&qele);
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele);
scanf("%d",&qele);
}
frontd=Q.front;
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front]);
Q.front++;
j++;
}
printf("\n");
Q.front=frontd;
printf("输入要出队的元素:");
scanf("%d",&j);
while(Q.front!=Q.rear)
{
if(Q.base[Q.front]==j)
{
printf("%d\n",Q.base[Q.front]);
Q=DeQueue(Q,Q.front);
m++;
break;
}
Q.front++;
}
Q.front=frontd;
while(Q.front!=Q.rear)
{
printf(" %d ",Q.base[Q.front]);
Q.front++;
}
printf("\n");
Q.front=frontd;
printf("队的长度:%d\n",Q.rear-Q.front);
printf("输入数据(END of '9999'):");
scanf("%d",&qele);
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele);
scanf("%d",&qele);
}
Q.front=frontd;
printf("队的长度:%d\n",Q.rear-Q.front);
Q.front=frontd;
printf("出队顺序:");
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front]);
Q=DeQueue(Q,Q.front);
m++;
}
printf("end\n");
Q.front=0;
Q.rear=m;
while(Q.rear!=Q.front)
{
free(Q.base);
//Q.base++;
Q.front++;
if(Q.rear-1==Q.front)
printf("队已经释放!\n");
}
return 0;
}
(1)初始化队列q;
(2)判断队列q是否非空;
(3)依次进队元素100、909、44、8;
(4)出队一个元素,输出该元素;
(5)输出队列q的元素个数;
(6)依次进队元素-67、55、99、70;
(7)输出队列q的元素个数;
#include<stdio.h>
#include<malloc.h>
#define QUEUE_INIT_SIZE 100
#define QUEUEINCREMENT 10
#define OK 1
#define TURE 1
#define FALSE 0
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q)
{
Q.base=(QElemType *)malloc
(QUEUE_INIT_SIZE*sizeof(QElemType));
if(!Q.base)
exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
int QueueNumElem(SqQueue Q)
{
return (Q.rear-Q.front)%QUEUE_INIT_SIZE;
}
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%QUEUE_INIT_SIZE==Q.front)
return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%QUEUE_INIT_SIZE;
return OK;
}
SqQueue DeQueue(SqQueue Q,int e)
{
int i;
if(Q.front==Q.rear)
printf("队为空!\n");
for(i=e;i<Q.rear;i++)
Q.base[i]=Q.base[i+1];
Q.rear--;
return Q;
}
int main()
{
SqQueue Q,Q1;
static int qele=0;
int i,j=0,k=0,m=0;
static int frontd=Q.front;
i=InitQueue(Q);
if(i==1)
printf("队初始化成功!!\n");
else
printf("队初始化失败!!\n");
if(Q.front!=Q.rear)
printf("队不为空!!\n");
else
printf("队为空L!!\n");
printf("输入数据(END of '9999'):");
scanf("%d",&qele);
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele);
scanf("%d",&qele);
}
frontd=Q.front;
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front]);
Q.front++;
j++;
}
printf("\n");
Q.front=frontd;
printf("输入要出队的元素:");
scanf("%d",&j);
while(Q.front!=Q.rear)
{
if(Q.base[Q.front]==j)
{
printf("%d\n",Q.base[Q.front]);
Q=DeQueue(Q,Q.front);
m++;
break;
}
Q.front++;
}
Q.front=frontd;
while(Q.front!=Q.rear)
{
printf(" %d ",Q.base[Q.front]);
Q.front++;
}
printf("\n");
Q.front=frontd;
printf("队的长度:%d\n",Q.rear-Q.front);
printf("输入数据(END of '9999'):");
scanf("%d",&qele);
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele);
scanf("%d",&qele);
}
Q.front=frontd;
printf("队的长度:%d\n",Q.rear-Q.front);
Q.front=frontd;
printf("出队顺序:");
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front]);
Q=DeQueue(Q,Q.front);
m++;
}
printf("end\n");
Q.front=0;
Q.rear=m;
while(Q.rear!=Q.front)
{
free(Q.base);
//Q.base++;
Q.front++;
if(Q.rear-1==Q.front)
printf("队已经释放!\n");
}
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询