前言:在我自己的理解中,栈和队列是一种利用线性表实现的数据处理方法,并没有什么固定的语法,所以我们要学好栈和队列,应该熟悉掌握顺序表和链表等线性表。
1、栈
1.1、栈的概念以及结构
栈是一种特殊的线性表,是只能从一端插入数据和删除数据的线性表。我们把插入数据和删除数据的一端称为栈顶,另一端称为栈底,数据只能在栈顶得到处理,所以数据都一定遵循后进先出LIFO(Last In First Out)的原则。
- 压栈:插入数据的操作就称为压栈/进栈/入栈。 压栈在栈顶压栈。
- 出栈:删除数据的操作称为出栈。 出栈也在栈顶。
结构如图:
我们把他想象成一个罐子,往里面装东西之后,要拿东西的话只能先拿上面的部分,慢慢往下拿。
1.2、栈的实现
栈一般用链表或者数组实现,但是我们从栈的结构出发,使用数组实现更佳,因为尾插起来更加方便且效率更高。
因为定长的栈在实际环境中作用不大,所以我们使用动态的数组来实现栈。
代码如下:
#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 StackInit(ST* ps);
//销毁栈
void StackDestory(ST* ps);
//向栈底插入数据
void StackPush(ST* ps, STDataType x);
//删除数据
void StackPop(ST* ps);
//判断栈是否为空
bool StackEmpty(ST* ps);
//计算栈大小
int StackSize(ST* ps);
//栈顶数据出栈
STDataType StackTop(ST* ps);
#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
ps->a = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
if (ps->a == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
--ps->top;
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
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;
}
2、 队列
2.1、队列的概念以及结构
队列:队列和栈差不多是反着来的,只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头。
结构如下:
2.2、队列的实现
队列也是可以由数组或者链表来实现,但是用链表实现更优,因为不用频繁的挪数据。
实现结构图:
队列的实现和操作集:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
typedef int QDataType;
typedef struct QueueNode
{
QDataType data;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
bool QueueEmpty(Queue* pq);
size_t QueueSize(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
//队列初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
//队列销毁
void QueueDestory(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
//队列插入
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
assert(newnode);
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)
{
assert(pq->head == NULL);
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
//队列删除
void QueuePop(Queue* pq)
{
assert(pq);
assert(pq->head && pq->tail);
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}
//判断队列是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
//计算队列长度
size_t QueueSize(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
size_t size = 0;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}
//队头数据
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->head->data;
}
//队尾数据
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(pq->tail);
return pq->tail->data;
}
另外在队列中,还有一种复杂队列——循环队列。
循环队列是有固定大小的队列,
结构如图:
有点复杂,可能我也讲不透,就不再这里用代码一一实现了。
本篇就到这里,如有不正确的地方,欢迎读者指正!