Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

1. Identify the sources for this conflict.

Answered: 1 week ago

Question

3. The group answers the questions.

Answered: 1 week ago