diff --git a/hashmap.cpp b/hashmap.cpp new file mode 100644 index 00000000..e69de29b diff --git a/queuestack.cpp b/queuestack.cpp new file mode 100644 index 00000000..ffa7a0c9 --- /dev/null +++ b/queuestack.cpp @@ -0,0 +1,45 @@ +class MyQueue { + + // stack used for pushing new elements + stack inSt; + + // stack used for popping and peeking elements + stack outSt; + +public: + + // constructor initializes empty stacks + MyQueue() {} + + // pushes element x to the back of the queue + void push(int x) { + inSt.push(x); + } + + // removes and returns the element from the front of the queue + int pop() { + // ensure out stack has the front element + peek(); + + int val = outSt.top(); + outSt.pop(); + return val; + } + + // returns the element at the front of the queue + int peek() { + // if out stack is empty, transfer all elements from in stack + if (outSt.empty()) { + while (!inSt.empty()) { + outSt.push(inSt.top()); + inSt.pop(); + } + } + return outSt.top(); + } + + // returns true if the queue is empty + bool empty() { + return inSt.empty() && outSt.empty(); + } +};