Monday 30 July 2012

WAP to implement stack.

#include<stdio.h>
#include<stdlib.h>
#define MAX 100
struct stack
{
int top;
int items[MAX];
};


struct stack st;
void push(struct stack *ps,int n)
{
   if(ps->top == MAX-1)
        printf("Overflow");
    else
        ps->items[++(ps->top)]=n;
}


int empty(struct stack *ps)
{
    return (ps->top == -1);
}


int pop(struct stack *ps)
{
        if(empty(ps))
        {
            printf("Underflow");
            exit(1);
        }
        else
            return ps->items[(ps->top)--];
}
int main()
{
    st.top== -1;
    int x;
    int i=0;
    push(&st,25);
    push(&st,51);
    push(&st,53);
    push(&st,51);
    push(&st,54);
    push(&st,30);
    push(&st,50);
    push(&st,70);
    push(&st,90);
    push(&st,10);
    push(&st,80);


printf("top: %d\n",st.top);
  for(i=st.top;i>0;i--){
      x=pop(&st);
    printf("%d\n",x);
  }
  return 0;
}


No comments:

Post a Comment