栈的简易实现

目录

栈的概念及结构

图示

分析

栈的实现

定义

初始化

销毁

压栈

出栈

判断栈是否为空

 数据个数

访问栈顶元素

头文件

测试文件 


栈的概念及结构

一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出的原则。

封闭了一端的线性表


压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶

 

图示

分析

栈是线性表,用动态数组实现更优。

需要动态开辟,需要记录有效元素个数、空间容量、还有栈顶位置。

可参考顺序表来进行理解顺序表的实现_i跑跑的博客-CSDN博客

栈的实现

定义

起始位置,栈顶位置(用下标进行操作),容量。

typedef struct Stack
{
	Datatype* a;
	int top;   //栈顶位置
	int capacity;   //容量
}ST;

初始化

起始位置置空,容量为0,栈顶在下标为0的位置。

//初始化
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

销毁

从起始位置开始释放掉,因为是连续开辟空间,释放起始位置即释放所有开辟的空间

容量和栈顶置0

//销毁
void StackDestory(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

压栈

断言后,首先得检查容量是否够用,不够需要realloc空间,空间不足时若=0,则给4个Datatype类型大小的空间,若不等于0,则给定已有空间的二倍的空间大小。

空间解决后,存入数据,并更新栈顶。

//压栈
void StackPush(ST* ps, Datatype x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		ps->a = realloc(ps->a, newcapacity*sizeof(Datatype));
		if (ps->a == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

出栈

要注意不仅要断言ps,还需要保证栈顶大于0,即栈内不为空。

因为开辟空间连续,不能单独释放,因此我们直接更新栈顶即可。

//出栈
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top>0);
	ps->top--;
}

判断栈是否为空

是空返回true,非空返回false

//判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	//if (ps->top > 0)
	//{
	//	return false;
	//}
	//else
	//{
	//	return true;
	//}
	return ps->top == 0;
}

 数据个数

数据个数直接为栈顶top的下标

//数据个数
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

访问栈顶元素

栈顶元素是栈顶的前一个,top-1即为栈顶元素的下标

//访问栈顶数据
Datatype StackTop(ST* ps)
{
	assert(ps);
	return ps->a[ps->top-1];
}

头文件

#pragma once

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

typedef int Datatype;

typedef struct Stack
{
	Datatype* a;
	int top;   //栈顶位置
	int capacity;   //容量
}ST;

void StackInit(ST* ps);

void StackDestory(ST* ps);

void StackPush(ST* ps,Datatype x);

void StackPop(ST* ps);

//判断栈是否为空
bool StackEmpty(ST* ps);

//数据个数
int StackSize(ST* ps);

//访问栈顶数据
Datatype StackTop(ST* ps);

测试文件 

#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);
	}

	StackDestory(&st);
}



int main()
{
	TestStack();
	return 0;
}

依旧是可以根据自己的需求来进行压栈和出栈,简易栈就实现啦!!

 

  • 19
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论
好的,以下是C++实现简易计算器的代码: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<int> numStack; // 存储数字的 stack<char> opStack; // 存储运算符的 string expression; // 表达式字符串 cin >> expression; int len = expression.length(); for (int i = 0; i < len; i++) { char ch = expression[i]; if (ch >= '0' && ch <= '9') { // 如果是数字,入数字 numStack.push(ch - '0'); } else { // 如果是运算符 if (opStack.empty() || ch == '(') { // 如果运算符为空或者当前运算符是左括号,直接入 opStack.push(ch); } else if (ch == ')') { // 如果当前运算符是右括号,弹出运算符中的元素直到遇到左括号 while (opStack.top() != '(') { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.pop(); // 弹出左括号 } else { // 如果当前运算符是加减乘除 while (!opStack.empty() && opStack.top() != '(' && ((ch == '*' || ch == '/') || (opStack.top() == '+' || opStack.top() == '-'))) { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.push(ch); } } } while (!opStack.empty()) { // 处理剩余的运算符 int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } cout << numStack.top() << endl; // 输出最终结果 return 0; } ``` 这个程序可以处理包含加减乘除和括号的表达式,但是没有考虑负数的情况。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今年依旧去年春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值