Question: 1) Download and run the program below, it implements an interactive Last In First Out queue LIFO. Commonly called a stack. 2) Become familiar with
1) Download and run the program below, it implements an interactive Last In First Out queue LIFO.
Commonly called a stack.
2) Become familiar with the options and usage.
3) Review code - It is based on a C-STRUCTURE called Stack.
4) Three functions are used:
a) PUSH - Adds 1 data element to the top of the stack
b) POP - Removes 1 element from the top of the stack
c) Display - Displays the current values contained in the stack - last element on top
5) Review the implementation of these functions.
MODIFY THE CODE TO DO THE FOLLOWING:
6) Add the ability to PUSH an integer array called frame8 containing [8][7][6][5][4][3][2][1]
when option 8 is selected. The element containing 1 should be at the top of the stack
at the end of this operation.
7) Add the ability to PUSH an integer array called frame4 containing [12][11][10][9]
when option 4 (re-defining it. Use 5 for Exit) is selected. The element containing 9 should
be at the top of the stack at the end of this operation.
3) Add the ability to POP 6 elements off the stack
when option 6 is selected.
6) Determine where changes need to be made in the code (prompts, switch statement,
add 3 new functions:
a) push8
b) push4
c) pop6
and implement.
4) Demo for checkoff
*/
/* Write a C program to implement stack. Stack is a LIFO data strcuture *
* LIFO - Last in First Out *
* Perform PUSH(insert operation), POP(Delete operation) and Display stack */
#include
#define MAXSIZE 5
struct Stack /* Structure definition for stack */
{
int stk[MAXSIZE];
int top;
};
struct Stack s;
/* Function declaration/Prototype*/
void push (void);
int pop(void);
void display (void);
void main ()
{
int choice;
int option = 1;
s.top = -1;
printf ("STACK OPERATION ");
while (option)
{
printf ("------------------------------------------ ");
printf (" 1 --> PUSH ");
printf (" 2 --> POP ");
printf (" 3 --> DISPLAY ");
printf (" 4 --> EXIT ");
printf ("------------------------------------------ ");
printf ("Enter your choice ");
scanf ("%d", &choice);
switch (choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return;
}
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 the status 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
4 --> 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
4 --> 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
4 --> 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
4 --> EXIT
------------------------------------------
Enter your choice
3
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
------------------------------------------
Enter your choice
2
poped element is = 78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3
The status of the stack is
45
23
Do you want to continue(Type 0 or 1)?
0
-----------------------------------------------------------------------------*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
