栈的实现(动态)
在实现之前,我们要思考:
首先,这是一个线性表实现的,所以我们有两个选择:顺序表,也就是数组实现;或者链表实现。栈的实现 —— 数组好还是链表好?
其实我们栈的插入和删除相当于尾插尾删用数组的唯一缺陷就是不够的时候要增容
用单链表就不太好了:尾插尾删要找尾如果不找尾,用尾指针记录尾结点。但是即便这样,我们也要找到前一个,所以要不就使用双链表实现。如果一定想使用单链表,把把栈顶栈底反一下,用头插,不要用尾插即可
所以用单链表也是ok的如果用尾作栈顶,那么用双链表好
如果要用单链表实现,那么就用让头作栈顶
总和各方面的要素,使用数组(顺序表)实现是最合适的。
Stack.h
#pragma once #include#include #include #include 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); //删除数据 STDataType StackTop(ST* ps); //取栈顶的元素 bool StackEmpty(ST* ps); //判断栈是否为空 int StackSize(ST* ps); //获取栈有多少数据
Stack.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
//销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
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;
STDataType* tmp =(STDataType*) realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
printf("realloc failn");
exit(-1);
}
ps->capacity = newcapacity;
ps->a = tmp;
}
ps->a[ps->top] = x;
ps->top++;
}
//删除数据
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
//取栈顶的元素
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//获取栈有多少数据
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void TestStack()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
StackPush(&st, 4);
StackPush(&st, 5);
while (!StackEmpty(&st))
{
printf("%d", StackTop(&st));
StackPop(&st);
}
printf("n");
StackDestory(&st);
}
int main()
{
TestStack();
return 0;
}
队列
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有
先进先出
FIFO(First In First Out) 入队列:进行插入操作的一端称为
队尾
出队列:进行删除操作的一端称为
队头。只一端入数据,没必要用双向循环链表,使用单链表。
队列代码
Queue.h
#pragma once #include#include #include #include typedef int QDataType; typedef struct QueueNode { struct QueueNode* next; QDataType data; }QNode; typedef struct Queue { QNode* head; QNode* 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); //取队尾数据 bool QueueEmpty(Queue* pq); int QueueSize(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);
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));
if (newnode == NULL)
{
printf("malloc failn");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
//队头出数据
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty);
//一个节点
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;
}
}
//取队头数据
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty);
return pq->head->data;
}
//取队尾数据
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty);
return pq->tail->data;
}
//判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head = NULL;
}
//计数
int QueueSize(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
int size = 0;
while (cur)
{
++size;
cur = cur->next;
}
return size;
}
Test.c
#include"Queue.h"
#include "Queue.h"
void TestQueue()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
printf("%d ", QueueFront(&q));
QueuePop(&q);
printf("%d ", QueueFront(&q));
QueuePop(&q);
QueuePush(&q, 3);
QueuePush(&q, 4);
QueuePush(&q, 5);
while (!QueueEmpty(&q))
{
printf("%d ", QueueFront(&q));
QueuePop(&q);
}
printf("n");
}
int main()
{
TestQueue();
return 0;
}
队列的应用场景
1、排队,保持绝对的公平性
2、广度优先遍历 BFS



