一、栈
1、栈的概念及结构
- 栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出的原则。
- 压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
- 出栈:栈的删除操作叫做出栈,出数据也在栈顶。

2、栈的实现
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
如图所示:
0)准备工作
我们在这里使用动态数组来实现栈,首先需要创建三个文件:
Stack.h
:各种库函数头文件的声明,栈的结构体定义,以及各种方法的声明。
Stack.c
:方法的实现。
test.c
:方法的测试。
首先先实现我们的头文件Stack.h:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int SLDataType;
typedef struct Stack
{
SLDataType* a;//数组
int top; //栈顶
int capacity;
}ST;
//栈的初始化和销毁
void STInit(ST* pst);
void STDestroy(ST* pst);
//入栈 出栈
void STPush(ST* pst, SLDataType x);
void STPop(ST* pst);
//取栈顶元素
SLDataType STTop(ST* pst);
//栈的判空
bool STEmpty(ST* pst);
//获取数据个数
int STSzie(ST* pst);
1)初始化和销毁
注意:将top初始化为0,就说明其指向的是栈顶数据的下一个位置。
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0;
pst->capacity = 0;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
2)入栈
void STPush(ST* pst, SLDataType x)
{
assert(pst);
//扩容
if (pst->top = pst->capacity)
{
int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
SLDataType* tmp = (SLDataType*)realloc(pst->a, newcapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
接着在test.c文件中进行测试:
int main()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
STDestroy(&s);
return 0;
}
调试结果:
发现已经成功入栈!
3)出栈
void STPop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
pst->top--;
}
进行测试:
int main()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
STPop(&s);
STDestroy(&s);
return 0;
}
调试结果:
4)取栈顶元素
SLDataType STTop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
return pst->a[pst->top-1];
}
进行测试:
int main()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
printf("%d ", STTop(&s));
STPop(&s);
printf("%d ", STTop(&s));
STDestroy(&s);
return 0;
}
运行结果:
5)判空

bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
使用判空依次打印元素:
int main()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
while (!STEmpty(&s))
{
printf("%d ", STTop(&s));
STPop(&s);
}
STDestroy(&s);
return 0;
}
运行结果:
6)栈数据个数

void STSize(ST* pst)
{
assert(pst);
return pst->top;
}
进行测试:
int main()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
printf("%d\n", STSize(&s));
STDestroy(&s);
return 0;
}
运行结果:
二、队列
1、队列的概念及结构
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出。
- 入队列:进行插入操作的一端称为队尾
- 出队列:进行删除操作的一端称为队头
2、队列的实现
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
在这里我们使用单链表来实现队列。
如图所示:
0)准备工作
在这里我们需要创建三个文件:
Queue.h
:队列结构体的定义,库函数头文件的声明,方法的声明。
Queue.c
:方法的实现。
test.c
:方法的测试。
首先我们先来实现头文件Queue.h:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.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);
//判空
bool QueueEmpty(Queue* pq);
//获取数据个数
int QueueSize(Queue* pq);
1)初始化

void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
2)销毁

void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->phead;
while (pq->phead)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
3)队尾插入
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++;
}
接着在test.c中进行测试:
int main()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
return 0;
}
调试结果:
4)队头删除

void QueuePop(Queue* pq)
{
assert(pq);
//头删
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--;
}
再进行测试:
int main()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
QueuePop(&q);
return 0;
}
调试结果:
5)取队头和队尾数据

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 main()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
printf("%d\n", QueueFront(&q));
printf("%d\n", QueueBack(&q));
return 0;
}
运行结果:
6)队列判空

bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->size == 0;
}
使用判空方法从头开始依次打印队列数据:
int main()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
while (!QueueEmpty(&q))
{
printf("%d ", QueueFront(&q));
QueuePop(&q);
}
return 0;
}
运行结果:
7)队列数据个数

int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
再进行测试:
int main()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
printf("%d\n", QueueSize(&q));
return 0;
}
运行结果: