Our next Data Structure will be Queue. Previously we took a look on Array data structure. So, let’s check what is queue, its pros and cons and how it helps in programming
What is Queue
It is like a Stack stores elements one by one, but the crucial difference is the way to get the element. Let’s try to dig dipper. It use principal FIFO (First In First Out). You must get the idea even from its name. Like queue in real life. You got the firs place in line means you will be first. Second will be served second, etc. Definition is really simple.
- Enqueue – adds element to the end of queue
- Dequeue – remove element from the beginning of it
- isEmpty – returns if its empty
- isFull – check if its full
- Peek – returns first element without removing it
Basically we use both sides to operate with elements. The end is used to add elements. The other side is for removing them.
Complexity of queue operations
The complexity of enqueue and dequeue operations is O(1)
It can be used in different tasks as:
- Sync async processes
- Some ordered processing problems
Comparing to Stack data structure
- Uses FIFO approach
- Basic operations: Enqueue and Dequeue
- Solve order related processing problems
- Items are added from Rear and deleted from the Front end
- Uses LIFO approach
- Basic operations: Pop and Push
- Solve mostly some recursive problems
- Items are added/deleted only from one side Top of stack.