top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: How to reverse a queue using one stack?

+1 vote
2,848 views
C: How to reverse a queue using one stack?
posted Jul 7, 2017 by Ajay Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
 
Best answer

/* 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 Jul 10, 2017 by Manikandan J
Similar Questions
...