当前位置:网站首页>Basic operation of circular queue (Experiment)
Basic operation of circular queue (Experiment)
2022-04-23 15:03:00 【White folding fan y】
Basic operation of circular queue ( Abridged edition )
Preface
The following basic operations of circular queue are programmed : Build a queue , Take the team leader element , The team , Out of the team .
Want to know the detailed explanation of circular queue Please order me
️ Personal home page : Collection and attention , Never get lost ~ ️
Now let's take a look at how to realize it
One 、 Code implementation
#include <iostream>
using namespace std;
#define ERROR -1
#define OK 1
typedef int QElemType;
typedef int Status;
// Storage structure of circular queue
#define MAXQSIZE 100 // Maximum queue length
typedef struct
{
QElemType *base; // Used to dynamically allocate storage space
int front; // Team leader index
int rear; // End of team index
} SqQueue;
// initialization
void InitQueue (SqQueue &Q)
{
// Construct an empty queue
Q.base =new QElemType[MAXQSIZE];
Q.front=Q.rear=0;
}
// Destroy queue
void DestroyQueue(SqQueue &Q)
{
if(Q.base)
free(Q.base);
Q.base = NULL;
Q.front = Q.rear = 0;
}
// Clear queue
void ClearQueue(SqQueue &Q)
{
Q.front=Q.rear=0;
}
// Find the length
int QueueLength (SqQueue Q)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
// Sentenced to empty
bool QueueEmpty (SqQueue Q)
{
return (Q.front==Q.rear);
}
// Find the team head element
Status GetHead (SqQueue Q, QElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
return OK;
}
// Loop queue in
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear = (Q.rear+1) % MAXQSIZE;
return OK;
}
// Loop queue out
Status DeQueue (SqQueue &Q,QElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1) % MAXQSIZE;
return OK;
}
// Traversal causes the queue to display
void DisplayQueue(SqQueue Q)
{
int i=Q.front;
while(Q.front!=Q.rear && (i+MAXQSIZE) % MAXQSIZE != Q.rear)
{
cout<<Q.base[i]<<endl;
i++;
}
}
void show_help()
{
cout<<"******* Data Structure ******"<<endl;
cout<<"1---- Empty the circular queue "<<endl;
cout<<"2---- Determine whether the circular queue is empty "<<endl;
cout<<"3---- Find the length of the loop queue "<<endl;
cout<<"4---- Take the team leader element "<<endl;
cout<<"5---- The team "<<endl;
cout<<"6---- Out of the team "<<endl;
cout<<"7---- Show queue "<<endl;
cout<<" sign out , Input 0"<<endl;
}
int main()
{
char operate_code;
show_help();
SqQueue Q;
InitQueue(Q);
QElemType e;
int i;
while(1)
{
cout<<" Please enter the operation code :";
cin>>operate_code;
if(operate_code=='1')
{
cout<<"The queue has been cleared."<<endl;
ClearQueue(Q);
}
else if (operate_code=='2')
{
if(QueueEmpty(Q))
cout<<"The queue is empty."<<endl;
else
cout<<"The queue is not empty."<<endl;
}
else if (operate_code=='3')
{
cout<<"The length of queue is:"<<QueueLength(Q)<<endl;
}
else if (operate_code=='4')
{
cout<<" The team leader element is :"<<endl;
if(GetHead(Q,e) == 1) cout<<e<<endl;
else cout <<"error"<<endl;
}
else if (operate_code=='5')
{
cout<<" Please enter the number of you want to join the team :"<<endl;
cin>>e;
EnQueue(Q,e);
}
else if (operate_code=='6')
{
cout<<" The outgoing element is :"<<endl;
if(DeQueue(Q,e)) cout<<e<<endl;
}
else if (operate_code=='7')
{
cout<<"The contents of the queue are:"<<endl;
DisplayQueue(Q);
}
else if (operate_code=='0')
{
break;
}
else
{
cout<<"\n Opcode error !!!"<<endl;
show_help();
}
}
// Call the destroy stack function
DestroyQueue(Q);
return 0;
}
Two 、 Running results
summary
This paper is used to introduce the code implementation process and running result example of circular queue in data structure . Displays the initialization of the circular queue in a menu style 、 Empty 、 The destruction 、 Find the length 、 Find the team head element 、 Sentenced to empty 、 The team 、 Out of the queue and traverse the queue to display .
版权声明
本文为[White folding fan y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231410400529.html
边栏推荐
- we引用My97DatePicker 实现时间插件使用
- My raspberry PI zero 2W tossing notes record some problems encountered and solutions
- Async keyword
- Alexnet model
- Achievements in science and Technology (21)
- JS - implémenter la fonction de copie par clic
- js——实现点击复制功能
- Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
- 中富金石财富班29800效果如何?与专业投资者同行让投资更简单
- Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
猜你喜欢
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
Svn detailed use tutorial
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
The art of automation
3、 Gradient descent solution θ
Introduction to distributed transaction Seata
Swift protocol Association object resource name management multithreading GCD delay once
分布式事务Seata介绍
随机推荐
Comment eolink facilite le télétravail
We reference My97DatePicker to realize the use of time plug-in
win10 任务栏通知区图标不见了
SQLSERVER事物与锁的问题
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
Nuxt project: Global get process Env information
OPPO数据湖统一存储技术实践
Frame synchronization implementation
QT Detailed explanation of pro file
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
[jz46 translate numbers into strings]
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
Async void caused the program to crash
Practice of unified storage technology of oppo data Lake
UML学习_day2
JUC学习记录(2022.4.22)
分布式事务Seata介绍