由于栈和队列结构逻辑较为简单,此文只做简单介绍并附上代码参考
1. 栈
1.1 栈的概念
一种特殊的线性表,只允许在固定一端进行插入和删除元素操作,进行数据插入和删除操作的一端称为栈顶 ,另一端称为栈底 ,栈中数据遵循后入先出的原则(Last In First Out)

1.2 栈的实现
栈的实现可以使用数组和链表结构,使用数组更优,因为数组在尾上插入删除数据上代价小一些
1.2.1 Stack.h
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
//初始化
void STInit(ST* pst);
//销毁
void STDestory(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
#include"Stack.h"
//初始化
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
//top赋值为0表示栈顶元素的下一个元素
pst->top = 0;
pst->capacity = 0;
}
//销毁
void STDestory(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("STPush::realloc fail");
return;
}
pst->capacity = newcapacity;
pst->a = tmp;
}
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 test.c
cpp
#include"Stack.h"
int main()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
printf("%d\n", STTop(&s));
//printf("%d\n", s.a[s.top]); 切忌这样访问 因为top不代表栈顶元素
while (!STEmpty(&s))
{
printf("%d ", STTop(&s));
STPop(&s);
}
STDestory(&s);
return 0;
}
2. 队列
2.1 队列概念
只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。进行插入操作的一端称为队尾 ,进行删除操作的一端称为队头 。队列遵循先入先出的原则(First In First Out)

2.2 队列的实现
队列也可以数组和链表的结构实现,使用链表结构更优,因为数组结构,出队列在数组头出数据,效率较低。
2.2.1 Queue.h
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#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;
}QueueNode;
//将头尾指针单独封装结构体 可避免使用二级指针 简化逻辑 还可减少函数参数
typedef struct Queue
{
QueueNode* phead;
QueueNode* ptail;
int size;
}Queue;
//初始化
void QueueInit(Queue* pq);
//队列尾插
void QueuePush(Queue* pq, QDataType x);
//队列头删
void QueuePop(Queue* pq);
//队列数据个数
int QueueSize(Queue* pq);
//取队头数据
QDataType QueueFront(Queue* pq);
//取队尾数据
QDataType QueueBack(Queue* pq);
//判空
bool QueueEmpty(Queue* pq);
//队列销毁
void QueueDestory(Queue* pq);
////以下形式也可使用 但由于使用二级指针 较为复杂 不推荐
//void QueuePush(QueueNode** pphead, QueueNode** pptail, QDataType x);
//void QueuePop(QueueNode** pphead, QueueNode** pptail);
2.2.2 Queue.c
cpp
#include"Queue.h"
//初始化
void QueueInit(Queue* pq)
{
pq->phead = NULL;
pq->ptail = NULL;
pq->size = 0;
}
//队列尾插
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
if (newnode == NULL)
{
perror("QueuePush::malloc fail");
return;
}
newnode->next = NULL;
newnode->val = x;
//队列无节点
if (pq->phead == NULL)
{
pq->phead = pq->ptail = newnode;
pq->size++;
}
//队列有节点
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
{
QueueNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
//队列数据个数
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
//取队头数据
QDataType QueueFront(Queue* pq)
{
assert(pq);
return pq->phead->val;
}
//取队尾数据
QDataType QueueBack(Queue* pq)
{
assert(pq);
return pq->ptail->val;
}
//判空
bool QueueEmpty(Queue* pq)
{
return pq->size == 0;
}
//队列销毁
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->phead;
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
2.2.3 test.c
cpp
#include"Queue.h"
int main()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
QueuePush(&q, 3);
QueuePush(&q, 4);
//QueuePop(&q);
while (!QueueEmpty(&q))
{
printf("%d ", QueueFront(&q));
QueuePop(&q);
}
printf("\n");
return 0;
}