Search This Blog

Tuesday 27 October 2015

Stack using array

#include<stdio.h>
#include<conio.h>
#define MAX 5
int stack[MAX],top=-1,item;
void push() {
 if(top==MAX-1)
  printf("\nStack is full\n");
 else {
  printf("enter the item: ");
  scanf("%d",&item);
  top++;
  stack[top]=item;
  printf("\nInserted element: %d\n",item);
 }
}
void pop() {
 if(top==-1)
  printf("\nStack is empty\n");
 else {
  item=stack[top];
  top--;
  printf("\nDeleted item: %d\n",item);
 }
}
void stacktop() {
 if(top==-1)
  printf("\nStack empty\n");
 else {
  item=stack[top];
  printf("\nStack top: %d\n",item);
 }
}
void display() {
 int i;
 if(top==-1)
  printf("\nStack is empty\n");
 else {
  for(i=top;i>=0;i--) {
  printf("\n%d\n",stack[i]);
  }
 }
}
void main() {
 int ch;
 clrscr();
 do {
  printf("\n 1.Push\n 2.Pop\n 3.Stacktop\n 4.Display\n 5.Exit\n");
  printf("\nEnter your choice: ");
  scanf("%d",&ch);
  switch(ch) {
   case 1: push(); break;
   case 2: pop(); break;
   case 3: stacktop(); break;
   case 4: display(); break;
   default: printf("Invalid Choice");
  }
 } while(ch!=4);
 getch();
}

No comments:

Post a Comment