概念
栈是一种特殊的线性表,遵循先进后出(后进先出)的原则,只允许在固定的一端进行插入和删除,而这一端称为栈顶,另一端称为栈底

压栈:插入元素到栈顶,也称进栈、入栈

出栈:删除元素在栈顶。

注:操作系统的栈与数据结构的栈不同,但是都是后进先出,比如操作系统的栈后开辟的栈区先释放

思路和源代码
思路
顺序表比链表更容易操作尾插尾删,并且顺序表可以随机访问,开辟和释放内存也是更适合栈的实现,所以栈的实现是利用顺序表来实现的,即数组,malloc或realloc开辟的连续的空间。

栈的操作是在栈顶,所以我们实现接口也是着重实现栈顶元素的删除(出栈),在栈顶加载上元素(压栈),获取栈顶的元素,其余就是初始化栈、获取元素的个数、顺序表是否为空(空会影响到压栈和出栈,压栈是否需要扩容,出栈判断元素为空时断言防止越界访问)、顺序表的销毁这些接口的实现。

源代码
头文件(主题框架)
#include <stdio.h>
#include <stdlib.h>/realloc,free/
#include <assert.h>/assert/
#include <stdbool.h>/bool/
typedef int DataType;
typedef struct Stack
{
DataType* a;/动态内存开辟的顺序表/
int capicity;/容量/
int top;/栈顶下标+1/
}ST;
/初始化/
void StackInit(ST* ps);
/销毁/
void Destroy(ST* ps);
/压栈/
void StackPush(ST* ps, DataType x);
/出栈/
void StackPop(ST* ps);
/判断是否为空/
bool StackEmpty(ST* ps);
/栈顶元素/
DataType StackTop(ST* s);
/元素个数/
int StackSize(ST* ps);
源文件,各个接口的实现
结构体的初始化
/初始化/
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capicity = 0;
}
压栈
/压栈/
void StackPush(ST* ps,DataType x)
{
assert(ps);
if (ps->top == ps->capicity)/空间满或未开辟空间/
{
int newcapicity = ps->capicity == 0 ? 4 : ps->capicity * 2;/重新开辟空间,原来的2倍最好/
DataType* ptr = (DataType*)realloc(ps->a, sizeof(DataType) * newcapicity);
if (ptr == NULL)
{
perror(“realloc fail”);
exit(-1);
}
ps->a = ptr;
ps->capicity = newcapicity;/栈顶不变,容量变/
}
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;
}
获取栈顶元素
/栈顶元素/
DataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));/空为真,不断言,所以反逻辑后的断言/
return ps->a[ps->top-1];/top代表着栈顶的元素+1.比如top初始化为0,第一次压栈后top就从0变成了1/
}
获取元素个数
/元素个数/
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
销毁
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
test.c源文件(main函数所在的.c源文件)
#include “stack.h”
void test()
{
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");
Destroy(&st);

}
int main()
{
test();
return 0;
}
Stack.c各个接口的实现源文件的整合
#include “stack.h”
/初始化/
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capicity = 0;
}
/销毁/
void Destroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capicity = 0;
}
/压栈/
void StackPush(ST* ps,DataType x)
{
assert(ps);
if (ps->top == ps->capicity)/空间满或未开辟空间/
{
int newcapicity = ps->capicity == 0 ? 4 : ps->capicity * 2;/重新开辟空间,原来的2倍最好/
DataType* ptr = (DataType*)realloc(ps->a, sizeof(DataType) * newcapicity);
if (ptr == NULL)
{
perror(“realloc fail”);
exit(-1);
}
ps->a = ptr;
ps->capicity = newcapicity;/栈顶不变,容量变/
}
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;
}
/栈顶元素/
DataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));/空为真,不断言,所以反逻辑后的断言/
return ps->a[ps->top-1];/top代表着栈顶的元素+1.比如top初始化为0,第一次压栈后top就从0变成了1/
}
/元素个数/
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐