栈
栈的基本概念
栈是一种只允许数据
先进后出
的线性表,其在数据进行增减操作的一端为栈顶,另一端则为栈底
栈有压栈和出栈这两种基础操作:
- 压栈:把数据推入栈内的操作
- 出栈:把栈顶的数据推出栈外
下图即为栈的运作方式:
栈的实现方式
栈能够用顺序表与链表两种方式实现
- 链表实现:通过
头插头删
节点来实现入栈出栈的操作 - 顺序表实现:通过在数组最后的位置增加来模拟
因为顺序表的实现方式更为简易,所以本文采用顺序表实现
代码实现
初始化
//stack.h
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
栈的初始化
void StackInit(Stack* st)
{
st->data = (STDataType*)malloc(sizeof(Stack));
st->capacity = 4; // 设容量为4
st->top = 0; // 栈顶位置
}
栈的销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->capacity = ps->top = 0;
ps->a = NULL;
}
栈的插入与删除
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity) //如果数据量达上限则扩容
{
STDataType* temp = (STDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(STDataType));
if (!temp)
{
perror("StackPush");
return;
}
ps->capacity *= 2;
ps->a = temp;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--; //直接使栈顶位置下移,下次入栈则直接覆盖
}
获取栈顶、栈的大小
STDataType StackTop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top-1];
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
// 判断是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
队列
队列的概念
和栈相反,队列是一种
先进先出
的数据结构,只允许数据在表的一段进行插入,一段进行删除,和日常生活的排队是一个道理。
队头:允许删除的一端被称为队头
。
队尾:允许插入的一端被称为队尾
。
- 队列的运作方式
队列的实现
队列有两种存储表示,用链表的方式和用数组的方式实现。
因为数组实现效率较低,所以建议使用链表的方式实现。
链表实现:
- 头文件
//queue.h
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* _next;
QDataType _data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* _front;
QNode* _rear;
}Queue;
// 初始化队列
void QueueInit(Queue* q);
// 队尾入队列
void QueuePush(Queue* q, QDataType data);
// 队头出队列
void QueuePop(Queue* q);
// 获取队列头部元素
QDataType QueueFront(Queue* q);
// 获取队列队尾元素
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数
int QueueSize(Queue* q);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);
- 函数实现
#include "queue.h"
QNode* BuyListNode(QDataType x) //创造新节点
{
QNode* node = (QNode*)malloc(sizeof(QNode));
if(!node)
{
perror("malloc");
}
node->_data = x;
node->_next = NULL;
return node;
}
void QueueInit(Queue *q) //初始化链表
{
q->_front = q->_rear = NULL;
}
void QueuePush(Queue *q, QDataType data) //入队列
{
if(!q->_front) //分为链表为空与不为空的情况
{
q->_front = q->_rear = BuyListNode(data);
}
else
{
q->_rear->_next = BuyListNode(data);
q->_rear = q->_rear->_next;
}
}
void QueuePop(Queue *q) //出队列
{
assert(q);
if(!q->_front)
return;
QNode* cur = q->_front->_next;
free(q->_front);
q->_front = cur;
}
QDataType QueueFront(Queue *q) //获取对头元素
{
return q->_front->_data;
}
QDataType QueueBack(Queue *q) //获取队尾元素
{
return q->_rear->_data;
}
int QueueSize(Queue* q) //获取队列长度
{
int res = 0;
QNode* cur = q->_front;
while(cur)
{
res++;
cur = cur->_next;
}
return res;
}
int QueueEmpty(Queue* q) //判断队列是否为空
{
return !q->_front;
}
void QueueDestroy(Queue *q) //销毁队列
{
while(q->_front)
{
QNode* next= q->_front->_next;
free(q->_front);
q->_front = next;
}
}
- 测试
#include "queue.h"
void test()
{
Queue node;
QueueInit(&node);
QueuePush(&node, 1);
printf("%d ", QueueBack(&node));
printf("%d ", QueueFront(&node));
QueuePop(&node);
QueuePush(&node, 2);
QueuePush(&node, 3);
QueuePop(&node);
QueuePush(&node, 4);
QueuePush(&node, 5);
printf("%d ", QueueBack(&node));
printf("%d ", QueueFront(&node));
QueuePop(&node);
QueuePop(&node);
QueuePop(&node);
}
int main()
{
test();
return 0;
}
测试结果:
1 1 5 3