Program for Array Implementation of Stack

Monday, 2 December 2013

/* program for Array Implementation of Stack*/

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 5

typedef struct stack
{
int data[MAX];
int top;
}stack;

void init(stack *);
int  empty(stack *);
int  full(stack *);
int  pop(stack *);
void push(stack *,int );
void print(stack *);
void main()
 { stack s;
   int x,op;
   init(&s);
   clrscr();
   do
     {
printf("\n1)Push\n2)Pop\n3)Print\n4)Quit");
printf("\nEnter Your Choice : ");
scanf("%d",&op);
switch(op)
{
     case 1: printf("\nEnter a Number : ");
     scanf("%d",&x);
     if(!full(&s))
 push(&s,x);
     else
 printf("\nStack is full");
     break;
     case 2: if(!empty(&s))
{
 x=pop(&s);
 printf("\nPopped Data is :%d",x);
}
     else
 printf("\nStack is empty");
     break;
     case 3: print(&s);break;
}
    }while(op!=4);
 }
void init(stack *s)
{
s->top=-1;
}

int empty(stack *s)
{
if(s->top==-1) return(1);
return(0);
}

int full(stack *s)
{
if(s->top==MAX-1)    return(1);
return(0);
}

void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}

int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
void print(stack *s)
 { int i;
   printf("\n");
   for(i=s->top;i>=0 ;i--)
      printf("%d   ",s->data[i]);
 }



OUTPUT:-


1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 1

Enter a Number : 12

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 1

Enter a Number : 45

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 1

Enter a Number : 56

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 3

56   45   12
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice :2
Popped Data is :56
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 2

Popped Data is :45
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 2

Popped Data is :12
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 2

Stack is empty
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice :4

1 comment

  1. This webpage is containing a pleasant stuff with the teachings, I loved it a lot.
    learn c++

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...
Related Posts Plugin for WordPress, Blogger...
 

Most Reading

Labels