- /**
- *
- * Althor:Hacker Hao
- * Create:2023.10.11
- *
- */
-
- #include
- using namespace std;
- #define MAXSIZE 200
- typedef struct Deque
- {
- int front; //头
- int rear; //尾
- int num; //队列中的元素数量
- int arr[MAXSIZE]; //队列中存储的数字
- }Deque;
-
- Deque* Init() //初始化队列
- {
- Deque* q = (Deque*)malloc(sizeof(Deque));
- if (!q)
- {
- cout << "建立错误!" << endl;
- exit(0);
- }
- q->front = q->rear = -1;
- q->num = 0;
- return q;
- }
-
- void Clean(Deque* q) //清空队列
- {
- if (!q)
- {
- cout << "队列不存在! " << endl;
- return;
- }
- //由于队列只是用了一次动态内存分配,所以直接free
- q->front = q->rear = -1;
- q->num = 0;
- }
-
- void Destroy(Deque* q) //销毁队列
- {
- if (!q)
- {
- cout << "队列不存在!" << endl;
- return;
- }
- free(q);
- q = NULL;
- }
-
- bool IsEmpty(Deque* q) //判断是否为空
- {
- if (q == NULL)
- {
- cout << "队伍不存在!" << endl;
- return true; //为了防止队列空时对空指针操作
- }
- return q->num == 0;
- }
-
- bool IsFull(Deque* q) //判断是否队满
- {
- if (q == NULL)
- {
- cout << "队伍不存在!" << endl;
- return true; //为了防止在队不满的情况下对空指针操作
- }
- return q->num == MAXSIZE;
- }
-
- int GetHead(Deque* q) //返回队首元素
- {
- if (q == NULL)
- {
- cout << "队列不存在!" << endl;
- return -1;
- }
- if (IsEmpty(q))
- {
- cout << "队列为空,没有头元素" << endl;
- return -1;
- }
- return q->arr[q->front];
- }
-
- int GetTail(Deque* q) //返回队尾元素
- {
- if (q == NULL)
- {
- cout << "队列不存在" << endl;
- return -1;
- }
- if (IsEmpty(q))
- {
- cout << "队列为空!" << endl;
- return -1;
- }
- return q->arr[q->rear];
- }
-
-
- int GetHeadIndex(Deque* q, int size) {
- if (IsEmpty(q)) {
- q->rear = 0;//如果从对头入队的是第一个元素,则需要将队头队尾指针都设为0
- return 0;
- }
- if (q->front - 1 < 0) {
- return (q->front - 1) + size;
- }
- else {
- return q->front - 1;
- }
- }
-
-
- void PushFromHead(Deque* q, int k)
- {
- if (q == NULL)
- {
- cout << "队列不存在" << endl;
- return;
- }
- if (IsFull(q))
- {
- cout << "队列已满,无法入队" << endl;
- return;
- }
- q->front = GetHeadIndex(q, MAXSIZE);
- q->num++;
- q->arr[q->front] = k;
- }
-
- //获得从队尾入队的下标
- int GetTailIndex(Deque* q, int size)
- {
- if (IsEmpty(q))
- {
- q->front = 0;
- return 0;
- }
- return (q->rear + 1) % size;
- }
-
- //从队尾入队
- void PushFromTail(Deque* q, int k)
- {
- if (!q)
- {
- cout << "队列不存在" << endl;
- return;
- }
- if (IsFull(q))
- {
- cout << "队列已满,无法入队" << endl;
- return;
- }
- q->rear = GetTailIndex(q, MAXSIZE);
- q->num++;
- q->arr[q->rear] = k;
- }
-
-
- //从队头出队
- int PopFromHead(Deque* q) {
- if (q == NULL) {
- cout << "队列不存在" << endl;
- return -1;
- }
- if (IsEmpty(q)) {
- cout << "队列为空,无法出队" << endl;
- return -1;
- }
- q->num--;
- int ret = q->arr[q->front];
- q->front = (q->front + 1) % MAXSIZE;
- return ret;
- }
-
- //从队尾出队
- int PopFromTail(Deque* q)
- {
- if (q == NULL) {
- cout << "队列不存在" << endl;
- return -1;
- }
- if (IsEmpty(q)) {
- cout << "队列为空,无法出队" << endl;
- return -1;
- }
- int ret = q->arr[q->rear];
- q->num--;
- if (q->rear - 1 < 0) {
- q->rear = q->rear - 1 + MAXSIZE;
- }
- else {
- q->rear -= 1;
- }
- return ret;
- }
-
- //从队头开始打印队列数据
- void Print(Deque* q)
- {
- if (q == NULL) {
- cout << "队列不存在" << endl;
- return;
- }
- if (IsEmpty(q)) {
- cout << "队列为空,无法打印" << endl;
- return;
- }
- int count = q->num;
- int p = q->front;
- while (count--) {
- cout << q->arr[p] << " ";
- p = (p + 1) % MAXSIZE;
- }
- cout << endl;
- }
-
- int main()
- {
- cin.tie(0), cout.tie(0);
- Deque* q = Init();
-
- cout << "从队首入队:" << endl;
- for (int i = 0; i < 10; i++)
- {
- PushFromHead(q, i);
- }
- Print(q);
-
- cout << "从队尾入队:" << endl;
- for (int i = 20; i < 35; i++) {
- PushFromTail(q, i);
- }
- Print(q);
-
- cout << "从队头删除的元素是:" << PopFromHead(q) << endl;
- Print(q);
-
- cout << "从队尾删除的元素是:" << PopFromTail(q) << endl;
- Print(q);
-
- cout << "队首元素是:" << GetHead(q) << endl;
- cout << "队尾元素是:" << GetTail(q) << endl;
-
- return 0;
- }
-