|
| 1 | +#include<iostream> |
| 2 | +#include<queue> |
| 3 | + |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | + |
| 8 | +//IMPLEMENTING STACK USING 2 QUEUES |
| 9 | + |
| 10 | +queue<int> q1; |
| 11 | +queue<int> q2; |
| 12 | + |
| 13 | + |
| 14 | +class Stack { |
| 15 | + public: |
| 16 | + void Push(int data); //Takes-O(1) time complexity |
| 17 | + void Pop();//-Takes -O(n)- time complexity- as we transfer n items from Queue1 to Queue2 |
| 18 | + void Top(); |
| 19 | +}; |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +void Stack::Push(int data) { |
| 24 | + |
| 25 | + if(q1.empty()) { |
| 26 | + q2.push(data); //if queue 1 is empty, then enqueue data to queue 2 |
| 27 | + } |
| 28 | + else { //otherwise enqueue data to queue1 |
| 29 | + q1.push(data); |
| 30 | + } |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +void Stack::Pop() { |
| 36 | + int i=0,size; |
| 37 | + if(q1.empty() && q2.empty()) { |
| 38 | + cout<<"The stack is empty"<<endl; |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if(q2.empty()) { //case if queue1 is not empty |
| 43 | + |
| 44 | + size= q1.size(); |
| 45 | + while(i < q1.size()-1 ) //transfer n-1 items from queue1(storage Queue) to queue2(temporary queue) |
| 46 | + { |
| 47 | + q2.push(q1.front()); //enqueue n-1 items from Queue 1 to Queue2 |
| 48 | + q1.pop(); |
| 49 | + i++; |
| 50 | + } |
| 51 | + cout<<"Deleted "<<q1.front()<<" from top."<<endl; //queue1 now has only 1 item-which will be popped in LIFO fashion |
| 52 | + q1.pop(); |
| 53 | + } |
| 54 | + |
| 55 | + else { |
| 56 | + size=q2.size(); |
| 57 | + |
| 58 | + while(i < size-1) { //transfer n-1 items from Q2(storage queue) to Q1(temp queue) |
| 59 | + q1.push(q2.front()); //enqueue n-1 items from Queue2 to Queue1 |
| 60 | + q2.pop(); |
| 61 | + i++; |
| 62 | + } |
| 63 | + cout<<"Deleted "<<q2.front()<<" from top."<<endl; // Queue2 now has only 1 item which is popped |
| 64 | + q2.pop(); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +void Stack::Top() { |
| 72 | + int i=0,size,front; |
| 73 | + if(q1.empty() && q2.empty()) { |
| 74 | + cout<<"The stack is empty"<<endl; |
| 75 | + return ; |
| 76 | + } |
| 77 | + |
| 78 | + if(q2.empty()) { //case if queue1 is not empty |
| 79 | + |
| 80 | + size= q1.size(); |
| 81 | + while(i < q1.size()-1 ) //transfer n-1 items from queue1(storage Queue) to queue2(temporary queue) |
| 82 | + { |
| 83 | + q2.push(q1.front()); //enqueue n-1 items from Queue 1 to Queue2 |
| 84 | + q1.pop(); |
| 85 | + i++; |
| 86 | + } |
| 87 | + |
| 88 | + front=q1.front(); //storing last element of storage queue Q1 in front |
| 89 | + |
| 90 | + cout<<"Top of stack is "<<front<<endl; |
| 91 | + |
| 92 | + //inserting last element of Q1 to Q2 |
| 93 | + q2.push(q1.front()); |
| 94 | + q1.pop(); |
| 95 | + |
| 96 | + |
| 97 | + //now again transfer all back to storage queue Q1(storage queue) and make Q2 empty |
| 98 | + while(!q2.empty()) { |
| 99 | + q1.push(q2.front()); // |
| 100 | + q2.pop(); |
| 101 | + |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + else { |
| 108 | + size=q2.size(); |
| 109 | + |
| 110 | + while(i < size-1) { //transfer n-1 items from Q2(storage queue) to Q1(temp queue) |
| 111 | + q1.push(q2.front()); //enqueue n-1 items from Queue2 to Queue1 |
| 112 | + q2.pop(); |
| 113 | + i++; |
| 114 | + } |
| 115 | + front = q2.front(); |
| 116 | + |
| 117 | + cout<<"Top of stack is "<<front<<endl; |
| 118 | + |
| 119 | + |
| 120 | + //insert the last item in Q2(storage queue) to Q1(temp queue) |
| 121 | + q1.push(q2.front()); |
| 122 | + q2.pop(); |
| 123 | + |
| 124 | + |
| 125 | + //now again transfer all items back to Q2(storage queue) and make Q1 empty |
| 126 | + while(!q1.empty()) { |
| 127 | + |
| 128 | + q2.push(q1.front()); // |
| 129 | + q1.pop(); |
| 130 | + |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +int main() |
| 145 | +{ |
| 146 | + |
| 147 | + Stack s; |
| 148 | + int choice,data; |
| 149 | + while(1) |
| 150 | + { |
| 151 | + |
| 152 | + |
| 153 | + cout<<"\n1)Insert\n2)Delete\n3)Top of stack\n4)Exit\n"<<endl; |
| 154 | + cin>>choice; |
| 155 | + |
| 156 | + |
| 157 | + switch(choice) { |
| 158 | + |
| 159 | + case 1: |
| 160 | + cout<<"Enter data :"<<endl; |
| 161 | + cin>>data; |
| 162 | + s.Push(data); |
| 163 | + break; |
| 164 | + case 2: |
| 165 | + |
| 166 | + s.Pop(); |
| 167 | + break; |
| 168 | + case 3: |
| 169 | + s.Top(); |
| 170 | + break; |
| 171 | + |
| 172 | + default: |
| 173 | + exit(0); |
| 174 | + break; |
| 175 | + |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return 0; |
| 180 | +} |
0 commit comments