Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Stack & Queue
1. Level up your coding skills with
the C++ Standard Template
Library (STL):
Stack & Queue
BY JOYJIT CHOUDHURY
2. Stack
Stack is a container adapter that gives the programmer
the functionality of a stack - specifically, a LIFO (Last-In,
First–Out) data structure.
A container adapter such as stack is implemented on top
of an underlying container like list, deque or vector.
Defined in the header <stack>
Belongs to the namespace std
(remember : using namespace std)
5. Size of the stack
Member function size(), returns the number of elements
in the stack.
myStack
34
21
13
myStack.size() = 3
6. Accessing the top
myStack
34
21
13
Member function top(), returns a reference to the top element in the
stack. So, not only can you access it, you can also modify it’s value.
myStack
34
21
10myStack.top()
7. Pop
Member function pop(), removes the top element of the stack.
Effectively reducing the size of the stack by one.
myStack
34
21
myStack
34
21
13
myStack.pop()
8. Whether the stack is empty or not?
Member function empty() returns true if the stack is empty (i.e.
stack size is 0), else returns false
myStack myStack
34
21
13
myStack.empty()=true myStack.empty()=false
9. Queue
Queue is a container adapter that gives the programmer
the functionality of a queue - specifically, a FIFO (First-In,
First–Out) data structure.
Defined in the header <queue>
Belongs to the namespace std
(remember : using namespace std)
11. Enqueue
Member function push(), is used to enqueue elements to the back
of the queue.
myQ
myQ
myQ
myQ
87
87 43
87 43 13
12. Accessing the elements
front(), returns a reference to the first or the ‘oldest’ element in the
queue. It the same element which is removed during the dequeue
operation in a queue.
back(), returns a reference to the the last or the ‘newest’ element in
the queue.
myQ 87 43 13
myQ.front() myQ.back()
13. Dequeue
pop(), removes the first or the ‘oldest’ element in the queue.
myQ 87 43 13
myQ 43 13
myQ 13
14. Size of the queue
Member function size(), returns the number of elements in the queue.
myQ
myQ.size() = 0
myQ 87 43 13
myQ.size() = 3
15. Is the queue empty?
Member function empty() returns true if the queue is empty (i.e. queue
size is 0), else returns false
myQ
myQ.empty() = true
myQ 23 30 99
myQ.size() = false
16. That’s not all. There are many other functions
and techniques that could come in handy.
Read about them on cplusplus.com or
cppreference.com or somewhere else. Just
google it !