Monday, May 30, 2016

WAP to implement stack of strings

#include<stdio.h>
#include<conio.h>
#include<string.h>
int top=-1;
char st[10][50];
void push(char v[])
{
if(top==10)
printf("Stack is full");
else
{
top++;
strcpy(st[top],v);
}

}
char* pop()
{
char *s;
if(top==-1)
printf("\nstack is empty");
else
{
strcpy(s,st[top]);
top--;
}
return s;
}
void peep(int n)
{
if(n>top)
{
printf("Out of Range");
}
else
{
printf("Value at ps %d is %s",n,st[top-n+1]);
}
}
void change(int p,char v[])
{
if(p>top)
{
printf("Out of Range");
}
else
{
strcpy(st[top-p+1],v);
}
}
void display()
{
int i;
if(top==-1)
{
printf("Stack is empty");
return;
}
printf("\n displaying stack:");
for(i=top;i>=0;i--)
{
printf("\n%s",st[i]);
}
}
void main()
{
int n,p;
char v[50];
clrscr();
do
{
printf("\n1.Push\n ");
printf("2.Pop\n ");
printf("3.Peep\n ");
printf("4.Change\n ");
printf("5.Display\n ");
printf("6.Exit\n ");
printf("\nEnter your choice : ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nenter a value :");
scanf("%s",&v);
push(v);
break;
case 2:
printf("Deleted value is: %s",pop());
break;
case 3:
printf("\nenter a positon :");
scanf("%d",&p);
peep(p);
break;
case 4:
printf("\nenter a postion :");
scanf("%d",&p);
printf("\nenter a value :");
scanf("%s",&v);
change(p,v);
break;
case 5:
display();
break;
case 6:
printf("\nThank you");
break;
default:
printf("\nYou have entered a wrong value");
}
}while(n!=6);
getch();
}

No comments:

Post a Comment