top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

C: How to reverse a queue using one stack?

+1 vote
126 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 Google+ Share Button LinkedIn Share Button Multiple Social 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
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...