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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!