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

相关文章

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL