当前位置:网站首页>Queue operation---
Queue operation---
2022-07-01 12:23:00 【Between the steps】
queue
queue , It is also a sequential storage structure , Its basic operation : Additions and deletions , Sentenced to empty , Full contract operation
1. initialization
#include<stdio.h>
#define Maxsize 10 // The maximum number of elements in the stack
typedef struct{
ElemType data[Maxsize]; // Static array elements
int front,rear; // Team head pointer and team tail pointer
}SqQueue;
void InitQueue(SqQueue &Q){
// initialization The pointer at the head and tail of the team points to 0
Q.front=Q.rear=0;
}
void testQueue{
Queue Q; // Declare a queue , Allocate space for it
// Follow up operation
InitStack(Q);
}

2. Empty operation ( The head pointer equals the tail pointer )
bool QueueEmpty(SqQueue Q){
if(Q.front==Q.rear)
return true; // Team space
else
return false;
}
3. Joining operation
bool EnQueue(SqQueue &Q,ElemType x){
if((Q.rear+1)%MaxSize==Q.front) // Judge that the team is full Not at all Q.rear==Q.front , One unit must be sacrificed
return false;
Q.data[Q.rear]=x; // take x Insert the line
Q.rear=(Q.rear+1)%Maxsize; // Move the pointer back at the end of the line
return true;
}

4. Out of line operation
bool DeQueue(SqQueue &Q,ElemType &x){
if(Q.rear==Q.front)
return false; // The team reported an error
x=Q.data[Q.front]; // Take out the deleted value
Q.front=(Q.front+1)%Maxsize; // The team leader pointer changes
return true;
}
The change of the operation pointer of leaving and entering the team is the same
Q.rear=(Q.rear+1)%Maxsize
Q.front=(Q.front+1)%Maxsize
5. Get the value of the team head element
bool GetHead(SqQueue &Q,ElemType &x){
if(Q.rear==Q.front)
return false; // The team reported an error
x=Q.data[Q.front]; // Directly output the value of the team head element
return true;
}
If you don't sacrifice a space What shall I do? ??
Method 2
use size Record the current length of the queue , On initialization size=0;
Insert the success size++
Delete successful size–
size=Maxsize Full for the team
size=0 Empty for team
typedef struct{
ElemType data[Maxsize]; // Static array elements
int front,rear; // Team head pointer and team tail pointer
int size; // Current queue length
}SqQueue;
Method 3
Put a tag To record the recent deletion and insertion , Such as
Every time the deletion succeeds , Make tag=0;
Every time the insertion succeeds , Make tag=1;
The judging team is full of :if(Q.frontQ.rear &&tag1)
Judge whether the team is empty :if(Q.frontQ.rear &&tag====0)
typedef struct{
ElemType data[Maxsize]; // Static array elements
int front,rear; // Team head pointer and team tail pointer
int tag; // Recently deleted or inserted
}SqQueue;
6. Determine the number of elements in the queue
Number of queue elements =(rear+MaxSize-front)%MaxSize
One more : If rear When the pointer points to the tail element , The time to join the team is
Q.rear=(Q.rear+1)%Maxsize;
Q.data[Q.rear]=x;
At this time, the initial pointer is
Judge team empty :(Q.rear+1)%Maxsize==Q.front;
The team is full :
Sacrifice storage units or add auxiliary variables
边栏推荐
- 技术分享 | MySQL:从库复制半个事务会怎么样?
- 91.(cesium篇)cesium火箭发射模拟
- Interpretation of R & D effectiveness measurement framework
- fatal error: execution: 没有那个文件或目录
- LeetCode 454. Add four numbers II
- 强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
- Consolidate -c operator
- Mysql database knowledge collation
- Implementation of address book management system with C language
- Golang introduces the implementation method of the corresponding configuration file according to the parameters
猜你喜欢

Self organization is the two-way rush of managers and members

循环链表--

强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
![[MCU] [nixie tube] nixie tube display](/img/5e/9e14302b4e4f5e03601392ac90479d.png)
[MCU] [nixie tube] nixie tube display

Common chart usage of Bi tools

MySQL common functions

C knowledge point form summary 2
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]

2022-06-28-06-29

Typora realizes automatic uploading of picture pasting
随机推荐
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5
MySQL common functions
Understanding of NAND flash deblocking
CPI教程-异步接口创建及使用
Golang des-cbc
Dlhsoft Kanban, Kanban component of WPF
C # dependency injection (straight to the point) will be explained as soon as you see the series
On recursion and Fibonacci sequence
AI抠图工具
C knowledge point form summary 2
Self organization is the two-way rush of managers and members
JS related interview questions and answers (1)
指定的服务已标记为删除
Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
Sort out relevant contents of ansible
Leetcode (Sword finger offer) - 58 - ii Rotate string left
Golang根据参数引入相应配置文件的实现方式
LeetCode 454. Add four numbers II
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
被锡膏坑了一把



