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

相关文章

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Docker部署Jenkins持续集成(CI)工具的实现

《Docker部署Jenkins持续集成(CI)工具的实现》Jenkins是一个流行的开源自动化工具,广泛应用于持续集成(CI)和持续交付(CD)的环境中,本文介绍了使用Docker部署Jenkins... 目录前言一、准备工作二、设置变量和目录结构三、配置 docker 权限和网络四、启动 Jenkins

Python3脚本实现Excel与TXT的智能转换

《Python3脚本实现Excel与TXT的智能转换》在数据处理的日常工作中,我们经常需要将Excel中的结构化数据转换为其他格式,本文将使用Python3实现Excel与TXT的智能转换,需要的可以... 目录场景应用:为什么需要这种转换技术解析:代码实现详解核心代码展示改进点说明实战演练:从Excel到

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Python中多线程和多进程的基本用法详解

《Python中多线程和多进程的基本用法详解》这篇文章介绍了Python中多线程和多进程的相关知识,包括并发编程的优势,多线程和多进程的概念、适用场景、示例代码,线程池和进程池的使用,以及如何选择合适... 目录引言一、并发编程的主要优势二、python的多线程(Threading)1. 什么是多线程?2.