利用链栈设计并实现一个整型算术表达式计算器(运算符至少包含±*/())。
数据结构:链表
概要设计:将自写栈(数据结构为数组)版计算器的栈结构替换成链栈,栈外的其他部分可几乎不做改动,只将对应顺序栈中函数替换成对应链栈中函数。
自写栈版链接点这里
详细设计:
1.设置结点Node存储运算符,设置结点node存储数字。
2.在原本的代码中字符栈和数字栈写在了同一个栈类里,通过不同的数据类型调用同名函数。为了保证思路清晰,改编后的代码中写了两个链表栈类,数字栈numstack和字符栈opstack。
3.将栈类原本的判空函数删去,将链栈中各函数与原来的函数一一对应。
代码如下
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<char,int> mp;
struct Node{
char oper;
Node *Next;
};
struct node
{
int data;
node *next;
};
class numstack
{
private:
node *top;
public:
numstack()
{
top=NULL;
}
~numstack()
{
while(!empty())
pop();}
bool empty()
{
return top==NULL;}
int get_top()
{
return top->data;}
void push(int x)
{
node *t = new node;
t->data=x;
t->next=NULL;//产生节点,装入元素到节点中(可以删掉但建议加上)
t->next=top;
top=t; //连接结点到表头
}
void pop()
{
if(empty())<