Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 19.12.2014
- Problem 13 You are provided with two stacks, as well as Pop and Push functions over
- them. Implement queue i.e. Enqueue and Dequeue using the available operations.
- */
- #include<stdio.h>
- #include<vector>
- #include<iostream>
- #define max 1000
- using namespace std;
- class Stack {
- private:
- int size = 0;
- vector<int> items;
- public:
- Stack(){
- items = vector<int>(1);
- }
- bool IsEmpty(){
- return size == 0;
- }
- bool IsFull(){
- return false;
- //return size == max; // ?
- }
- int Pop(){
- int popItem = items[size - 1];
- size--;
- if ((size > 0) && (size == items.size() / 4)){
- items.resize(items.size() / 2);
- }
- return popItem;
- }
- void Push(int item){
- size++;
- items[size - 1] = item;
- if (size == items.size()){
- items.resize(items.size() * 2);
- }
- }
- int Top(){
- return items[size - 1];
- }
- };
- class Queue{
- private:
- Stack s1;
- Stack s2;
- public:
- Queue(){
- s1 = Stack();
- s2 = Stack();
- }
- void Enqueue(int item){
- s1.Push(item);
- }
- int Dequeue(){
- int retVal;
- if (s2.IsEmpty()){
- while (!s1.IsEmpty()){
- int item = s1.Pop();
- s2.Push(item);
- }
- }
- retVal = s2.Pop();
- return retVal;
- }
- bool IsEmpty(){
- return s1.IsEmpty()&&s2.IsEmpty();
- }
- };
- int main(){
- Queue q;
- for (int i = 0; i < 10; i++){
- q.Enqueue(i);
- }
- while (!q.IsEmpty()){
- cout << q.Dequeue() << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement