当前位置:网站首页>循环队列(c语言实现)
循环队列(c语言实现)
2022-07-26 13:44:00 【ndream的三水】
存储结构
//循环队列的顺序存储结构
typedef struct {
QElemType data[MAXSIZES];
int front; // 头指针
int rear; // 尾指针,若队列不为空,指向队列元素的下一个位置
}SqQueue;
初始化操作
//初始化一个空队列Q
Status InitQueue(SqQueue *Q)
{
Q->front = Q->rear = 0;
return OK;
}
求队长
//返回Q的元素个数,也就是队列的当前长度
int QueueLength(SqQueue Q)
{
return (Q.rear - Q.front + MAXSIZES) % MAXSIZES;
}
入队操作
//入队操作
Status EnQueue(SqQueue *Q, QElemType e)
{
if ((Q->rear + 1) % MAXSIZES == Q->front) // 队列满的判断
return ERROR;
Q->data[Q->rear] = e; // 将元素e赋值给队尾
Q->rear = (Q->rear + 1) % MAXSIZES; // rear指针向后移动一个位置
// 若到最后则转到数组头部
return OK;
}
出队操作
//出队操作
Status DeQueue(SqQueue *Q, QElemType *e)
{
if (Q->front == Q->rear) // 队列空的判断
return ERROR;
*e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZES;
return OK;
}
实验结果

完整的代码
#include <stdio.h>
#define Status int
#define OK 1
#define ERROR 0
#define MAXSIZES 10
typedef int QElemType;
//循环队列的顺序存储结构
typedef struct {
QElemType data[MAXSIZES];
int front; // 头指针
int rear; // 尾指针,若队列不为空,指向队列元素的下一个位置
}SqQueue;
//初始化一个空队列Q
Status InitQueue(SqQueue *Q)
{
Q->front = Q->rear = 0;
return OK;
}
//返回Q的元素个数,也就是队列的当前长度
int QueueLength(SqQueue Q)
{
return (Q.rear - Q.front + MAXSIZES) % MAXSIZES;
}
//入队操作
Status EnQueue(SqQueue *Q, QElemType e)
{
if ((Q->rear + 1) % MAXSIZES == Q->front) // 队列满的判断
return ERROR;
Q->data[Q->rear] = e; // 将元素e赋值给队尾
Q->rear = (Q->rear + 1) % MAXSIZES; // rear指针向后移动一个位置
// 若到最后则转到数组头部
return OK;
}
//出队操作
Status DeQueue(SqQueue *Q, QElemType *e)
{
if (Q->front == Q->rear) // 队列空的判断
return ERROR;
*e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZES;
return OK;
}
int main()
{
SqQueue q;
InitQueue(&q);
printf("队列初始化成功!!!\n");
printf("插入2 4 6...\n");
EnQueue(&q, 2);
EnQueue(&q, 4);
EnQueue(&q, 6);
printf("插入成功,队列长度为 %d\n", QueueLength(q));
int a = 0, b = 0, c = 0;
DeQueue(&q, &a);
DeQueue(&q, &b);
DeQueue(&q, &c);
printf("队列中的元素为 %d %d %d\n", a, b, c);
return 0;
}
参考:
《大话数据结构》程杰著
边栏推荐
- Basic sentence structure of English ----- origin
- El table implements editable table
- 421. Maximum XOR value of two numbers in the array
- B+ tree (4) joint index -- MySQL from entry to proficiency (16)
- The.Net webapi uses groupname to group controllers to render the swagger UI
- 【C语言学习者必会的题目集锦1】巩固基础,稳步提高
- JS object assignment problem
- Target detection network r-cnn series
- 云智技术论坛工业专场 明天见!
- (Reprint) creation methods of various points in ArcGIS Engine
猜你喜欢

Team research and development from ants' foraging process (Reprint)

.net6与英雄联盟邂逅之——根据官方LCU API制作游戏助手

Unicorn, valued at $1.5 billion, was suddenly laid off, and another track was cold?

Brief introduction of reflection mechanism
![[flower carving hands-on] fun music visualization series small project (12) -- meter tube fast rhythm light](/img/99/6581b8a576e59a13aa4e977e3a1b70.jpg)
[flower carving hands-on] fun music visualization series small project (12) -- meter tube fast rhythm light

Ultimate doll 2.0 | cloud native delivery package
![[oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer](/img/7e/4f652c4d3d72ad563ddbc1c1f1f50b.png)
[oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer

Photoshop (cc2020) unfinished

周伟:寻找非共识性投资机会,陪伴延迟满足的创始团队

Ros2 learning (1) introduction to ros2
随机推荐
Flutter multi-channel packaging operation
Team research and development from ants' foraging process (Reprint)
Photoshop(CC2020)未完
消息的订阅和发布
Unity中序列化类为json格式
Brief introduction of reflection mechanism
Sword finger offer (VII): Fibonacci sequence
Comparator (interface between comparable and comparator)
LCL三相pwm整流器(逆变器)
JSON数据传递参数&日期型参数传递
Familiarize you with the "phone book" of cloud network: DNS
Oom caused by improper use of multithreading
Tdsql-c serverless: help start-ups achieve cost reduction and efficiency increase
[beauty of open source] nanomsg (2): req/rep mode
带你熟悉云网络的“电话簿”:DNS
Codeforces round 810 (Div. 2) [competition record]
TDSQL-C Serverless:助力初创企业实现降本增效
Time complexity and space complexity
[shaders realize overlay to re cover cross dressing effect _shader effect Chapter 9]
Mobile dual finger scaling event (native), e.originalevent.touches