Monday, December 15, 2014

Stack Operation With Push() & Pop() Function

/* Stack Operation With Push() & Pop() Function
Save as 'stack.h' and include it with main function  */
#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;
    }
}

No comments:

Post a Comment