1.栈
一、 栈的基本概念
栈(Stack)是一种遵循后进先出(Last-In-First-Out, LIFO)原则的线性数据结构。可以将它想象成一叠盘子:你只能从最上面取走盘子(后放的先拿走),也只能将新盘子放在最上面(先放的后拿走)。
栈有两个核心操作:
- 入栈 (Push): 将一个元素添加到栈的顶部。
- 出栈 (Pop): 移除并返回栈顶的元素。
通常还会提供以下辅助操作:
- 查看栈顶 (Peek/Top): 返回栈顶的元素但不移除它。
- 判空 (isEmpty): 检查栈是否为空。
- 获取栈大小 (Size): 返回栈中元素的数量。
栈的核心特性决定了其元素的添加和移除都只在栈的一端进行,这一端被称为栈顶 (Top),相对的另一端则称为栈底(Bottom)。
二、 栈的实现方式
栈作为一种抽象数据类型(ADT),可以使用不同的底层数据结构来实现。
1. 基于数组 (顺序存储)
- 原理 :使用一个固定大小或动态扩容的数组来存储栈元素。需要一个额外的变量(通常称为
top)来跟踪当前栈顶的位置(即数组索引)。 - 操作 :
- 初始化
- 入栈 (
push) - 出栈 (
pop) - 查看栈顶 (top)
- 判空 (
empty) - 大小 (
size)
- 优点 :
- 实现简单。
- 访问元素快(常数时间复杂度)。
- 缺点 :
- 需要预先分配固定大小的空间(可能导致空间浪费或栈溢出)。
- 动态扩容(如Python列表)可能涉及复制原有数组,有一定开销。
cpp
#include "Stack.h"
//初始化与销毁
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0; //top指向栈顶元素的下一个
pst->capacity = 0;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
//入栈 出栈
void STPush(ST* pst, STDataType x)
{
assert(pst);
//扩容
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
void STPop(ST* pst)
{
assert(pst);
pst->top--;
}
//取栈顶
STDataType STTop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
return pst->a[pst->top-1];
}
//判空
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
//元素个数
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
2.队列
一、队列的基本概念
队列是一种先进先出(FIFO)的线性数据结构,元素从队尾入队,从队头出队。队列的操作通常包括入队(enqueue)、出队(dequeue)、查看队头元素(peek)和判断队列是否为空。
二、队列的实现方式
链表实现
cpp
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = NULL;
pq->ptail = NULL;
pq->size = 0;
}
//队尾插入
void QueuePush(Queue* pq, QDataType x)
{
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail!");
return;
}
newnode->next = NULL;
newnode->val = x;
if (pq->ptail == NULL)
{
pq->phead = pq->ptail = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
pq->size++;
}
//队头删除
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->size != 0);
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;
}
//if (pq->phead == NULL)
// pq->ptail = NULL;
pq->size--;
}
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
//取队头队尾元素
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->phead);
return pq->phead->val;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->ptail->val);
return pq->ptail->val;
}
//判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return 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;
}