Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

//Create a file seta1.

#include <stdio.h>
#include <stdlib.h>
#include "Sstack.h"
#define MAX 20

void main()
{
struct stack s1;
int ch, x;
init(&s1);
do
{
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Empty");
printf("\n Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &x);
push(&s1,x);
printf("stack elements are");
display(&s1);
break;
case 2:
if(isEmpty(&s1))
printf("stack is empty");
else
printf("poped element is %d \n",pop(&s1));
printf("stack elements are");
display(&s1);
break;
case 3:
if(isEmpty(&s1))
printf("stack is empty");
else
printf("stack is not empty");
break;
}
}
while(ch>0 && ch<3);
}

//create a file Sstack.h

#define MAX 20
struct stack
{
int data[MAX];
int top;
};

void init(struct stack *s)


{
s->top = -1;
}

void push(struct stack *s,int item)


{
s->data[++s->top]=item;
}

int pop(struct stack *s)


{
return (s->data[s->top--]);
}

int isEmpty(struct stack *s)


{
if (s->top == -1)
return -1;
else
return 0;
}

void display(struct stack *s)


{
int i;
if(s->top==-1)
{
printf("stack is empty");
return;
}
for(i=s->top;i>=0;i--)
{
printf(" %d->",s->data[i]);
}
}

You might also like