1.栈
1.1 栈的概念
栈是一种特殊的线性表,不过他很独特,他只能从固定一端输入和删除数据,遵循后入先出的原则(通俗理解就是后来者居上),我们把输入和删除的一端称为栈顶,另一端则称为栈底。
压栈:插入数据叫入栈,插入数据在栈顶。
栈顶:删除数据叫出栈,删除数据也在栈顶。
栈的底层实现是什么?
栈的底层可以是数组,也可以是链表。不过相对于链表,数组的时间和空间复杂度都更低,所以一般使用数组。
1.2 栈的实现
Stack.h 文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* arr;
int top;//栈顶(相当于size)
int capacity;//栈的大小
}ST;
//初始化
void StackInit(ST* st);
//销毁
void StackDestroy(ST* st);
//入栈
void StackPush(ST* st, STDataType x);
//出栈
void StackPop(ST* st);
//判断栈顶为空
bool StackEmpty(ST* st);
//获取栈顶元素
STDataType Stacktop(ST* st);
//获取栈顶元素有效个数
int StackSize(ST* st);
Stack.c 文件
#include "stack.h"
//初始化
void StackInit(ST* ps)
{
ps->arr = NULL;
ps->top = ps->capacity = 0;
}
//销毁
void StackDestroy(ST* ps)
{
assert(ps);
if (ps->arr != NULL)
{
free(ps->arr);
ps->arr = 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 : 2 * ps->capacity;
STDataType* temp = realloc(ps->arr, newcapacity * sizeof(STDataType));
if (temp == NULL)
{
perror("realloc fail!");
return -1;
}
else//扩容成功
{
ps->arr = temp;
ps->capacity = newcapacity;
}
}
//空间够了,进行入栈
ps->arr[ps->top] = x;
ps->top++;
}
//出栈
void StackPop(ST* ps)
{
assert(ps);
if (!StackEmpty(ps))
{
ps->top--;
}
}
//判断栈顶为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//获取栈顶元素
STDataType Stacktop(ST* ps)
{
assert(ps);
return ps->arr[ps->top-1];
}
//获取栈顶元素有效个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
test.c 文件
#include "stack.h"
void test()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
StackPush(&st, 4);
StackPush(&st, 5);
StackPop(&st);
//StackPop(&st);
//StackPop(&st);
//StackPop(&st);
//while (!StackEmpty(&st))
//{
// STDataType ret = Stacktop(&st);
// printf("%d -> ", ret);//取栈顶元素
// StackPop(&st);//出栈
//}
STDataType ret = StackSize(&st);
printf("%d\n", ret);
StackDestroy(&st);
}
int main()
{
test();
return 0;
}
2. 队列
2.1 队列的概念
队列可以看作是栈的孪生兄弟,为什么这么说呢?这就跟队列的概念有联系了!
概念:只允许一端进行插入数据,另一端只允许进行删除数据的特殊的线性表,具有先进先出的特点。
入队列:插入数据的一端称为队尾;
出队列:删除数据的一端称为队头;
同样的道理,队列的实现的底层是什么呢?
如果使用链表我们可以知道队列的基本结构:
2.2 队列的实现
Queue.h 文件
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
QDataType data;
struct QueueNode* next;
}QueueNode;
typedef struct Queue
{
QueueNode* phead;
QueueNode* ptail;
int size;
}Queue;
//初始化
void QueueInit(Queue* pq);
//销毁
void QueueDestory(Queue* pq);
//队列判空
bool QueueEmpty(Queue* pq);
//入队列
void QueuePush(Queue* pq, QDataType x);
//队列有效元素个数
int QueueSize(Queue* pq);
//出队列
void QueuePop(Queue* pq);
//取队头元素
QDataType QueueFront(Queue* pq);
//取队尾元素
QDataType QueueBack(Queue* pq);
Queue.c 文件
#include "Queue.h"
//初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
//销毁
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* pcur = pq->phead;
while (pcur)
{
QueueNode* next = pcur->next;
free(pcur);
//pcur = NULL;
pcur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
//队列判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->phead == NULL;
}
//入队列
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
//申请新的结点
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
if (newnode == NULL)
{
perror("malloc fail!\n");
exit(1);
}
newnode->data = x;
newnode->next = NULL;
//空队列
if (pq->phead == NULL)
{
pq->phead = pq->ptail = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = pq->ptail->next;
}
pq->size++;
}
//队列有效元素个数
int QueueSize(Queue* pq)
{
assert(pq);
//QueueNode* pcur = pq->phead;
//int size = 0;
//while (pcur)
//{
// pcur = pcur->next;
// size++;
//}
//return size;
return pq->size;
}
//出队列
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
//只有一个结点时
if (pq->phead == pq->ptail)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QueueNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
//取队头元素
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
//取队尾元素
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
test.c 文件
#include "Queue.h"
void test()
{
Queue pq;
QueueInit(&pq);
QueuePush(&pq, 1);
QueuePush(&pq, 2);
QueuePush(&pq, 3);
QueuePush(&pq, 4);
QueuePush(&pq, 5);
//int ret = QueueSize(&pq);
//printf("%d\n", ret);
//QueuePop(&pq);
//QueuePop(&pq);
//QueuePop(&pq);
//QueuePop(&pq);
//QueuePop(&pq);
//int num = QueueFront(&pq);
//printf("%d\n", num);
//int num = QueueBack(&pq);
//printf("%d\n", num);
QueueDestory(&pq);
}
int main()
{
test();
return 0;
}