Stacks and Queues Lecture
Stacks and Queues Lecture
Introduction To Stack
Push operation
● Add an element to the top of the stack
Stack Operations
Stack Operations
Stack Operations
Stack Operations
Pop operation
● Remove element from the top of the stack
Stack Operations
Stack Operations
Stack Operations
Top
Peek operation
● returning the top element of a stack.
Is empty()
● Check if the list is empty or not.
● If it’s empty return True else False.
Implementation
#include <vector>
#include <stdexcept>
#include <iostream>
int top() const {
class Stack { if (data.empty()) {
private: throw
std::vector<int> data; std::out_of_range("top() called on
public:
empty stack");
Stack() {}; }
return data.back();
void push(int x) { }
data.push_back(x);
}
bool empty() const {
int pop() { return data.empty();
if (data.empty()) {
}
throw std::out_of_range("pop() called on
empty stack"); };
}
int val = data.back();
data.pop_back();
return val;
}
Can we implement Stack with a
different data structure?
Implementing stack using linked list
Push operation
● Initialise a node
● Otherwise make pointer let say temp to the top node and move forward the top node by 1
step
● Now free this temp node
Removed node
Top operation
● Check if there is any node present or not, if not then return.
● Otherwise return the value of top node of the linked list which is the node at Head
Implementation
#include <stdexcept>
// Destructor to clean up any remaining nodes // Pop the top element off the stack and return its value
~Stack() { // Throws std::out_of_range if the stack is empty
while (head) { int pop() {
Node* tmp = head; if (isempty()) {
head = head->next; throw std::out_of_range("pop() called on empty stack");
delete tmp; }
Node* popped_node = head;
}
int value = popped_node->data;
} head = head->next;
delete popped_node;
// Check whether the stack is empty return value;
bool isempty() const { }
return head == nullptr; };
}
/
Time and space complexity
● Push
○ Time complexity - ___?
● Pop
○ Time complexity - ___?
● Peek
○ Time complexity - ___?
● isEmpty()
○ Time complexity - ___?
Time and space complexity
● Push
○ Time complexity - O(1)
● Pop
○ Time complexity - O(1)
● Peek
○ Time complexity - O(1)
● isEmpty()
○ Time complexity - O(1)
Applications of Stack
Reflection: Stack can help you simulate deletion of
elements in the middle in O(1) time complexity
22
Reflection: stack can help you defer decision
until some tasks are finished.
23
Common PitFalls
● Stack overflow
A collection whose elements are added at one end (the rear) and
removed from the other end (the front)
Enqueue (Append)
● Add an element to the tail of a queue
● First In
Queue Operations
Dequeue (Popleft)
● Remove an element from the head of the queue
● First Out
Implementing Queue
class Queue {
std::vector<int> queue;
int headIndex;
public:
Queue(int k) : headIndex(0) {}
void append(int value) {
queue.push_back(value);
}
int pop() {
if (headIndex < queue.size()) {
int val = queue[headIndex];
headIndex++;
return val;
}
}
};
Implementing Queue
deque()
● Internally, deque() is implemented as a linked list of nodes
.pop()
.append()
.popleft()
.appendleft()
Time and space complexity
● Append
○ Time complexity - ____?
● Pop
○ Time complexity - ____?
● Peek
○ Time complexity - ____?
● isEmpty()
○ Time complexity - ____?
Time and space complexity
● Append
○ Time complexity - O(1)
● Pop
○ Time complexity - O(1)
● Peek
○ Time complexity - O(1)
● isEmpty()
○ Time complexity - O(1)
Stack Overview
Header:
🔹 std::stack<T> – Key Methods
🔹 #include <stack>
Header:
🔹 std::queue<T>
🔹 #include <queue>