C++数据结构——链栈(基本代码实现与案例)

2024-04-17 04:38

本文主要是介绍C++数据结构——链栈(基本代码实现与案例),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一:基本代码实现

#include<iostream>
using namespace std;enum error_code {success, underflow
};struct node
{int data;node* next; // 指向下一结点
};class linkStack {
public:linkStack();~linkStack();bool empty()const;error_code get_top(int& x)const;error_code push(const int x);error_code pop();
private:int count;node* top; // 指向自己
};linkStack::linkStack()
{count = 0;top = NULL;
}linkStack::~linkStack()
{while (!empty()) pop();
}bool linkStack::empty()const {return count == 0;
}error_code linkStack::get_top(int& x)const
{if (empty()) return underflow;x = top->data;return success;
}error_code linkStack::push(const int x)
{node* s = new node;s->data = x;s->next = top;top = s;count++;return success;
}error_code linkStack::pop()
{if (empty()) return underflow;node* s = new node;s = top;top = s->next;delete s;count--;return success;
}bool ReferenceError(error_code a)
{if(a == underflow){cout << "underflow!" << endl;return false;}return success;
}int main()
{linkStack s;int top;for(int i = 0; i < 10; i++) // 入栈 ReferenceError(s.push(i));for(int i = 0; i < 3; i++) // 出栈 ReferenceError(s.pop());ReferenceError(s.get_top(top)); // 取栈顶cout << "栈顶元素:" << top << endl; return 0;
}

二:链栈案例

  • 案例一:设计算法将十进制转换为八进制
  • 案例二:符号"{“,”}“,”[“,”]“,”(“,”)"应该是互相匹配的,设计算法对以字符串形式读取的表达式S,判断其中的各括号是否是匹配的
  • 案例三:使用栈结构写一个能计算带括号的四则表达式表达式的计算器
#include<iostream>
using namespace std;enum error_code {success, underflow
};template <class T>
struct node
{T data;node<T>* next;
};template <class T>
class linkStack {
public:linkStack();~linkStack();bool empty()const;error_code getTop(T& x)const;error_code push(const T x);error_code pop();
private:T count;node<T>* top;
};template <class T>
linkStack<T>::linkStack()
{count = 0;top = NULL;
}template <class T>
linkStack<T>::~linkStack()
{while (!empty()) pop();
}template <class T>
bool linkStack<T>::empty()const {return count == 0;
}template <class T>
error_code linkStack<T>::getTop(T& x)const
{if (empty()) return underflow;x = top->data;return success;
}template <class T>
error_code linkStack<T>::push(const T x)
{node<T>* s = new node<T>;s->data = x;s->next = top;top = s;count++;return success;
}template <class T>
error_code linkStack<T>::linkStack::pop()
{if (empty()) return underflow;node<T>* s = new node<T>;s = top;top = s->next;delete s;count--;return success;
}bool ReferenceError(error_code a)
{if (a == underflow) {cout << "underflow!" << endl;return false;}return success;
}// 十进制转八进制
void transform()
{// 初始化题目条件 int num, result = 0;cout << "请输入要转化为八进制的十进制数:";cin >> num;// 初始化栈结构 linkStack<int>s;int top; // 开始运算 while(num != 0){s.push(num % 8);num /= 8;}while(!s.empty()){s.getTop(top);s.pop();result = result * 10 + top; }cout << "转成八进制为:" << result << endl;
}// 检验符号匹配
string symbol()
{// 初始化题目条件string s;cout << "请输入表达式S: ";cin >> s;int length;length = s.length();// 初始化栈结构linkStack<char>a;char top;// 开始运算for(int i = 0; i < length; i++){if(s[i] == '(' || s[i] == '[' || s[i] == '{')a.push(s[i]);if(s[i] == ')'){a.getTop(top);if(top == '(')a.pop();elsereturn "不匹配!!";}if(s[i] == ']'){a.getTop(top);if(top == '[')a.pop();elsereturn "不匹配!!";}if(s[i] == '}'){a.getTop(top);if(top == '{')a.pop();elsereturn "不匹配!!";} }return "匹配!!";
}// 计算器
int getRes(int a, int b, char c) // 计算
{  int result;switch(c){case '+': result = a + b;break;case '-': result = a - b;break;case '*': result = a * b;break;case '/': result = a / b;break;}return result;
}
int getPri(char a) // 获得优先级 
{if(a == ')')return 1; if(a == '+' || a == '-')return 2;if(a == '*' || a == '/')return 3;if(a == '(')return 4;elsecout << "输入错误!!" << endl;
}
void calculator()
{// 初始化题目条件string s;cout << "请输入计算式:";cin >> s;int i = 0, k = 1, num1, num2;char sign;// 初始化栈结构linkStack<int>num;int top_n;linkStack<char>oper;char top_o;// 开始计算if(s[(s.length() - 1)] != '='){cout << "算式请以 '=' 结尾";return;	} while(s[i] != '='){if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '(' || s[i] == ')'){l:if(!oper.empty()){oper.getTop(top_o);if(getPri(s[i]) > getPri(top_o) || top_o == '(')oper.push(s[i]);else{oper.getTop(top_o);if(top_o != '('){num.getTop(top_n);num1 = top_n;num.pop();num.getTop(top_n);  num.pop();num.push(getRes(top_n, num1, top_o));oper.pop();oper.getTop(top_o);if(top_o == '('){oper.pop();i++;continue;	}goto l;  	}}}if(oper.empty())oper.push(s[i]); k = 1;}else{if(k == 0){num.getTop(top_n);num.pop();num.push(top_n * 10 + s[i] - 48);	}if(k == 1){num.push(s[i] - 48);k = 0;}}i++;}num.getTop(top_n);num1 = top_n;num.pop();num.getTop(top_n);oper.getTop(top_o);cout << "计算结果为:" << getRes(top_n, num1, top_o) << endl;
}int main()
{// 十进制转八进制transform();// 检验符号匹配cout << symbol() << endl;// 计算器calculator();return 0;
}

更多相关内容大家可以前往我的个人博客浏览:eyes++的个人空间

这篇关于C++数据结构——链栈(基本代码实现与案例)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/910800

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja