文章目录
栈和队列
1. 栈
1.1 栈的概念及结构
- 栈是一种特殊的线性表,其只允许在固定的一端进行插入和删除操作。
- 进行数据插入和删除操作的一段被称为栈顶,另一端则被称为栈底。
- 栈中的数据元素遵循后进先出(Last In Fist Out)的原则。
- 栈的插入操作被称为进栈/压栈/入栈,入数据在栈顶。
- 栈的删除操作叫做出栈,出数据也在栈顶。
1.2 栈的实现
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
1.2.1 声明
//由于静态栈实用性较低,本文实现的是动态栈
typedef int STDataType;//类型重定义,增加代码的灵活性,便于修改
typedef struct Stack
{
STDataType* a;
int capacity;
int top;// 当前栈顶的位置
}ST;
//初始化
void StackInit(ST* ps);
//插入
void StackPush(ST* ps, STDataType x);
//删除
void StackPop(ST* ps);
//获取栈顶元素
STDataType StackTop(ST* ps);
//销毁
void StackDestory(ST* ps);
//判空
bool StackEmpty(ST* ps);
//数据个数
int StackSize(ST* ps);
1.2.2 实现
- 初始化栈
//初始化
void StackInit(ST* ps)
{
assert(ps);
//1. 开空间
//
//2. 不开空间
ps->a = NULL;
ps->capacity = ps->top = 0;
}
- 销毁栈
//销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
- 入栈
//插入
void StackPush(ST* ps, STDataType x)
{
assert(ps);
//容量不足,扩容
if (ps->capacity == ps->top)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
if (!tmp)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
//插入
ps->a[ps->top] = x;
ps->top++;
}
- 出栈
//删除
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
- 获取栈顶元素
//访问栈顶数据
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
- 检测栈是否为空
//判空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
- 获取栈中有效元素个数
//数据个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
2. 队列
2.1 队列的结构及概念
- 队列是只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。
- 遵循先进先出FIFO(First In First Out) 的原则。
- 入队列:进行插入操作的一端称为队尾 。
- 出队列:进行删除操作的一端称为队头。
2.2 队列的实现
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
2.2.1 声明
typedef int QDateType;
//结点
typedef struct QueueNode
{
QDateType data;
struct QueueNode* next;
//int size;
}QNode;
//封装的结点指针
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;
//初始化
void QueueInit(Queue* pq);
//销毁
void QueueDestory(Queue* pq);
//插入
void QueuePush(Queue* pq, QDateType x);
//删除
void QueuePop(Queue* pq);
//取头部数据
QDateType QueueFront(Queue* pq);
//取尾部数据
QDateType QueueBack(Queue* pq);
//判空(容量)
bool QueueEmpty(Queue* pq);
//大小
int QueueSize(Queue* pq);
2.2.2 实现
- 初始化队列
//初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
- 销毁队列
//销毁
void QueueDestory(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* del = cur;
cur = cur->next;
free(del);
}
pq->head = pq->tail = NULL;
}
- 队尾入队列
//插入
void QueuePush(Queue* pq, QDateType x)
{
assert(pq);
//开辟新节点
QNode* newNode = (QNode*)malloc(sizeof(QNode));
if (newNode == NULL)
{
perror("malloc fail");
exit(-1);
}
else
{
newNode->data = x;
newNode->next = NULL;
}
//插入
if (pq->tail == NULL)
{
pq->head = pq->tail = newNode;
}
else
{
pq->tail->next = newNode;
pq->tail = newNode;
}
}
- 队头出队列
//删除
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* del = pq->head;
pq->head = pq->head->next;
free(del);
}
}
- 获取队列头部元素
//取头部数据
QDateType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
- 获取队列队尾元素
//取尾部数据
QDateType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
- 检测队列是否为空
//判空(容量)
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL && pq->tail == NULL;
}
- 获取队列中有效元素个数
//大小
int QueueSize(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
int n = 0;
while (cur)
{
n++;
cur = cur->next;
}
return n;
}
3. 栈和队列OJ题
尝试使用栈和队列挑战下面的OJ题吧: