Slip 2_2 ,Slip 8_2, Slip 17_2: Write a program that copies the contents of one stack into another. Use stack library to perform basic stack operations. The order of two stacks must be identical.(Hint: Use a temporary stack to preserve the order).

 #include <stdio.h>
char s[20];
int top;
void init()
{
 top=-1;
}
int isempty(){
 if(top==-1)
 return 1;
 else
 return 0;
}
int isfull()
{
 if(top==19)
 return 1;
 else
 return 0;
}
void push(char ch)
{
 if(isfull()==1)
 printf("stack is full");
 else
 {
 top++;
 s[top]=ch;
 }
}
char pop()
{
 char ch;
 if(isempty()==1)
 printf("stack is empty");
 else
 {
 ch=s[top];
 top--;
 }
 return ch;
}
main()
 {
 int i,k=0;
 char temp[20];
 init();
 char str[20];
 printf("enter string ");
 scanf("%s",str);
 for(i=0;str[i]!='\0';i++)
 {
 push(str[i]);
 }
 while(!isempty())
 {
 temp[k]=pop();
 k++;
 }
 temp[k]='\0';
 //Again String push into stack
 printf("second string :");
 for(i=0;temp[i]!='\0';i++)
 {
 push(temp[i]);
 }
 while(!isempty())
 {
 printf("%c",pop());
 }
 }

Comments

Popular posts from this blog

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

Slip10_2, 30_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection sort

Slip 23_2: Read the data from file ‘sortedcities.txt’ containing sorted names of cities and their STD codes. Accept a name of the city from user and use binary search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”.