Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fix the errors in this code: #include #include #define Max_Size 5 void push(int s[], int *p_top, int value); int pop(int s[]); int peep(int s[]); int

Fix the errors in this code:

#include #include #define Max_Size 5 void push(int s[], int *p_top, int value); int pop(int s[]); int peep(int s[]); int isempty(); void printCurrentStack(int s[], int *p_top);

int main() { int element, choice; int top=0; int n; int *p_top=⊤ int s[]={}; while(1) { printf("Stack Operations. "); printf("1. Push. "); printf("2. Pop. "); printf("3. Peek. "); printf("4. IsEmpty. "); printf("5. Print Current Stack. "); printf("6. Exit. "); printf("Enter your choice. "); scanf("%d",&choice);

switch (choice) { case 1: if (*p_top == Max_Size) { printf("Error: Overflow "); printCurrentStack(s, p_top); } else { printf("Enter the element to Push. "); scanf("%d", &element); push(s, p_top, element); } break;

case 2: element=pop(s); if(element != -1) print(" popped element is : %d",element); break:

case 3: element=peek(s); if(element != -1); print(" The value stored at the top of the stack is : %d",element); break;

case 4: if(isempty()) { print("stack is empty"); } else { print("stack is not empty : "); } break;

case 5: printCurrentStack(s, p_top); break;

case 6: exit(1); } } }

void push(int s[], int *p_top, int value) { s[(*p_top)] = value; (*p_top)++; printCurrentStack(s, p_top); }

void printCurrentStack(int s[], int *p_top) { if ((*p_top) == 0) { printf("The stack is empty. "); return; }

printf("There are %d elements currently in the stack. ", (*p_top));

for (int i = (*p_top) - 1; i >= 0; i--) printf("%d ", s[i]); printf(" "); }

int pop(int s[]) { if((*p_top)==-1) { print(" stack underflow : "); return -1; } int value=s[(*p_top)] ; (*p_top)--; return value; }

int peep(int s[]) { if((*p_top)==-1) { print(" stack underflow : "); return -1; } return s[(*p_top)]

}

int isempty() { if((*p_top)==-1) return 1; else return 0; }

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

More Books

Students also viewed these Databases questions