top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to reverse a queue, please share sample C code?

+3 votes
425 views
How to reverse a queue, please share sample C code?
posted Apr 5, 2016 by Saurabh Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Push all nodes one by one to the stack till end node and pop one by one and reconstruct the queue which will be the reverse of the original one.

1 Answer

0 votes

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; 
}
answer Apr 6, 2016 by Manikandan J
Stack.h and QQ.h missing
...