Saturday, December 20, 2014

Reverse of Queue using Stack

 /*  1.First insert all  elements in rear side of Queue.
     2.Secondly,delete elements one by one from   front    of Queue and puts those in a stack .
  3.Then pop and print the elements until empty */

#include<stdio.h>
#include "stack.h"  //given below
#include"QQ.h"   //given below

int main()
{
    int n,arr[20],i,j=0;
    struct stack s;
    initstack(&s);
    printf("Enter no");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter values:");
        scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
        insert(arr[i]);
    }
    while(j!=n)
    {
        push(&s,del());
        j++;
    }
    printf("Reverse is ");
    while(s.top!=-1)
    {
        printf("%d",pop(&s));
    }
    printf("\n");
return 0;
}