今天我们来介绍一种新的数据结构,队列。
队列的概念即结构
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。
队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头
队列的实现
队列与栈类似。也可以选择数组或者链表来实现。
但是使用链表结构的话更优一些,因为使用数组的结构,出队列在数组的头部,效率低于链表。
1.头文件Queue.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
struct QueueNode
{
struct QueueNode* next;
QDataType data;
};
typedef struct Queue
{
QueueNode* phead;
QueueNode* ptail;
}Queue;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
int QueueSize(Queue* pq);
int QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
bool QueueEmpty(Queue* pq);
2.源文件Queue.c
#include"Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
}
//------------------------------------------------
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->phead;
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
}
//------------------------------------------------------
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
if (newNode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newNode->data = x;
newNode->next = NULL;
if (pq->ptail == NULL)
{
pq->phead = pq->ptail = newNode;
}
else
{
pq->ptail->next = newNode;
pq->ptail = newNode;
}
}
//-----------------------------------------------------
void QueuePop(Queue* pq) //对头
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->phead->next == NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QueueNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
}
//------------------------------------------------------
int QueueSize(Queue* pq)
{
assert(pq);
int n = 0;
QueueNode* cur = pq->phead;
while (cur)
{
n++;
cur = cur->next;
}
return n;
}
//--------------------------------------------------------
int QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->ptail == NULL && pq->phead == NULL;
}
栈与队列练习题
接下来,我们来做几道有关队列的题目:
使用队列实现栈
用两个堆的基础操作模拟出栈的基础操作。
我们先要重新梳理一下队列与栈的区别,栈是后进先出,队列是先进先出。
所以在元素进入的时候它们是类似的,但在取出元素的时候,队列取的是队头,栈取的是队尾。
那么我们如何使用两个队列,完成后进先出:
可以看出来:
- 初始一个队列有数据,一个队列没数据
- 入数据向不为空的那个入
- 出数据,不为空的队列的前size-1个倒入到另一个队列,Pop掉最后一个一个的数据 ,这个数据就是模拟栈的删除。
如果我们想继续模拟栈的删除,只需要重复上述步骤:
我们已经了解到了模拟栈的删除,实际上,其他的功能就和原本栈差不太多了,在之后展示代码的过程中会简单讲解一下,
代码部分:
- 引入栈,定义模拟栈的结构,这里我们使用一个结构体包含两个队列:
typedef struct {
Queue q1;
Queue q2;
} MyStack;
- 初始化
MyStack* myStackCreate() {
MyStack*pst=(MyStack*)malloc(sizeof(MyStack));
QueueInit(&pst->q1);
QueueInit(&pst->q2);
return pst;
}
- 入“栈”,无返回值,我们选择不空的那个队列插入元素
void myStackPush(MyStack* obj, int x) {
assert(obj);
if(!QueueEmpty(&obj->q1)){
QueuePush(&obj->q1,x );
}
else
{
QueuePush(&obj->q2,x);
}
}
- 出“栈”,并且有返回值。按照之前的思路删除元素
int myStackPop(MyStack* obj) {
Queue*emptyQ=&obj->q1;
Queue*nonemptyQ=&obj->q2;
if(!QueueEmpty(&obj->q1))
{
emptyQ=&obj->q2;
nonemptyQ=&obj->q1;
}
//将非空队列中的n-1个数字倒入空队列
while(QueueSize( nonemptyQ)>1)
{
QueuePush(emptyQ,QueueFront(nonemptyQ));
QueuePop(nonemptyQ);
}
//将非空队列中的最后一个元素删除,这个数据相当于使栈顶的数据
int top=QueueFront(nonemptyQ);
QueuePop(nonemptyQ);
return top;
}
- 取栈顶元素,从不空的队列中取出队尾元素,也就是栈顶元素
这里我们要注意辨析一下,栈顶尾和队列头尾的 区别:
所以,一般来说,栈顶对应的是队尾。
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
- 判空
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
}
- 释放空间,
void myStackFree(MyStack* obj) {
assert(obj);
QueueDestory(&obj->q1);
QueueDestory(&obj->q2);
free(obj);
}
接下来贴出来所有代码:
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QueueNode;
typedef struct Queue
{
QueueNode* phead;
QueueNode* ptail;
}Queue;
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->ptail == NULL && pq->phead == NULL;
}
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
}
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->phead;
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
if (newNode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newNode->data = x;
newNode->next = NULL;
if (pq->ptail == NULL)
{
pq->phead = pq->ptail = newNode;
}
else
{
pq->ptail->next = newNode;
pq->ptail = newNode;
}
}
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->phead->next == NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QueueNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
}
int QueueSize(Queue* pq)
{
assert(pq);
int n = 0;
QueueNode* cur = pq->phead;
while (cur)
{
n++;
cur = cur->next;
}
return n;
}
int QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
MyStack*pst=(MyStack*)malloc(sizeof(MyStack));
QueueInit(&pst->q1);
QueueInit(&pst->q2);
return pst;
}
void myStackPush(MyStack* obj, int x) {
assert(obj);
if(!QueueEmpty(&obj->q1)){
QueuePush(&obj->q1,x );
}
else
{
QueuePush(&obj->q2,x);
}
}
int myStackPop(MyStack* obj) {
Queue*emptyQ=&obj->q1;
Queue*nonemptyQ=&obj->q2;
if(!QueueEmpty(&obj->q1))
{
emptyQ=&obj->q2;
nonemptyQ=&obj->q1;
}
//将非空队列中的n-1个数字倒入空队列
while(QueueSize( nonemptyQ)>1)
{
QueuePush(emptyQ,QueueFront(nonemptyQ));
QueuePop(nonemptyQ);
}
//将非空队列中的最后一个元素删除,这个数据相当于使栈顶的数据
int top=QueueFront(nonemptyQ);
QueuePop(nonemptyQ);
return top;
}
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
assert(obj);
QueueDestory(&obj->q1);
QueueDestory(&obj->q2);
free(obj);
}
/**
* Your MyStack struct will be instantiated and called as such:
* MyStack* obj = myStackCreate();
* myStackPush(obj, x);
* int param_2 = myStackPop(obj);
* int param_3 = myStackTop(obj);
* bool param_4 = myStackEmpty(obj);
* myStackFree(obj);
*/
设计循环队列
对于这一道题,我们既可以使用循环链表,也可以使用数组来解决,在这里,我们暂时使用数组。
对于循环队列,主要有两个问题
- 如何实现“循环”
- 对于队空和队满的判断方法
如图,我们不能实现物理上的连接,但是我们可以通过指针的 的移动来实现,当head或者tail移动到数组尾,下次移动时就跳转到数组首。
接下来我们来研究一下,如何判空,判满。
- 判空 ,显然front==rear的时候,队列中是没有元素的
2.判满,这个时候如果我们依旧使用front=rear来判满的话,就与队列空的时候无法区分了。
所以我们要采用其他方式:我们每次只使用数组n-1的空间,留下一个空间不去使用,这样当rear+1=front的时候,我们就认定数组已经有n-1个元素了,也就是队列满。
同时,对于循环队列,我们要清楚与普通队列的区别:
- 节点是一开始就要创建好的
- 出数据,是head指针往后走,节点不删除
- 入数据,是tail指针往后走,不申请节点
接下来,我们开始代码实现;
- 定义结构体:
typedef struct {
int*a; //数组
int front;//队头指针
int rear; //队尾指针
int k; //数组大小
} MyCircularQueue;
- 判满
bool myCircularQueueIsFull(MyCircularQueue* obj) {
assert(obj);
return (obj->rear+1)%(obj->k+1)==obj->front;
}
- 判空
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
assert(obj);
return obj->front==obj->rear;
}
- 初始化,这里我们指定一个k值作为有效空间值,而实际的数组大小为k+1
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue*q=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
q->a=(int*)malloc(sizeof(int)*(k+1));
q->front=0;
q->rear=0;
q->k=k;
return q;
}
- 入队列
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
assert(obj);
if(myCircularQueueIsFull(obj))
return false;
obj->a[obj->rear]=value;
obj->rear++;
(obj->rear)%=(obj->k+1);
return true;
}
- 出队列
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
assert(obj);
if(myCircularQueueIsEmpty(obj))
{
return false;
}
++obj->front;
obj->front%=(obj->k+1);
return true;
}
- 取队头
int myCircularQueueFront(MyCircularQueue* obj) {
assert(obj);
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->a[obj->front];
}
- 取队尾,由于rear指向那个空空间,所以真正的队尾元素在rear之前的位置。
int myCircularQueueRear(MyCircularQueue* obj) {
assert(obj);
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
int prevRear=obj->rear-1;
if(obj->rear==0)
prevRear=obj->k;
return obj->a[prevRear];
}
- 释放空间
void myCircularQueueFree(MyCircularQueue* obj) {
assert(obj);
free(obj->a);
free(obj);
}