Monday, 21 January 2013

Static Stacks

Static Implementation of Stack  through C


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 10
struct stack
{
    int a[max];
    int top;
    };
   
void push(struct stack *p,int n)
{
  if(p->top==max-1)
 
 {
printf("STACK FULL");
 }
  else
  {
  p->a[++p->top]=n;
}
 
 }//end of push

 int pop(struct stack *p)
 {
  if(p->top==-1)
  {
printf("\nStack is empty\n");
return -999;
           }
else
return p->a[p->top--];
 }//end of POP

int peek(struct stack *p)
{
  if(p->top!=max-1)
  return p->a[p->top];
}//help us to see the top most element of the stack without removing it


 int menu(struct stack *s)
 {
  printf("\n1.PUSH\n2.POP\n3.PEEK\n4.EXIT\n");
 int a;
 do
 {
 printf("Enetr the choice\n");
 scanf("%d",&a);

      switch(a)
      {
 case 1:
 
   printf("Enter the number\n");
   int n;
  scanf("%d",&n);
   push(s,n);
   return 1;
    break;
  case 2:
  printf("%d\n",pop(s));
  return 2;
  break;
  case 3:printf("%d",peek(s));
  return 3;
  case 4:return 4;
  break;
}

      }while(a>4||a<1);
 
  }
 

 main()
 {
    struct stack s;
    s.top=-1;
    int x;
  do{
  x=menu(&s);
}while(x!=4);
 
    getch();
    }
 

No comments:

Post a Comment