How to implement Stack using Queue Class only ?srikiran G14yAsked Jan 3, 2013, 1:47 AM EST1 answerShareLinkedInXCopy linkSaveHow to implement Stack using Queue Class only ?Note:no funtions are used kindly help me in Urgent !1 AnswerKnow the answer? Post it. Somebody with the same question will find it here.srikiran G2 points14y agoPosted Jan 3, 2013, 2:47 AM EST0C#Copy#include <iostream> #include <stdio.h> #define max 5 using namespace std; class queue { int front; int rear; int data[max]; public: queue() { front = 0; rear = 0; } void enQueue(int i) { if( rear == max ) return ; data[rear++] = i; } int deQueue() { if( front == rear ) { return -1; } return data[front++]; } void disp() { cout<<"Front ="<<front<<" Rear = "<<rear<<endl; } int size() { return rear; } int reSet() { front = 0; rear = 0; } }; //I am Stack also here class stack { queue q; public: void push( int i) { if( q.size() == max ) { cout<<"Stack is full"<<endl; return ; } q.enQueue(i); } int pop() { int ret = 0; int result = 0; queue temp; int c = 0; while( ( ret = q.deQueue() ) != -1) { temp.enQueue(ret); result = ret; } q.reSet(); int size = temp.size(); if( size == 0 ) { cout<<"stack is empty " <<endl; return -1; } else { size--; } while(size--) q.enQueue(temp.deQueue()); temp.reSet(); return result; } void disp() { q.disp() ; } };0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign in
How to implement Stack using Queue Class only ?srikiran G14yAsked Jan 3, 2013, 1:47 AM EST1 answerShareLinkedInXCopy linkSaveHow to implement Stack using Queue Class only ?Note:no funtions are used kindly help me in Urgent !
srikiran G2 points14y agoPosted Jan 3, 2013, 2:47 AM EST0C#Copy#include <iostream> #include <stdio.h> #define max 5 using namespace std; class queue { int front; int rear; int data[max]; public: queue() { front = 0; rear = 0; } void enQueue(int i) { if( rear == max ) return ; data[rear++] = i; } int deQueue() { if( front == rear ) { return -1; } return data[front++]; } void disp() { cout<<"Front ="<<front<<" Rear = "<<rear<<endl; } int size() { return rear; } int reSet() { front = 0; rear = 0; } }; //I am Stack also here class stack { queue q; public: void push( int i) { if( q.size() == max ) { cout<<"Stack is full"<<endl; return ; } q.enQueue(i); } int pop() { int ret = 0; int result = 0; queue temp; int c = 0; while( ( ret = q.deQueue() ) != -1) { temp.enQueue(ret); result = ret; } q.reSet(); int size = temp.size(); if( size == 0 ) { cout<<"stack is empty " <<endl; return -1; } else { size--; } while(size--) q.enQueue(temp.deQueue()); temp.reSet(); return result; } void disp() { q.disp() ; } };0
srikiran G2 pointsPosted Jan 3, 2013, 2:47 AM EST
#include <iostream> #include <stdio.h> #define max 5 using namespace std; class queue { int front; int rear; int data[max]; public: queue() { front = 0; rear = 0; } void enQueue(int i) { if( rear == max ) return ; data[rear++] = i; } int deQueue() { if( front == rear ) { return -1; } return data[front++]; } void disp() { cout<<"Front ="<<front<<" Rear = "<<rear<<endl; } int size() { return rear; } int reSet() { front = 0; rear = 0; } }; //I am Stack also here class stack { queue q; public: void push( int i) { if( q.size() == max ) { cout<<"Stack is full"<<endl; return ; } q.enQueue(i); } int pop() { int ret = 0; int result = 0; queue temp; int c = 0; while( ( ret = q.deQueue() ) != -1) { temp.enQueue(ret); result = ret; } q.reSet(); int size = temp.size(); if( size == 0 ) { cout<<"stack is empty " <<endl; return -1; } else { size--; } while(size--) q.enQueue(temp.deQueue()); temp.reSet(); return result; } void disp() { q.disp() ; } };