stack using structure
#include<stdio.h>
#include<conio.h>
typedef struct stack
{
int top;
int ar[50];
}st;
void push(st*,int);
int pop(st*);
int peek(st);
int isempty(st);
int isfull(st);
void disp(st);
void main()
{
int m,ch;
st s;
s.top=-1;
while(1)
{
printf("\n1) Push\n2) Pop\n3) Peek\n4) Disp\n5) Exit");
printf("\nEnter the choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: if(!isfull(s))
{
printf("Enter the value : ");
scanf("%d",&m);
push(&s,m);
}
else
printf("No more space ..... ");
break;
case 2: if(!isempty(s))
{
printf("The popped value is %d",pop(&s));
}
else
printf("Nothing to display .....");
break;
case 3: if(!isempty(s))
{
printf("The top most value is %d",peek(s));
}
else
printf("Nothing to display .....");
break;
case 4: if(!isempty(s))
{
printf("The values are");
disp(s);
}
else
printf("Nothing to display .....");
break;
case 5: exit(0);
default: printf("Not a proper choice");
}
}
}
void push(st *s,int g)
{
s->ar[++s->top]=g;
}
int pop(st *s)
{
int g;
g=s->ar[s->top];
s->top--;
return g;
}
int peek(st s)
{
return s.ar[s.top];
}
int isempty(st s)
{
if(s.top==-1)
return 1;
else
return 0;
}
int isfull(st s)
{
if(s.top==49)
return 1;
else
return 0;
}
void disp(st s)
{
int p=s.top;
while(p>=0)
{
printf("\n|%d|" ,s.ar[p]);
p--;
}
}
O/P:
run the program and see the result .........
#include<conio.h>
typedef struct stack
{
int top;
int ar[50];
}st;
void push(st*,int);
int pop(st*);
int peek(st);
int isempty(st);
int isfull(st);
void disp(st);
void main()
{
int m,ch;
st s;
s.top=-1;
while(1)
{
printf("\n1) Push\n2) Pop\n3) Peek\n4) Disp\n5) Exit");
printf("\nEnter the choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: if(!isfull(s))
{
printf("Enter the value : ");
scanf("%d",&m);
push(&s,m);
}
else
printf("No more space ..... ");
break;
case 2: if(!isempty(s))
{
printf("The popped value is %d",pop(&s));
}
else
printf("Nothing to display .....");
break;
case 3: if(!isempty(s))
{
printf("The top most value is %d",peek(s));
}
else
printf("Nothing to display .....");
break;
case 4: if(!isempty(s))
{
printf("The values are");
disp(s);
}
else
printf("Nothing to display .....");
break;
case 5: exit(0);
default: printf("Not a proper choice");
}
}
}
void push(st *s,int g)
{
s->ar[++s->top]=g;
}
int pop(st *s)
{
int g;
g=s->ar[s->top];
s->top--;
return g;
}
int peek(st s)
{
return s.ar[s.top];
}
int isempty(st s)
{
if(s.top==-1)
return 1;
else
return 0;
}
int isfull(st s)
{
if(s.top==49)
return 1;
else
return 0;
}
void disp(st s)
{
int p=s.top;
while(p>=0)
{
printf("\n|%d|" ,s.ar[p]);
p--;
}
}
O/P:
run the program and see the result .........
Comments
Post a Comment