目录
[1. 栈](#1. 栈)
[1.1 概念和结构](#1.1 概念和结构)
[1.2 代码实现:](#1.2 代码实现:)
[1.2.1 Stack.h 头文件](#1.2.1 Stack.h 头文件)
[1.2.2 Stack.c 源文件](#1.2.2 Stack.c 源文件)
[1.2.3 栈初始化注意事项:](#1.2.3 栈初始化注意事项:)
[2. 队列](#2. 队列)
[2.1 概念与结构](#2.1 概念与结构)
[2.2 代码实现](#2.2 代码实现)
[2.2.1 Queue.h 头文件](#2.2.1 Queue.h 头文件)
[2.2.2 Queue.c 源文件](#2.2.2 Queue.c 源文件)
[2.2.3 代码注意事项](#2.2.3 代码注意事项)
正文开始:
1. 栈
1.1 概念和结构
栈:⼀种特殊的线性表,其只允许在固定的⼀端进⾏插⼊和删除元素操作。进⾏数据插⼊和删除操作的⼀端称为栈顶,另⼀端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
**压栈:**栈的插⼊操作叫做进栈 / 压栈 / 入栈,⼊数据在栈顶。
出栈 :栈的删除操作叫做出栈。出数据也在栈顶。

栈的实现⼀般可以使用数组或者链表实现,相对而言数组的结构实现更优⼀些。因为数组在尾上插⼊数据的代价⽐较⼩。
1.2 代码实现:
1.2.1 Stack.h 头文件
cpp
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a; //a是指向动态数组的指针
int top;
int capacity;
}ST;
// 初始化和销毁
void STInit(ST* pst);
void STDestroy(ST* pst);
// 入栈 出栈
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
// 取栈顶数据
STDataType STTop(ST* pst);
// 判空
bool STEmpty(ST* pst);
// 获取数据个数
int STSize(ST* pst);
1.2.2 Stack.c 源文件
cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"
// 初始化和销毁
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
// top指向栈顶数据的下一个位置
pst->top = 0;
// top指向栈顶数据
//pst->top = -1;
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 : pst->capacity * 2;
STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
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);
assert(pst->top > 0);
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;
}
1.2.3 栈初始化注意事项:
cpp
// 设计1:top指向栈顶元素的下一个位置
// 初始时:top = 0
// 空栈条件:top == 0
// 栈中元素数量 = top
typedef struct Stack {
STDataType* a;
int top; // top位置即下一个要插入的位置
int capacity;
}ST;
// 设计2:top指向栈顶元素的位置
// 初始时:top = -1
// 空栈条件:top == -1
// 栈中元素数量 = top + 1
typedef struct Stack {
STDataType* a;
int top; // 当前栈顶元素的位置
int capacity;
}ST;
可视化理解:
cpp
初始栈: push(10) push(20) pop()
↓ ↓ ↓
top=0 top=1 top=2 top=1
数组: [] [10] [10,20] [10,20]
↑ ↑ ↑
top=1 top=2 top=1(20被逻辑移除)
尝试pop()时:
- top=1 → 成功(top变为0)
- 再次pop()时:top=0 → assert触发(top > 0 为假)
2. 队列
2.1 概念与结构
概念:只允许在⼀端进⾏插⼊数据操作,在另⼀端进⾏删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)
**入队列:**进⾏插⼊操作的⼀端称为队尾
出队列:进⾏删除操作的⼀端称为队头

队列也可以数组和链表的结构实现,使⽤链表的结构实现更优⼀些,因为如果使⽤数组的结构,出队列在数组头上出数据,效率会⽐较低。
2.2 代码实现
2.2.1 Queue.h 头文件
cpp
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType val;
}QNode;
typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
// 队尾插入
void QueuePush(Queue* pq, QDataType x);
// 队头删除
void QueuePop(Queue* pq);
// 取队头和队尾的数据
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);
bool QueueEmpty(Queue* pq);
//// 队尾插入
//void QueuePush(QNode** pphead, QNode** pptail, QDataType x);
//// 队头删除
//void QueuePop(QNode** pphead, QNode** pptail);
2.2.2 Queue.c 源文件
cpp
#define _CRT_SECURE_NO_WARNINGS
#include"Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = NULL;
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, QDataType x)
{
assert(pq);
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);
/*QNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
if (pq->phead == NULL)
pq->ptail = NULL;*/
// 一个节点
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--;
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->phead);
return pq->phead->val;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->ptail);
return pq->ptail->val;
}
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->size == 0;
}
2.2.3 代码注意事项
一般来讲,队列的插入代码,可以以如下形式声明设计:(按设计需求,灵活变通)
cpp
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType val;
}QNode;
//队尾插入
void QueuePush(QNode** pphead, QNode** pptail, QDataType x);
//队头删除
void QueuePop(QNode** pphead, QNode** pptail);
但是:此声明形式需要传的参数较多,且需要传的是较为复杂的二级指针才能满足需求。
所以我们可换一种形式,即定义两个结构体,一个结构体定义一个一个的结点构成队列,又因为队列的设计需求需要两个指针phead、ptail实现其功能,所以我们可再定义一个结构体包含这两个指针,从而可达成只需传一个参数的目的,而且只需要传这个结构体的一级指针,就可实现对其包含在内的phead、ptail指针进行修改,既避免了传多个参数,又避免了传二级指针,一箭双雕简化代码。
cpp
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType val;
}QNode;
typedef struct Queue
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
// 队尾插入
void QueuePush(Queue* pq, QDataType x);
// 队头删除
void QueuePop(Queue* pq);