Author: Gour Ch. Saha
Contact for any query:: gour_ch_saha@yahoo.co.in
#include<stdio.h>
#include<stdlib.h>
#define MAXSTACK 100
typedef struct stack_tag
{
int top;
int entry[MAXSTACK];
}stack_type;
void initialize_stack(stack_type *st_ptr);
void push(int x,stack_type *st_ptr);
void pop(int *item , stack_type *st_ptr);
int stack_full(stack_type *st_ptr);
int stack_empty(stack_type *st_ptr);
void print_stack(stack_type *st_ptr);
void initialize_queue(stack_type *st_ptr1 , stack_type *st_ptr2);
void add(int x,stack_type *st_ptr1 , stack_type *st_ptr2);
void delete(int *item , stack_type *st_ptr1 , stack_type *st_ptr2);
void print_queue(stack_type *st_ptr1 , stack_type *st_ptr2);
main()
{
int item;
stack_type stack1;
stack_type *st_ptr1=&stack1;
stack_type stack2;
stack_type *st_ptr2=&stack2;
initialize_queue(st_ptr1 , st_ptr2);
add(10 , st_ptr1 , st_ptr2);
add(14 , st_ptr1 , st_ptr2);
add(15 , st_ptr1 , st_ptr2);
add(16 , st_ptr1 , st_ptr2);
print_queue(st_ptr1 , st_ptr2);
delete(&item , st_ptr1 , st_ptr2);
printf("The poped item is %d\n",item);
delete(&item , st_ptr1 , st_ptr2);
printf("The poped item is %d\n",item);
print_queue(st_ptr1 , st_ptr2);
printf("********************\n");
add(19 , st_ptr1 , st_ptr2);
//add(20 , st_ptr1 , st_ptr2);
print_queue(st_ptr1 , st_ptr2);
delete(&item , st_ptr1 , st_ptr2);
printf("The poped item is %d\n",item);
delete(&item , st_ptr1 , st_ptr2);
printf("The poped item is %d\n",item);
delete(&item , st_ptr1 , st_ptr2);
printf("The poped item is %d\n",item);
delete(&item , st_ptr1 , st_ptr2);
printf("The poped item is %d\n",item);
delete(&item , st_ptr1 , st_ptr2);
printf("The poped item is %d\n",item);
print_queue(st_ptr1 , st_ptr2);
}
void initialize_queue(stack_type *st_ptr1 , stack_type *st_ptr2)
{
initialize_stack(st_ptr1);
initialize_stack(st_ptr2);
}
void print_queue(stack_type *st_ptr1 , stack_type *st_ptr2)
{
printf("The content of First stack (st1)\n");
print_stack(st_ptr1);
printf("The content of second stack (st2)\n");
print_stack(st_ptr2);
}
void delete(int *x , stack_type *st_ptr1 , stack_type *st_ptr2)
{
int item;
while(!(stack_empty(st_ptr1)))
{
pop(&item,st_ptr1);
push(item,st_ptr2);
}
pop(x,st_ptr2);
}
void add(int x,stack_type *st_ptr1 , stack_type *st_ptr2)
{
int item;
while(!(stack_empty(st_ptr2)))
{
pop(&item,st_ptr2);
push(item,st_ptr1);
}
push(x,st_ptr1);
}
void pop(int *item, stack_type *st_ptr)
{
if(stack_empty(st_ptr))
{
printf("The stack is Empty. Can't pop\n");
exit(0);
}
else
{
*item=st_ptr->entry[--st_ptr->top];
}
}
void push(int x,stack_type *st_ptr)
{
if(stack_full(st_ptr))
{
printf("Stack is full. Cann't push item\n");
exit(0);
}
else
{
st_ptr->entry[st_ptr->top++]=x;
}
}
void initialize_stack(stack_type *st_ptr)
{
st_ptr->top = 0;
}
int stack_full(stack_type *st_ptr)
{
return (st_ptr->top >= MAXSTACK);
}
int stack_empty(stack_type *st_ptr)
{
return (st_ptr->top<=0);
}
void print_stack(stack_type *st_ptr)
{
int i;
for(i=0;i<st_ptr->top;i++)
{
printf("%d ",st_ptr->entry[i]);
}
printf("\n");
}