Stack for storing character value using pointer

// stack using pointer for character array

#include<stdio.h>
#include<conio.h>

typedef struct stack
{
int top;
char ar[50];
}st;




void push(st*,char);
char pop(st*);
char peek(st);
int isempty(st);
int isfull(st);
void disp(st);

void main()
{
int ch;
char m;
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 : ");
fflush(stdin);
scanf("%c",&m);
push(&s,m);
}
else
printf("\nNo more space ..... ");
break;
case 2: if(!isempty(s))
{
printf("\n\nThe popped value is %c",pop(&s));
}
else
printf("\nNothing to display .....");
break;
case 3: if(!isempty(s))
{
printf("\n\nThe top most value is %c",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,char g)
{
s->ar[++s->top]=g;
}

char pop(st *s)
{
char g;
g=s->ar[s->top];
s->top--;
return g;
}

char 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|%c|" ,s.ar[p]);
p--;
}
}

Comments

Popular posts from this blog

JAVA program to add two distance

Print Pattern using C