Design and develop an interactive program that will evaluate the infix expressio
ID: 3592624 • Letter: D
Question
Design and develop an interactive program that will evaluate the infix expression using stack concept and operations. Implement the stack using linked list structure.
Your program must consist of the following operations:
Push an element into stack
Pop an element from the stack
Display the top element in the stack
Display all the elements in the stack.
Enter an infix expression: (((2 * 5) - (1 * 2)) / (9 - 7))
Current content in the stack: (
Top value in the stack: (
Current content in the stack: ((
Top value in the stack: (
Current content in the stack: (((
Top value in the stack: (
Current content in the stack: (((2
Top value in the stack: 2
Current content in the stack: (((2 *
Top value in the stack: *
Current content in the stack: (((2 * 5
Top value in the stack: 5
Current content in the stack: ((10
Top value in the stack: 10
Current content in the stack: ((10 -
Top value in the stack: -
Current content in the stack: ((10 – (
Top value in the stack: (
Current content in the stack: ((10 – (1
Top value in the stack: 1
Current content in the stack: ((10 – (1 *
Top value in the stack: *
Current content in the stack: ((10 – (1 * 2
Top value in the stack: 2
Current content in the stack: ((10 – 2
Top value in the stack: 2
Current content in the stack: (8
Top value in the stack: 8
Current content in the stack: (8 /
Top value in the stack: /
Current content in the stack: (8 / (
Top value in the stack: (
Current content in the stack: (8 / (9
Top value in the stack: 9
Current content in the stack: (8 / (9 -
Top value in the stack: -
Current content in the stack: (8 / (9 - 7
Top value in the stack: 7
Current content in the stack: (8 / 2
Top value in the stack: 2
Current content in the stack: 4
Top value in the stack: 4
Current content in the stack: <empty>
Top value in the stack: <undefined>
The result is 4
Explanation / Answer
Design and develop an interactive program that will evaluate the infix expression using stack concept and operations. Implement the stack using linked list structure.
Your program must consist of the following operations:
Push an element into stack
Pop an element from the stack
Display the top element in the stack
Display all the elements in the stack.
#include <stdio.h>
#include <conio.h>
#define MAXSIZE 5
struct stack /* Structure definition for stack */
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
/* Function declaration/Prototype*/
void push (void);
int pop(void);
void display_Top_element(void)
void display (void);
void main ()
{
int choice;
int option = 1;
clrscr ();
s.top = -1;
printf ("STACK OPERATION ");
while (option)
{
printf ("------------------------------------------ ");
printf (" 1 --> PUSH ");
printf (" 2 --> POP ");
printf (" 3 --> DISPLAY Top Element ");
printf (" 4 --> DISPLAY ");
printf (" 5 --> EXIT ");
printf ("------------------------------------------ ");
printf ("Enter your choice ");
scanf ("%d", &choice); Switch(choice)
{
Case 1:push()
break;
Case 2:pop()
break;
Case 3:display_top_element()
break;
Case 4:display()
break;
Case 5:return()
break;
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)? ");
scanf ("%d", &option);
}
}
/*Function to add an element to the stack*/
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full ");
return;
}
else
{
printf ("Enter the element to be pushed ");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/*Function to delete an element from the stack*/
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty ");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %d ", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/*Function to display top element from the stack*/
int display_top_element ()
{
if (s.top == - 1)
{
printf ("Stack is Empty ");
return (s.top);
}
else
{
printf ("Top element is = %d ", s.stk[s.top]);
s.top = s.top - 1;
}
}
/*Function to display the status(all element) of the stack*/
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty ");
return;
}
else
{
printf (" The status of the stack is ");
for (i = s.top; i >= 0; i--)
{
printf ("%d ", s.stk[i]);
}
}
printf (" ");
}
/*---------------------------------------------------------------------------
Output
STACK OPERATION
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY TOP ELEMENT
4 --> DISPLAY
5 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
23
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY TOP ELEMENT
4 --> DISPLAY
5 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
45
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY TOP ELEMENT
4 --> DISPLAY
5 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY TOP ELEMENT
4 --> DISPLAY
5 --> EXIT
------------------------------------------
Enter your choice
4
The status of the stack is
78
45
23
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.