Sequence_Queue(顺序队列)

本文介绍了一个简单的顺序队列实现,包括初始化、判断空、显示、获取队首元素、插入及删除等基本操作,并通过示例演示了这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#define MAXSIZE 100
typedef int Datatype;
typedef struct {
Datatype a[MAXSIZE];
int front;
int rear;
}sequennce_queue;


void init(sequennce_queue *sq)
{
sq->front = 0;
sq->rear = 0;
}


int empty(sequennce_queue sq)
{
return (sq.front == sq.rear ? 1 : 0);
}


void display(sequennce_queue sq)
{
int i;
if (empty == 1)
printf("顺序表为空!\n");
else
{
for (i = sq.front; i < sq.rear; i++)
printf("%5d", sq.a[i]);
putchar('\n');
}
}


Datatype get(sequennce_queue sq)//取得队首的值
{
if (empty == 1)
{
printf("顺序表为空!,无法获取队首的值。\n");
return 0;
}
return sq.a[sq.front];
}


void insert(sequennce_queue *sq, Datatype x)
{
int i;
if (sq->rear == MAXSIZE)
printf("队列已满,无法进行插入!\n");
else
{
sq->a[sq->rear] = x;
++sq->rear;
}
}


void dele(sequennce_queue *sq)
{
if (empty == 1)
{
printf("顺序表为空!,无法进行删除。\n");
}
else
++sq->front;
}


void main()
{
int i;
sequennce_queue SQ;
init(&SQ);
printf("队列空为1,非空为0:%d\n", empty(SQ));
for (i = 0; i < 5; i++)
insert(&SQ, i*2 );
display(SQ);
printf("队列空为1,非空为0:%d\n", empty(SQ));
printf("首部的元素为:%d\n", get(SQ));
dele(&SQ);
printf("首部的元素为:%d\n", get(SQ));


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值