From 6d58228d1b83500fecd2acdd9f6b6c87b120d752 Mon Sep 17 00:00:00 2001 From: rbhargav0104 Date: Thu, 25 Dec 2025 13:26:23 +0530 Subject: [PATCH] Completed Design-2 --- hashmap.cpp | 0 queuestack.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 hashmap.cpp create mode 100644 queuestack.cpp 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(); + } +};