目录
1. 栈的表示和实现
1.1. 栈的概念及结构
1.2. 栈的实现
1.3. 效果展示图
1.3.01 栈要实现的接口
1.3.02 栈的实现
1.3.03 栈的初始化
1.3.04 栈的销毁
1.3.05. 入栈
1.3.06 出栈
1.3.07 取栈顶数据
1.3.08 求有多少个数据
1.3.09 求是不是为空
1.4. 栈的源代码
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
#include"Queue.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);
}
StackDistory(&st);
}
void testQueue()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
printf("%d ", QueueFront(&q));
QueuePop(&q);
QueuePush(&q, 3);
QueuePush(&q, 4);
printf("%d ", QueueFront(&q));
QueuePop(&q);
QueuePush(&q, 5);
while (!QueueEmpty(&q))
{
printf("%d ",QueueFront(&q));
QueuePop(&q);
}
QueueDestory(&q);
}
int main()
{
testStack();
//testQueue();
return 0;
}
Stack.h
#pragma once
#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
#include<assert.h>
#include<stdlib.h>
typedef int STDateType;
typedef struct Stack
{
//动态数组存数据
STDateType* a;
//目前数据个数
int top;
//总容量
int capacity;
}ST;
//进栈 出栈 初始化 销毁 取栈顶数据 多少个数据 判断
//初始化
void StackInit(ST* ps);
//销毁
void StackDistory(ST* ps);
//入栈
void StackPush(ST* ps, STDateType x);
//出栈
void StackPop(ST* ps);
//取栈顶数据
STDateType 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 = (STDateType*)malloc(sizeof(STDateType)*4);
ps->capacity = 4;
//这里是初始化的0,也就是说永远指向栈顶的下一个位置
ps->top = 0;
}
//销毁
void StackDistory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
//入栈
void StackPush(ST* ps, STDateType x)
{
assert(ps);
//满了增容
if (ps->capacity == ps->top)
{
STDateType* newa = (STDateType*)realloc( ps->a, sizeof(STDateType) * ps->capacity * 2);
if (newa == NULL)
{
printf("realloc fail\n");
exit(-1);
}
else
{
ps->capacity *= 2;
ps->a = newa;
}
}
ps->a[ps->top] = x;
ps->top++;
}
//出栈
void StackPop(ST* ps)
{
assert(ps);
//栈空了调用直接报错
assert(ps->top > 0);
ps->top--;
}
//取栈顶数据
STDateType 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;
}
2. 队列的表示和实现
2.1 队列的概念及结构
2.2 队列的实现
2.4. 队列的效果示意图
2.5.01. 队列要实现的接口
2.5.02. 队列的实现
2.5.03. 队列的初始化
2.5.04. 队列的销毁
2.5.06. 队列的插入
2.5.07. 队列的删除
2.5.08. 得到队头数据
2.5.09. 得到队尾数据
2.5.10. 得到队列数据个数
2.5.11. 判断队列是否为空
2.6. 队列源代码
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
#include"Queue.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);
}
StackDistory(&st);
}
void testQueue()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
QueuePush(&q, 2);
printf("%d ", QueueFront(&q));
QueuePop(&q);
QueuePush(&q, 3);
QueuePush(&q, 4);
printf("%d ", QueueFront(&q));
QueuePop(&q);
QueuePush(&q, 5);
while (!QueueEmpty(&q))
{
printf("%d ",QueueFront(&q));
QueuePop(&q);
}
QueueDestory(&q);
}
int main()
{
//testStack();
testQueue();
return 0;
}
Queue.c
#pragma once
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
#include<malloc.h>
#include<stdlib.h>
typedef int QDateType;
typedef struct QueueNode
{
struct QueueNode* next;
QDateType date;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;
//初始化
void QueueInit(Queue* pq);
//销毁
void QueueDestory(Queue* pq);
//插入
void QueuePush(Queue* pq, QDateType x);
//删除
void QueuePop(Queue* pq);
//得到头部数据
QDateType QueueFront(Queue* pq);
//得到尾部数据
QDateType QueueBack(Queue* pq);
//得到大小
int QueueSize(Queue* ps);
//判空
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 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, QDateType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
newnode->date = x;
newnode->next = NULL;
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
if (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);
//1个数据
//多个
if (pq->head->next == NULL)
{
//一个数据如果不分情况
//free后tail是野指针再插入就会有问题
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}
//得到头部数据
QDateType QueueFront(Queue* pq)
{
assert(pq->head);
return pq->head->date;
}
//得到尾部数据
QDateType QueueBack(Queue* pq)
{
assert(pq->head);
return pq->tail->date;
}
//得到大小
int QueueSize(Queue* pq)
{
assert(pq->head);
int sz = 0;
QNode* cur=pq->head;
while (cur)
{
sz++;
cur = cur->next;
}
return sz;
}
//判空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}