Lecture 14 --- Stack and Queue
Today’s Lecture
- STL Queue and STL Stack
14.1 Additional STL Container Classes: Stacks
- A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle.
- Stacks allow access, insertion and deletion from only one end called the top.
- There is no access to values in the middle of a stack.
- Stacks may be implemented efficiently in terms of vectors and lists, although vectors are preferable.
- All stack operations are O(1).
14.1.1 Member functions of std::stack
- push(const T& value): Adds an element value to the top of the stack.
- pop(): Removes the top element from the stack.
- top(): Returns a reference to the top element of the stack without removing it.
- empty(): Checks if the stack is empty. Returns true if the stack is empty, false otherwise.
- size(): Returns the number of elements in the stack.
14.1.2 Stack Example Program
- Following is an example program, remember to include the stack library.
#include <iostream>
#include <stack>
int main() {
std::stack<int> myStack;
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);
myStack.push(50);
std::cout << "Size of stack: " << myStack.size() << std::endl;
std::cout << "Top element: " << myStack.top() << std::endl;
if (!myStack.empty()) {
std::cout << "Stack is not empty" << std::endl;
} else {
std::cout << "Stack is empty" << std::endl;
}
myStack.pop();
// What is the output of this next line?
std::cout << "Top element after pop: " << myStack.top() << std::endl;
return 0;
}
You can compile and run this above program.
14.1.3 Stack Implementation
We have the stack implementation and test code here: stack.h, stack_test.cpp.
14.2 Additional STL Container Classes: Queues
- A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle.
- Queues allow insertion at one end, called the back and removal from the other end, called the front.
- There is no access to values in the middle of a queue.
- Queues may be implemented efficiently in terms of a list. Using vectors for queues is also possible, but requires more work to get right.
- All queue operations are O(1).
14.2.1 Member functions of std::queue
- push(const T& value): Adds an element value to the rear of the queue. This operation is also known as enqueue.
- pop(): Removes the front element from the queue. This operation is also known as dequeue.
- front(): Returns a reference to the front element of the queue without removing it.
- empty(): Checks if the queue is empty. Returns true if the queue is empty, false otherwise.
- size(): Returns the number of elements in the queue.
14.2.2 Queue Example Program
- Following is an example program, remember to include the queue library.
#include <iostream>
#include <queue>
int main() {
std::queue<int> myQueue;
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
myQueue.push(40);
myQueue.push(50);
std::cout << "Size of queue: " << myQueue.size() << std::endl;
std::cout << "Front element: " << myQueue.front() << std::endl;
if (!myQueue.empty()) {
std::cout << "Queue is not empty" << std::endl;
} else {
std::cout << "Queue is empty" << std::endl;
}
myQueue.pop();
// What is the output of this next line?
std::cout << "Front element after pop: " << myQueue.front() << std::endl;
return 0;
}
You can compile and run this above program.
14.3 Leetcode Exercises
- Leetcode problem 225: Implement Stack using Queues. Solution: p225_stack_using_queues.cpp.
- Leetcode problem 232: Implement Queue using Stacks. Solution: p232_queue_using_stacks.cpp.
- Leetcode problem 20: Valid Parentheses. Solution: p20_valid_parentheses.cpp
- Leetcode problem 71: Simplify Path. Solution: p71_simplify_path.cpp