Baba Stacks DS 05-03-2022
Baba Stacks DS 05-03-2022
COURSE : M.C.A
DATE: 05-03-2022
PROGRAM : STACKS
STACKS
Write a c program that illustrates stack operations PUSH and POP
#include<stdio.h>
//#include<process.h>
#include<conio.h>
#include<stdlib.h>
int top=-1,stack[MAX];
void push();
void pop();
void display();
int main()
{
int ch;
printf("\n\n1.push\n2.pop\n3.Display\n4.exit");
scanf("%d",&ch);
switch(ch)
case 1: push();
break;
case2:pop();
break;
case3:display();
break;
case4:exit(0);
default:printf("\nwrong choice!!");
void push()
int val;
if(top==MAX-1)
{
printf("\nstack is full!!");
else
scanf("%d",&val);
top=top+1;
stack[top]=val;
void pop()
if(top==-1)
printf("\nstack is empty!!");
else
top=top-1;
void display()
int i;
if(top==-1)
printf("\nstack is empty!!");
else
printf("\nstack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
OUTPUT:
1.push
2.pop
3.Display
4.exit
1.push
2.pop
3.Display
4.exit
wrong choice!!
1.push
2.pop
3.Display
4.exit
wrong choice!!
2.pop
3.Display
4.exit
wrong choice!!
1.push
2.pop
3.Display
4.exit
#include<stdio.h>
#include<stdlib.h>
struct stack{
int top;
unsigned capacity;
int* array;
};
// stack as 0
stack->capacity=capacity;
stack->top=-1;
stack->array=(int*)malloc(stack->capacity*sizeof(int));
return stack;
return stack->top==-1;
}
if(isfull(stack))
return;
stack->array[++stack->top]=item;
if(isempty(stack))
return INT_MIN;
return stack->array[stack->top--];
if(isempty(stack))
return INT_MIN;
return stack->array[stack->top];
}
//driver program to test above functions
int main()
push(stack, 10);
push(stack, 20);
push(stack, 30);
return 0;
Output:
10 pushed to stack
20 pushed to stack
30 pushed to stack
--------------------------------