队列的基本操作实验报告c语言,队列(顺序表)基本操作实现(c语言)

#include

#include

#define MAX_SIZE 100

typedef int ElemType;

typedef struct node{

int front;

int rear;

ElemType data[MAX_SIZE];

}*queueList,queueNode;

//顺序队列初始化

queueList initQueueList()

{

queueList q=(queueList)malloc(sizeof(queueNode));

if(!q)

{

printf("动态内存分配失败!\n");

exit(0);

}

q->front=0;

q->rear=0;

printf("队列初始化成功!\n");

return q;

}

//入队列

int enQueue(queueList q,ElemType e)

{

if((q->rear+1)%MAX_SIZE==q->rear)

{

printf("队列已满!");

return 0;

}

q->data[q->rear]=e;

q->rear=(q->rear+1)%MAX_SIZE;

return 1;

}

//出队列

int deQueue(queueList q,ElemType *e)

{

if(q->rear==q->front)

{

printf("\n队列为空");

return 0;

}

*e=q->data[q->front];

q->front=(q->front+1)%MAX_SIZE;

return 1;

}

//判断队列是否为空

int isEmpty(queueList q)

{

if(q->rear==q->front)

{

return 1;

}

return 0;

}

//获取队头元素

int getHead(queueList q)

{

if(!isEmpty(q))

{

return q->data[q->front];

}

return 0;

}

//清空队列

int clearQueue(queueList q)

{

if(q)

{

q->rear=q->front=0;

return 1;

}

return 0;

}

//求队列长度

int queueLength(queueList q)

{

return q->rear-q->front;

}

int main()

{

queueList q=initQueueList();

if(isEmpty(q))

{

printf("当前队列为空!\n");

}

int n,a[MAX_SIZE],i,d,head,length;

printf("请输入数字n:\n");

scanf("%d",&n);

printf("请输入%d个数:\n",n);

for(i=0;i

{

scanf("%d",&a[i]);

enQueue(q,a[i]);

head=getHead(q);

length=queueLength(q);

printf("当前队列长度为%d,队头元素为%d\n",length,head);

}

printf("打印出队序列:\n");

while(deQueue(q,&d))

{

printf("%d ",d);

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值