Question
Please help in C++ question. Need to build a stack with linked list to do the RPN (like 3 9 -) (9 4 2 -
Please help in C++ question.
Need to build a stack with linked list to do the RPN (like 3 9 -) (9 4 2 - *), only consider 0-9 and * / + -
I have write the code
#include
#include
using namespace std;
struct Stack{
double number;
Stack *link;
};
typedef Stack* StackPrt;
void push(StackPrt& head,double the_number);
double pop(StackPrt& head);
bool empty(StackPrt head);
int main ()
{
char input_numberc = 'a' ;//input
double input_number; //operation
double hold1;
double hold2;
double result;
StackPrt head;
head = NULL;
while(input_numberc != ' ' ){
cin>>input_numberc;
if(input_numberc == '*'){
hold1 = pop(head);
hold2 = pop(head);
result = hold1 * hold2;
push(head,result);
}
else if(input_numberc == '+'){
hold1 = pop(head);
hold2 = pop(head);
result = hold1 + hold2;
push(head,result);
}
else if(input_numberc == '-'){
hold1 = pop(head);
hold2 = pop(head);
result = hold2 - hold1;
push(head,result);
}
else if(input_numberc == '/'){
hold1 = pop(head);
hold2 = pop(head);
result = hold2 / hold1 ;
push(head,result);
}
else if( input_numberc == '0'){
push(head,0);}
else if( input_numberc == '1'){
push(head,1);}
else if( input_numberc == '2'){
push(head,2);}
else if( input_numberc == '3'){
push(head,3);}
else if( input_numberc == '4'){
push(head,4);}
else if( input_numberc == '5'){
push(head,5);}
else if( input_numberc == '6'){
push(head,6);}
else if( input_numberc == '7'){
push(head,7);}
else if( input_numberc == '8'){
push(head,8);}
else if( input_numberc == '9'){
push(head,9);}
}
cout<<"The retsult is"<
return(0);
}
//functions
void push(StackPrt& head,double the_number){
StackPrt temp_prt;
temp_prt = new Stack;
temp_prt->number = the_number;
if(!empty(head)){
temp_prt->link = head;
}
else if(empty(head)){
temp_prt->link = NULL;
}
head = temp_prt;
}
double pop(StackPrt& head){
double number_store;
if( !empty(head) ){
number_store = head->number;
}
else{
cout<<"there is no number now"<
exit (1);
}
StackPrt temp;
temp = head;
head = head->link;
delete temp;
return(number_store);
}
bool empty(StackPrt head){
return ( head == NULL);
}
The stack no work. Can you help me fix that ?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started