本篇文章来详细介绍一下栈和队列,并且通过数组或链表来实现。
目录
1.栈
1.1栈的概念及结构
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO (Last ln First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
1.2栈的实现
栈的实现一般可以使用数组或者链表实现,相对而言,数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
数组实现栈:
单链表实现栈:
数组代码实现:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* data;
int top;
int capacity;
}Stack;
//初始化
void StackInit(Stack* pst);
//销毁
void StackDestroy(Stack* pst);
//入栈
void StackPush(Stack* pst, STDataType x);
//出栈
void StackPop(Stack* pst);
//获取栈顶元素
STDataType StackTop(Stack* pst);
//判空
bool StackEmpty(Stack* pst);
//获取个数
int StackSize(Stack* pst);
接口实现:
//初始化
void StackInit(Stack* pst)
{
assert(pst);
pst->data = NULL;
pst->top = 0;
pst->capacity = 0;
}
//销毁
void StackDestroy(Stack* pst)
{
assert(pst);
free(pst->data);
pst->data = NULL;
pst->top = pst->capacity = 0;
}
//入栈
void StackPush(Stack* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* ptr = (STDataType*)realloc(pst->data, newcapacity * sizeof(STDataType));
if (ptr == NULL)
{
perror("malloc fail");
return;
}
pst->data = ptr;
pst->capacity = newcapacity;
}
pst->data[pst->top] = x;
pst->top++;
}
//出栈
void StackPop(Stack* pst)
{
assert(pst);
assert(!StackEmpty(pst));
pst->top--;
}
//获取栈顶元素
STDataType StackTop(Stack* pst)
{
assert(pst);
assert(!StackEmpty(pst));
return pst->data[pst->top - 1];
}
//判空
bool StackEmpty(Stack* pst)
{
assert(pst);
/*if (pst->top == 0)
{
return true;
}
else
{
return false;
}*/
return pst->top == 0;
}
//获取个数
int StackSize(Stack* pst)
{
assert(pst);
return pst->top;
}
2.队列
2.1队列的概念及结构
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First ln First Out)。
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头
2.2队列的实现
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,数组头删效率会比较低。
链表实现队列:
入队出队示意图:
链表代码实现队列:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QueDataType;
typedef struct QueueNode
{
QueDataType data;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
//初始化
void QueueInit(Queue* pq);
//释放
void QueueDestroy(Queue* pq);
//入队
void QueuePush(Queue* pq, QueDataType x);
//出队
void QueuePop(Queue* pq);
//取队头
QueDataType QueueFront(Queue* pq);
//取队尾
QueDataType QueueBack(Queue* pq);
//判空
bool QueueEmpty(Queue* pq);
接口实现:
//初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
//释放
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->phead;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
//入队
void QueuePush(Queue* pq, QueDataType x)
{
assert(pq);
//创建新节点
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->next = NULL;
newnode->data = x;
//入队
if (pq->phead == NULL)
{
assert(pq->ptail == NULL);//防止头为空时,尾不为空
pq->ptail = pq->phead = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
pq->size++;
}
//出队
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
//1.一个节点
//2.多个节点
if (pq->phead->next == NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
//取队头
QueDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
//取队尾
QueDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
//判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->phead == NULL && pq->ptail == NULL;
}
另外扩展了解一下,实际中我们有时还会使用一种队列叫循环队列。如操作系统课程讲解生产者消费者模型时可以就会使用循环队列。环形队列可以使用数组实现,也可以使用循环链表实现。
Q.rear指向的是有效数据的下一个位置,在这时我们不能知道 Q.rear = Q.front 时,是队列满还是空,所以这里有两种解决办法来判断队满,图中采用的是第二种方法。
3.栈和队列面试题
1.括号匹配问题。OJ链接
2.用队列实现栈。OJ链接
3.用栈实现队列。OJ链接
4.设计循环队列。OJ链接
4.相关概念选择题
答案在最后
答案
本篇结束