当前位置:网站首页>Double ended queue
Double ended queue
2022-07-23 11:59:00 【Look, that's a licking dog】
deque It's a double ended queue
(deque, full name double-ended queue) It's a data structure with the properties of queue and stack . Elements in a two terminal queue can pop up from both ends , It limits the insertion and deletion operations on both sides of the table .
Structure definition and initialization
typedef int T;
typedef struct DoubleQuene
{
T data[MAXSIZE];
int front;// Head position
int rear;// Tail position
}*deque;
bool InitQuene(deque q)// initialization
{
if (!q)
return false;
q->front = 0;
q->rear = 0;
return true;
}
Compared with circular queues , There are functions equivalent to header insertion and tail deletion .
bool push_back(deque q, T val)// Join the team from the end
{
if (Is_Full(q))
return false;
q->data[q->rear] = val;
q->rear = (q->rear + 1) % MAXSIZE;
return true;
}
bool push_front(deque q, T val)// Join the team from scratch
{
if (Is_Full(q))
return false;
q->front = (q->front + MAXSIZE - 1) % MAXSIZE;
q->data[q->front] = val;
return true;
}
T pop_front(deque q)// Out of the team
{
if (Is_Empty(q))
return -1;
T tmp = q->data[q->front];
q->front = (q->front + 1) % MAXSIZE;
return tmp;
}
T pop_back(deque q)// Join the team from the end
{
if (Is_Empty(q))
return -1;
q->rear = (q->rear + MAXSIZE - 1) % MAXSIZE;
T tmp = q->data[q->rear];
return tmp;
}
There is also getting the head and tail elements .
T GetHead(deque q)// Get team leader elements
{
if (Is_Empty(q))
return -1;
return q->data[q->front];
}
T GetTail(deque q)// Get the tail node
{
if (Is_Empty(q))
return -1;
return q->data[q->front];
}
Other operations
bool ClearQuene(deque q)// Qingdui
{
if (!q)
return false;
q->front = q->rear = 0;
return true;
}
bool Is_Empty(deque q)// Sentenced to empty
{
return q->front == q->rear;
}
bool Is_Full(deque q)// Full sentence
{
return (q->rear + 1) % MAXSIZE == q->front;
}
int GetLength(deque q)// The number of data
{
return (q->rear - q->front + MAXSIZE) % MAXSIZE;
}
All the code
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 40
typedef int T;
typedef struct DoubleQuene
{
T data[MAXSIZE];
int front;// Head position
int rear;// Tail position
}*deque;
bool InitQuene(deque q)// initialization
{
if (!q)
return false;
q->front = 0;
q->rear = 0;
return true;
}
bool ClearQuene(deque q)// Qingdui
{
if (!q)
return false;
q->front = q->rear = 0;
return true;
}
bool Is_Empty(deque q)// Sentenced to empty
{
return q->front == q->rear;
}
bool Is_Full(deque q)// Full sentence
{
return (q->rear + 1) % MAXSIZE == q->front;
}
int GetLength(deque q)// The number of data
{
return (q->rear - q->front + MAXSIZE) % MAXSIZE;
}
T GetHead(deque q)// Get team leader elements
{
if (Is_Empty(q))
return -1;
return q->data[q->front];
}
T GetTail(deque q)// Get the tail node
{
if (Is_Empty(q))
return -1;
return q->data[q->front];
}
bool push_back(deque q, T val)// Join the team from the end
{
if (Is_Full(q))
return false;
q->data[q->rear] = val;
q->rear = (q->rear + 1) % MAXSIZE;
return true;
}
bool push_front(deque q, T val)// Join the team from scratch
{
if (Is_Full(q))
return false;
q->front = (q->front + MAXSIZE - 1) % MAXSIZE;
q->data[q->front] = val;
return true;
}
T pop_front(deque q)// Out of the team
{
if (Is_Empty(q))
return -1;
T tmp = q->data[q->front];
q->front = (q->front + 1) % MAXSIZE;
return tmp;
}
T pop_back(deque q)// Join the team from the end
{
if (Is_Empty(q))
return -1;
q->rear = (q->rear + MAXSIZE - 1) % MAXSIZE;
T tmp = q->data[q->rear];
return tmp;
}
void Show(deque q)// Traverse
{
if (Is_Empty(q))
return;
int i = q->front;
while (i != q->rear)
{
printf("%d ", q->data[i]);
i = (i + 1) % MAXSIZE;
}
printf("\n");
}
int main()
{
deque q;
q = (deque)malloc(sizeof(DoubleQuene));
InitQuene(q);
for (int i = 1; i <= 10; i++)
{
push_front(q, i);
push_back(q,i);
}
Show(q);
printf("Head:%d\n", GetHead(q));
printf("Tail:%d\n", GetTail(q));
printf("Len:%d\n", GetLength(q));
for (int i = 1; i <= 3; i++)
{
printf("%d ", pop_front(q));
printf("%d ", pop_back(q));
}
printf("\n");
Show(q);
return 0;
}
Double ended queues can complete the desired functions by limiting the interface , For example, limit tail insertion , Tail deletion is used as a stack, and so on .
边栏推荐
- 方法的定义应用
- The user logs in continuously (interruption is allowed) to query SQL
- 八、集合框架和泛型
- Entrepôt de données 4.0 Notes - acquisition de données sur le comportement de l'utilisateur II
- NT68661-屏参升级-RK3128-开机自己升级屏参
- 10. I/o input / output stream
- [pyrodiomics] the extracted image omics characteristic value is abnormal (many 0 and 1)
- Object类
- Print right angle triangle, isosceles triangle, diamond
- APP自动化测试工具-appium的安装及使用
猜你喜欢

APP自动化测试工具-appium的安装及使用

Websocket long connection

UE4.24版本VR项目打包后,未出现手柄控制器

飞桨高层API实现人脸关键点检测

使用飞桨实现肺部 CT 扫描的 3D 图像分类

数仓4.0笔记——用户行为数据采集四
![[monitoring deployment practice] display the charts of Prometheus and loki+promtail based on granfana](/img/34/b7a05bff05e1d3a1daef4fb2b98a92.png)
[monitoring deployment practice] display the charts of Prometheus and loki+promtail based on granfana

pytorch与paddlepaddle对比——以DCGAN网络实现为例

Cuda10.0 configuration pytorch1.7.0+monai0.9.0
![[untitled]](/img/c6/735dec4022d7fed8a62b5df45df17b.png)
[untitled]
随机推荐
[untitled]
[flick]flick on yarn's flick conf simplest configuration
Entrepôt de données 4.0 Notes - acquisition de données commerciales
两个栈共用空间
[untitled]
[untitled]
Charles抓包的使用步骤
3. DQL (data query statement)
Kubesphere ha install (II)
What is the difference between abstract classes and interfaces?
Inheritance and polymorphism
Internet communication
MySQL卸载
Method of recognizing b value from DICOM tag in DWI image
MySQL用户管理
规范数据库设计
笔记 | 百度飞浆AI达人创造营:数据获取与处理(以CV任务为主)
Yarn capacity scheduler settings
1、MySQL初体验
Data warehouse 4.0 notes - user behavior data collection III