Stack using array c program
Aim: To implement stack using array in c program
program:
#include<stdio.h>
#define MAXSIZE 3
void push(int);
int pop();
void display();
int top=0;stack[MAXSIZE];
void main()
{
 int item,n,ch;
 clrscr();
 while(1)
 {
  printf("1.push\n");
  printf("2.pop\n");
  printf("3.display\n");
  printf("4.exit\n");
  printf("enter your choice:");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:printf("enter the item you want to push:");
    scanf("%d",&item);
    push(item);
    break;
   case 2:item=pop();
    printf("item deleted from stack is %d\n",item);
    break;
   case 3:display();
    break;
   default:exit(0);
  }
 }
}
void push(int item)
{
 if(top==MAXSIZE)
  printf("overflow");
 else
 {
  top=top+1;
  stack[top]=item;
 }
}
int pop()
{
 int item;
 if(top==0)
 {
  printf("underflow");
  exit(0);
 }
 else
 {
  item=stack[top];
  top=top-1;
  return item;
 }
}
void display()
{
 int i;
 printf("elements of stack are \n");
 for(i=1;i<=top;i++)
  printf("%3d",stack[i]);
 printf("\n");
}
 
 
 
          
      
 
  
 
 
 
 
 
 
 
 
 
 
No comments:
Post a Comment