栈
栈的介绍
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
总之要记住的栈的数据是先进后出,只能取栈顶的数据。
例如我们需要往栈里面入int a[5] = {1,2,3,4,5};
最终会是这样:
那我们怎么出栈呢?
我们只能从栈顶出数据,所以先出5,最后出1,所以栈是先进后出。
总体思想
我们只用处理栈的尾部数据,那我们用什么数据类型来存储呢?是线性表还是链表呢?通过栈的性质,我们要灵活运用栈的尾(即栈顶),所以不适合用链表来实现,因为链表尾删还需要找前一个节点非常麻烦,而我们用顺序表去实现是非常合适的。所以我们用顺序表去实现栈。
代码实现
stack.h 头文件
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int STDataType;
//动态栈
typedef struct Stack
{
STDataType* a;
int top; //栈顶的位置
int capacity;//容量
}ST;
//接口
void StackInit(ST* ps);//初始化
void StackDestroy(ST* ps);//摧毁
void StackPush(ST* ps, STDataType x);//压栈
void StackPop(ST* ps);//出栈
STDataType StackTop(ST* ps);
int StackSize(ST* ps);//栈的大小
bool StackEmpty(ST* ps);//判断栈是否为空
stack.c 源文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"stack.h"
void StackInit(ST* ps)//初始化
{
assert(ps);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackDestroy(ST* ps)//摧毁
{
assert(ps);
ps->top = ps->capacity = 0;
free(ps->a);
ps->a = NULL;
}
void StackPush(ST* 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, sizeof(STDataType)* newCapacity);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity = newCapacity;
}
}
ps->a[ps->top++] = x;
}
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;
}
队列
队列的介绍
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出
FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头
总体思想
在队列的使用的过程中,我们只需要尾入头出,所以我们用链表去实现队列是非常合适的。因为链表的头删和尾删都是O(1),而且实现起来方法便,如果用顺序表的话头删是很复杂的,需要把后面的数全部覆盖到前一项。
代码实现
Queue.h头文件
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
//队列的性质:First in first out.先进先出
//出队口叫队头,入队口叫队尾
//用链式结构更优
//因为队列需要找尾进行入队,而链式结构找尾需要O(N)的时间复杂度
//所以我们可以直接定义一个尾指针指向队尾
typedef int QDataType;
//队列的一个节点
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QueueNode;
//真正的队列:
typedef struct Queue
{
QueueNode* head;//指向队头
QueueNode* tail;//指向队尾
}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);//队尾数据
int QueueSize(Queue* pq);//队列数据个数
bool QueueEmpty(Queue* pq);//队列是否为空
Queue.c源文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
void QueueInit(Queue* pq)//队列初始化
{
assert(pq);
pq->head = pq->tail = NULL;
//开始都为空
}
void QueueDestroy(Queue* pq)//摧毁队列
{
assert(pq);
QueueNode* cur = pq->head;
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
void QueuePush(Queue* pq, QDataType x)//入队
{
assert(pq);
//新开节点
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
else
{
newnode->data = x;
newnode->next = NULL;
}
//如果队列为空,头和尾相同
if (pq->head == NULL)
{
pq->head = pq->tail = newnode;
}
//不同,则将尾的下一个指向newnode,然后把newnode赋给tail
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
void QueuePop(Queue* pq)//出队
{
assert(pq);
assert(!QueueEmpty(pq));
QueueNode* next = pq->head->next;
free(pq->head);
pq->head = next;
if (pq->head == NULL)
{
pq->tail = NULL;
}
}
QDataType QueueFront(Queue* pq)//队头数据
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
QDataType QueueBack(Queue* pq)//队尾数据
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
int QueueSize(Queue* pq)//队列数据个数
{
int size = 0;
QueueNode* cur = pq->head;
while (cur != NULL)
{
size++;
cur = cur->next;
}
return size;
}
bool QueueEmpty(Queue* pq)//队列是否为空
{
assert(pq);
if (pq->head == NULL)
{
return true;
}
return false;
}