栈的基本操作

栈的基本概念:

  • 栈是只允许在一端进行插入或删除操作的线性表

栈的顺序结构:

  • 栈是一种操作受限于线性表,它也有两种存储方式。
  • 我们本次的操作是基于顺序表的结构来实现栈的基本功能。

栈的存储结构:

typedef struct Stack {
	STDataType* data;
	int top;		//栈顶
	int capacity;	//空间容量
}ST;

栈的初始化:

void StackInit(ST* ps) {//初始化栈
	assert(ps);
	ps->data = NULL;
	ps->top = 0;//ps->top=-1也可,先++
	ps->capacity = 0;
}

栈的销毁:

void StackDestroy(ST* ps) {//销毁栈
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->top = ps->capacity = 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 = realloc(ps->data, sizeof(STDataType)*newCapacity);
		if (tmp == NULL) {
			printf("realloc fail.\n");
			exit(-1);
		}
		ps->data = tmp;
		ps->capacity = newCapacity;
	}

	ps->data[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->data[ps->top - 1];

}

计算栈中的元素个数:

int StackSize(ST* ps) {
	assert(ps);

	return ps->top;
}

判断栈是否为空:

bool StackEmpty(ST* ps) {
	assert(ps);

	return ps->top == 0;
}

完整源码:

头文件:Stack.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int STDataType;
typedef struct Stack {
	STDataType* data;
	int top;		//栈顶
	int capacity;	//空间容量
}ST;

void StackInit(ST* ps);//初始栈
void StackDestroy(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

#include"Stack.h"
void StackInit(ST* ps) {//初始化栈
	assert(ps);
	ps->data = NULL;
	ps->top = 0;//ps->top=-1也可,先++
	ps->capacity = 0;
}
void StackDestroy(ST* ps) {//销毁栈
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->top = ps->capacity = 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 = realloc(ps->data, sizeof(STDataType)*newCapacity);
		if (tmp == NULL) {
			printf("realloc fail.\n");
			exit(-1);
		}
		ps->data = tmp;
		ps->capacity = newCapacity;
	}

	ps->data[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->data[ps->top - 1];

}
int StackSize(ST* ps) {
	assert(ps);

	return ps->top;
}
bool StackEmpty(ST* ps) {
	assert(ps);

	return ps->top == 0;
}

源文件:main.c

#include"Stack.h"

void TestStack1() {
	ST st;
	StackInit(&st);


	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);

	StackPop(&st);
	StackPop(&st);
	StackPop(&st);
	StackPop(&st);
	
	printf("%d", StackTop(&st));

	StackDestroy(&st);
}
void TestStack2()
{
	ST st;
	StackInit(&st);

	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);

	printf("%d ", StackTop(&st));
	StackPop(&st);
	printf("%d ", StackTop(&st));
	StackPop(&st);

	StackPush(&st, 5);
	StackPush(&st, 6);


	while (!StackEmpty(&st))
	{
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}

	StackDestroy(&st);
}

int main()
{
	TestStack1();

	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值