- 一、pandas是什么?栈的概念及结构
- 二、栈的框架定义
- 三、栈的功能实现
- 1.栈的初始化
- 2.栈的销毁
- 3.入栈
- 4.出栈
- 5.取栈顶元素
- 6.栈的判空
- 7.计算栈的大小
- 四.总代码
- 1.Stack.h
- 2.Stack.c
- 3.Test.c
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端
称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
实现栈有两种结构:分别为数组结构和链式结构。相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
2.栈的销毁
//销毁
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
3.入栈
//入栈
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top==ps->capacity)
{
//刚开始是0,给四个容量,不是的话,扩展2倍
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
4.出栈
//出栈
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
//不用释放空间
//free(ps->a[ps->top]);
ps->top--;
}
5.取栈顶元素
// 获取栈顶元素
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
6.栈的判空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
7.计算栈的大小
//获取栈中有效元素个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
四.总代码
1.Stack.h
#pragma once #include2.Stack.c#include #include #include 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); //检测栈是否为空,如果为空返回非零结果,如果不为空返回0 bool StackEmpty(ST* ps); // 获取栈顶元素 STDataType StackTop(ST* ps); //获取栈中有效元素个数 int StackSize(ST* ps);
#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
//销毁
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
//入栈
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top==ps->capacity)
{
//刚开始是0,给四个容量,不是的话,扩展2倍
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
//出栈
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
// 获取栈顶元素
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
//获取栈顶号数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
3.Test.c
#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"
void TestStack()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
printf("%d ", StackTop(&st));
StackPop(&st);
printf("%d ", StackTop(&st));
StackPop(&st);
StackPush(&st, 4);
StackPush(&st, 5);
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("n");
}
int main()
{
TestStack();
return 0;
}



