Saturday, August 31, 2013

Simple Stacks Implementation using arrays : PUSH , POP

/*Simple Stacks Implementation : PUSH , POP*/

#include<stdio.h>

#define max 10

struct stack
 {
  int arr[max];
  int top;
 };
void ini(struct stack *s)
 {
  s->top=(-1);
 }
void push(struct stack *s,int val)
 {
  if(s->top==(max-1))
  {
  printf("\nSTACK OVERFLOW\n");
  return;
 }

  s->top++;
  s->arr[s->top]=val;

 }
int pop(struct stack *s)
 {
  int data;
  if(s->top==-1)
    {
  printf("\nSTACK UNDERFLOW\n");
return;
}


data=s->arr[s->top];
s->top--;
return data;

}
void main()
 {
  struct stack s1;
  int i;
  ini(&s1);
  push(&s1,10);
  push(&s1,40);
  push(&s1,20);
  i=pop(&s1);
  printf("\n%d\n",i);
  i=pop(&s1);
  printf("\n%d\n",i);
  i=pop(&s1);
  printf("\n%d\n",i);
 }




No comments: