Computer Science, asked by mhondabothy87, 9 months ago

Write a C program to remove an item in a stack

Answers

Answered by masnayashwanth
0

Answer:

Given a stack with push(), pop(), empty() operations, delete middle of it without using any additional data structure.

Input : Stack[] = [1, 2, 3, 4, 5]

Output : Stack[] = [1, 2, 4, 5]

Input : Stack[] = [1, 2, 3, 4, 5, 6]

Output : Stack[] = [1, 2, 4, 5, 6]

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to use recursive calls. We first remove all items one by one, then we recur. After recursive calls, we push all items back except the middle item.

Answered by unknownRU
0

Hope it helps!

Mark me as Brainlist!

Full Program of stack using array in c programming

#include<stdio.h>

#include<conio.h>

#define MAX 5

int a[MAX],top=-1;

void push();

void pop();

void display();

int main()

{

int ch;

printf("1.push or insert\n");

printf("2.pop or delete\n");

printf("3.display\n");

printf("4.end program\n");

while(1){

printf("\nEnter choice");

scanf("%d",&ch);

switch(ch){

case 1:{

push();

break;

}

case 2:{

pop();

break;

}

case 3:{

display();

break;

}

case 4:{

exit(0);}

default:

{

printf("wrong choice");

}}

}}

void push(){

int data;

if(top==MAX-1){

printf("\n overflow or stack is full");

}

else{

printf("enter element to be pushed:");

scanf("%d",&data);

top++;

a[top]=data;

}}

void pop(){

if(top==-1){

printf("underflow or stack is empty");

}

else{

printf("popped element: %d",a[top]);

top--;

}}

void display (){

int i;

if(top>0){

printf("elements:");

for(i=top;i>0;i--)

printf("\n %d",a[i]);

}

else{

printf("the stack is empty");

}}

Similar questions