Stack using structure pointer
#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("\n\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("\nNo more space ..... ");
break;
case 2: if(!isempty(s))
{
printf("\n\nThe popped value is %d",pop(&s));
}
else
printf("\nNothing to display .....");
break;
case 3: if(!isempty(s))
{
printf("\n\nThe top most value is %d",peek(s));
}
else
printf("\nNothing to display .....");
break;
case 4: if(!isempty(s))
{
printf("\n\nThe values are");
disp(s);
}
else
printf("\nNothing to display .....");
break;
case 5: exit(0);
default: printf("\nNot 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--;
}
}
#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("\n\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("\nNo more space ..... ");
break;
case 2: if(!isempty(s))
{
printf("\n\nThe popped value is %d",pop(&s));
}
else
printf("\nNothing to display .....");
break;
case 3: if(!isempty(s))
{
printf("\n\nThe top most value is %d",peek(s));
}
else
printf("\nNothing to display .....");
break;
case 4: if(!isempty(s))
{
printf("\n\nThe values are");
disp(s);
}
else
printf("\nNothing to display .....");
break;
case 5: exit(0);
default: printf("\nNot 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--;
}
}
Comments
Post a Comment