/*
* 双端队列
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct Deque
{
int *data;
int front;
int tail;
int size;
int capacity;
} *Deque; // struct Deque* -> Deque
// 初始化
Deque initiation(int capacity)
{
Deque queue = (Deque) malloc(sizeof (struct Deque));
queue->data = (int *) malloc(sizeof (int) * capacity);
queue->capacity = capacity;
queue->front = 0;
queue->tail = 0;
queue->size = 0;
if (queue == NULL || queue->data == NULL)
{
perror("Deque initiation failed");
exit(-1);
}
return queue;
}
// 获取容量
int getCapacity(Deque q)
{
return q->capacity;
}
// 获取元素个数
int getSize(Deque q)
{
return q->size;
}
// 判断队列是否空
int isEmpty(Deque q)
{
return q->size == 0;
}
// 扩容/缩容
void resize(int newCapacity, Deque q)
{
int *newData = (int *) malloc(newCapacity * sizeof (in
C语言双端队列
最新推荐文章于 2024-08-20 15:01:17 发布
本文详细介绍了如何使用C语言来实现一个双端队列的数据结构,包括其基本操作如插入、删除、检查元素等。同时,文章还探讨了双端队列在算法和数据结构中的应用和优势。
摘要由CSDN通过智能技术生成