1. 栈的基础概念和结构
在线性表中,栈属于其中的一种特殊的线性表。它的特点在于数据只允许从固定的一端进行插入和删除操作。而进行数据插入和删除操作的一段称为栈顶,另一端称为栈底。
栈类比于一个杯子,想要装水只能从杯口倒入,喝水的时候也要从杯口倒出(正常情况下)
两个名词:压栈和出栈。入栈和出栈都是在栈顶(杯口)
压栈是指栈的数据插入操作,同时也可以成为进栈/入栈。
出栈是指删除操作。
栈的实现可以使用数组或者链表进行操作。
下面介绍动态增长栈的数据结构
这是头文件需要的函数声明
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int StDataType;
typedef struct StackF
{
StDataType* a;
int top;
int capacity;
}Stack;
//初始化栈结构
void StackInit(Stack* ps);
//压栈
void StackPush(Stack* ps, StDataType x);
//出栈
void StackPop(Stack* ps);
//读取栈顶数据
StDataType StackTop(Stack* ps);
//读取栈中有的数据数量
int StackSize(Stack* ps);
//判断是否为空
bool StackEmpty(Stack* ps);
//最后销毁栈的操作
void StackDestory(Stack* ps);
以下是函数实现的操作
#include"Stack.h"
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void STPush(Stack* ps, StDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity*2;
StDataType* tmp = (StDataType*)realloc(ps->a, newcapacity * sizeof(StDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top]=x;
ps->top++;
}
void StackPop(Stack* ps)
{
assert(ps);
ps->top--;
}
StDataType StackTop(Stack* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top-1];
}
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
void StackDestory(Stack* ps)
{
free(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
队列
队列也是线性表中的一种特殊结构。它的规则是只允许在一端进行插入操作,另一端进行删除数据操作的特殊线性表。队列是先进先出的特性。它也可以使用数组/链表来实现结构。
进行插入操作的一端是:队尾。
进行删除操作的一端是:队头。
以下是队列的结构头文件展示
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QueDatatype;
typedef struct QListNode
{
struct QListNode* next;
QueDatatype data;
}QNode;
typedef struct Queue
{
QNode* front;
QNode* back;
int size;
}Queue;
void QueueInit(Queue* q);
// 队尾入队列
void QueuePush(Queue* q, QueDatatype data);
// 队头出队列
void QueuePop(Queue* q);
// 获取队列头部元素
QueDatatype QueueFront(Queue* q);
// 获取队列队尾元素
QueDatatype QueueBack(Queue* q);
// 获取队列中有效元素个数
int QueueSize(Queue* q);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);
以下是实际结构函数的内容展示
#include"Queue.h"
void QueueInit(Queue* q)
{
assert(q);
q->front = NULL;
q->back = NULL;
q->size = 0;
}
bool QueueEmpty(Queue* q)
{
assert(q);
return q->size == 0;
}
void QueuePush(Queue* q, QueDatatype data)
{
assert(q);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("creat newnode error");
return;
}
newnode->next = NULL;
newnode->data = data;
if (q->back == NULL)
{
q->front = q->back = newnode;
}
else
{
q->back->next = newnode;
q->back = newnode;
}
q->size++;
}
void QueuePop(Queue* q)
{
assert(q);
assert(q->size != 0);
if (q->front->next == NULL)
{
free(q->front);
q->front = q->back = NULL;
}
else
{
QNode* next = q->front->next;
free(q->front);
q->front = next;
}
q->size--;
}
QueDatatype QueueFront(Queue* q)
{
assert(q);
assert(q->front);
return q->front->data;
}
QueDatatype QueueBack(Queue* q)
{
assert(q);
assert(q->back);
return q->back->data;
}
int QueueSize(Queue* q)
{
assert(q);
return q->size;
}
void QueueDestroy(Queue* q)
{
assert(q);
QNode* cur = q->front;
while (cur)
{
QNode* next = q->front->next;
free(q->front);
cur = next;
}
q->front = q->back = NULL;
q->size = 0;
}