Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added hashmap.cpp
Empty file.
45 changes: 45 additions & 0 deletions queuestack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class MyQueue {

// stack used for pushing new elements
stack<int> inSt;

// stack used for popping and peeking elements
stack<int> 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();
}
};