Sunday, July 24, 2011

Stack


#include<stdio.h>
#include<conio.h>
int a[10];
main()
{
      int tos=-1,i,n,j;
      void push(int *top,int item);
      extern int isempty(int top);
      extern int isfull(int top);
      int pop(int *top);
      printf("Enter the number of items to be pushed:");
      scanf("%d",&n);
      for(j=1;j<=n;j++)
      {
      printf("\nEnter the item to be pushed:");
      scanf("%d",&i);
      push(&tos,i);
      }
      for(j=1;j<=n;j++)
      {
      i=pop(&tos);
      }
      getch();
      return 0;
}
void push(int *top,int item)
{
 int c;
 c=isfull(*top);
 if(c==0)
 {
          printf("stack is full. it is not possible to PUSH %d ",item);
          return;
 }
 a[++(*top)]=item;
}
int isfull(int top)
{
    if(top==9)
     return 0;
    return 1;
}
int pop(int *top)
{
    int c,i;
    c=isempty(*top);
    if(c==0)
     {
          printf("stack is empty. it is not possible to POP it further");
          return 0;
     }
    i=a[(*top)--];
    printf("\nitem %d is poped from the stack",i);
    return i;
}
int isempty(int top)
{
    if(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