Sunday, July 24, 2011

Stack implimentation using array


#include<stdio.h>
#include<conio.h>
struct stk
{
int a[10];
int top;
};

main()
{
      int i,n,j=0;
      void push(struct stk *s,int item);
      extern int isempty(struct stk *s);
      extern int isfull(struct stk *s);
      int pop(struct stk *s);
      struct stk s;
      s.top=-1;
      while(1)
      {
      printf("Do you want to push the stack if Yes press \"1\" otherwise press \"0\" : ");
      scanf("%d",&n);
      if(n==1)
      {
              j=1;
      printf("\nEnter the item to be pushed:");
      scanf("%d",&i);
      push(&s,i);
      }
      else
      break;
      }
      if(j==1)
      {
       while(1)
      {
      printf("\nDo you want to pop the stack if Yes press \"1\" otherwise press \"0\" : ");
      scanf("%d",&n);
      if(n==1)
      {
              i=pop(&s);
              if(i==-1000)
              {
                          break;
              }
             
      printf("\nitem poped is: %d",i);
     
      }
      else
      break;
     
      }
   
     }
     getch();
      return 0;
}

void push(struct stk *s,int item)
{
 int c;
 c=isfull(s);
 if(c==0)
 {
          printf("stack is full. it is not possible to PUSH %d ",item);
          return;
 }
 s->a[++(s->top)]=item;
}
int isfull(struct stk *s)
{
    if(s->top==9)
     return 0;
    return 1;
}
int pop(struct stk *s)
{
    int c,i;
    c=isempty(s);
    if(c==0)
     {
          printf("stack is empty. it is not possible to POP ");
          return -1000;
     }
    i=s->a[(s->top)--];
    return i;
}
int isempty(struct stk *s)
{
    if(s->top==-1)
     return 0;
    return 1;
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More