Saturday, December 20, 2014

"stack.h" file

 /* Stack Operation with push and pop */
#include<stdio.h>
#include<stdlib.h>
#define MAX 20

struct stack
{
    int arr[MAX];
    int top;
};


void initstack(struct stack *s)
{
    s->top=-1;
}


void push(struct stack *s,int item)
{
    if(s->top==MAX-1)
    {
    printf("stack is full\n");
    }
    else
    {
    s->top++;
    s->arr[s->top]=item;
    }
}


int pop(struct stack *s)
{
    int data;
    if(s->top==-1)
    {
    printf("Stack is empty.\n");
    exit(1);
    }
    else
    {
    data=s->arr[s->top];
    s->top--;
    return data;
    }
}