当前位置:网站首页>Have you learned the basic operation of circular queue?
Have you learned the basic operation of circular queue?
2022-04-23 15:03:00 【White folding fan y】
Newcomer Xiaobai's first blog
️ I hope you will pay more attention
It will be updated frequently in the future ~️ Personal home page : Collection and attention , Never get lost ~ ️
Preface
Tips: It's a little long , Little Lord, be patient ~
Programming to achieve the basic operation of circular queue : Build a queue , Take the team leader element , The team , Out of the team
One 、 What is a circular queue ?
1️⃣ Let's first introduce the linear table : The data structure is divided into linear structure and nonlinear structure , Queues and linear tables are linear structures . The linear table is written by n A finite sequence of data elements , The sequence has a unique “ first ” And unique “ the last one ” data elements ; except “ first ” and “ the last one ” outside , Each data element in the queue has only one direct precursor and one direct successor . The insertion and deletion of a linear table can be performed anywhere in the table .
2️⃣ Let's talk about the queue : A queue is a special kind of linear table , What's special about this is that it's only allowed at the front of the table (front) Delete operation , And at the back end of the table (rear) Insert operation . Like the stack , A queue is a linear table with restricted operations . The end of the insertion operation is called the tail of the queue , The end of the delete operation is called the queue head . When there are no elements in the queue , Called an empty queue .
3️⃣ Related concepts of queue : The data elements of a queue are called queue elements . Inserting a queue element into a queue is called queuing , Deleting a queue element from a queue is called out of queue . Because queues can only be inserted at one end , Delete... At the other end , So only the first elements to enter the queue can be deleted from the queue first , So the queue is also called first in, first out (FIFO—first in first out) The linear table .
4️⃣ Why do you have a circular queue : In order to make full use of vector space , Overcome " False spillover " The method of phenomenon is : Think of a vector space as a ring with its head and tail connected , And call this vector a cyclic vector . The queues stored in it are called circular queues (Circular Queue). A circular queue is a sequential queue connected end to end , Look at the table that stores the queue elements logically as a ring , It's called a circular queue .
5️⃣ Related characteristics of circular queue : In the circular queue structure , When the last position of the storage space has been used and it is necessary to enter the queue operation , Only the first location of the storage space is free , You can add the element to the first position , The first location of the storage space will be used as the tail of the team . Circular queues can more easily prevent pseudo overflow , But the queue size is fixed .
Two 、 Defined function
1. Define the storage structure
// 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;
2. initialization
// initialization
void InitQueue (SqQueue &Q)
{
// Construct an empty queue
Q.base =new QElemType[MAXQSIZE];
Q.front=Q.rear=0;
}
3. Destroy queue
// Destroy queue
void DestroyQueue(SqQueue &Q)
{
if(Q.base)
free(Q.base);
Q.base = NULL;
Q.front = Q.rear = 0;
}
4. Clear queue
// Clear queue
void ClearQueue(SqQueue &Q)
{
Q.front=Q.rear=0;
}
5. Find the length
// Find the length
int QueueLength (SqQueue Q)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
6. Sentenced to empty
// Sentenced to empty
bool QueueEmpty (SqQueue Q)
{
return (Q.front==Q.rear);
}
7. Find the team head element
// 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;
}
8. Loop queue in
// 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;
}
9. Loop queue out
// 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;
}
10. Traversal causes the queue to display
// 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++;
}
}
3、 ... and 、 Define small menus
You can use a small menu to make the interface more beautiful ~
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;
}
Four 、 Complete code
️️️
#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;
}
5、 ... and 、 Running results
️️️
summary
This paper is used to introduce the code implementation process and running result example of circular queue in data structure . The initialization of the circular queue is displayed in menu style 、 Empty 、 The destruction 、 Find the length , Find the team head element 、 Sentenced to empty 、 The team , Out of the team , And traversing the queue to make it display .
版权声明
本文为[White folding fan y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231410400560.html
边栏推荐
- raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
- I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
- Async keyword
- What is the main purpose of PCIe X1 slot?
- The difference between having and where in SQL
- Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
- epoll 的EPOLLONESHOT 事件———实例程序
- 多语言通信基础 06 go实现grpc的四种数据流模式实现
- 在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
- thinkphp5+数据大屏展示效果
猜你喜欢
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
[detailed explanation of factory mode] factory method mode
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
PCIe X1 插槽的主要用途是什么?
eolink 如何助力远程办公
[NLP] HMM hidden Markov + Viterbi word segmentation
分享 20 个不容错过的 ES6 的技巧
8.4 realization of recurrent neural network from zero
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
随机推荐
Redis master-slave synchronization
Is asemi ultrafast recovery diode interchangeable with Schottky diode
分享 20 个不容错过的 ES6 的技巧
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
nuxt项目:全局获取process.env信息
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
Thinkphp5 + data large screen display effect
We reference My97DatePicker to realize the use of time plug-in
Frame synchronization implementation
Leetcode162 - find peak - dichotomy - array
Realization of four data flow modes of grpc based on Multilingual Communication
2-Go变量操作
How does eolink help telecommuting
Alexnet model
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
Epoll's et, lt working mode -- example program
分布式事务Seata介绍
解决computed属性与input的blur事件冲突问题
Sword finger offer II 019 Delete at most one character to get palindrome (simple)