My Journey to Learning Data Structures from Scratch —Queue
This is Day 4 of #100DaysOfDSA —Queue Data Structure
Just like Stack, Queue also means what the literal meaning of the word “queue” means — a collection of objects following the property of First-In-First-Out. Think of the queue at the burger store next door. The person who comes first, will get his order placed first. Some practical examples that use this are, Process scheduling, Multiple producer — Single Consumer setup are some of the many places that implement queue at the back-end.
Common operations used to play around with this data structure are —
- Enqueue— Alias for Insertion of an element in the queue.
- Dequeue— Alias for Deletion of an element from the queue.
- Get Front— Returns the first element of the queue.
- Get Rear — Returns last element of the queue.
- Is Full — Checks if the queue is full or not — Useful for checking Overflow condition
- Is Empty — Checks if the queue is empty or not — Useful for checking Underflow condition
- Size — Returns the size(number of elements) of the queue.
Let’s take an example and visually understand the working of all the above-mentioned operations —
Queue can be implemented as Array, Linked List, Strings, etc, anything where you can implement and have a control on the FIFO property, is essentially a Queue.
So, some of the edge cases that must be part of the implementation are to check for Underflow and Overflow issue. Underflow is when you try to pop or access in the situations where the queue is already empty. Overflow happens when you try to insert in an already full-queue. Overflow is relatively unlikely to happen because most of the library implementations have dynamic allocation already implemented as part of the package. But, if you are writing Queue class, on your own, then beware.
Points to Remember
- First In First Out in nature.
- Handle Underflow and Overflow.
Practice Problems
Please star and clone my GitHub Repository — “Learning Data Structures from Scratch” to get notified on whenever I update the repository next.
In the last post, we have already seen some problems on Stacks. In this post, we covered theory on queue and 1 problem where we define Queue class using arrays which i have already put in the GitHub Repository. Feel free to check it out!