Write a C Program to implement Stack using array.

Program:-

#include<stdio.h>
#include<stdlib.h>
#define max 10
int stack[max],top=-1,item;

void push()
{
 if(top==max-1)printf("\nStack is Full.\n");
 else
 {
  printf("\nEnter value to PUSH:"); 
  scanf("%d",&item);
  top++; stack[top]=item;
 }
}

void pop()
{
 if(top==-1)printf("\nStack is Empty.\n");
 else
 {
  item=stack[top];
  top--;
  printf("\n%d has been POPED OUT.\n",item);
 }
}

void dis()
{
 if(top==-1)printf("\nStack is Empty.\n");
 else
 {
  printf("\nThe stack will be:-\n");
  for(item=top;item>=0;item--){ printf("%d\n",stack[item]); }
 }
}

//This program is Developed by MONISH KUMAR BAIRAGI

main()
{
 while(1)
 {
  printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\nEnter your choice:");
  scanf("%d",&item);
  switch(item)
  {
   case 1:push(); break;
   case 2:pop(); break;
   case 3:dis(); break;
   default:printf("\nThank You for using this Program.\n"); exit(0);
  }
 }
}

Output:-


1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:1

Enter value to PUSH:10

1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:1

Enter value to PUSH:20

1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:1

Enter value to PUSH:30

1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:3

The stack will be:-
30
20
10

1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:2

30 has been POPED OUT.

1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:3

The stack will be:-
20
10

1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:1

Enter value to PUSH:50

1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:3

The stack will be:-
50
20
10

1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:4

Thank You for using this Program.

Post a Comment

0 Comments