Queues are data structures in computer science that look like lists in Python where you can insert and delete items. A queue is like stacks where you can insert and delete items in a specific order, but unlike stacks that follow the principle of last-in-first-out data structures, queues follow the principle of last-in-first-out data structures where the first item added is the first item removed.
You can think of Queues data structures as a line of people waiting to buy tickets for a show. Here the first person in line is the first to buy the first ticket and so on. So we can say that the queue data structure in computer science simulates the real queue.
A queue is like arrays where we work with a length that is one greater than the index of the last element in the array. In queues, we use a similar approach. In the section below, I’ll walk you through implementing queues using the Python programming language.
Below are the functions that a queue data structure provides:
- enqueue: it is used to insert a new item into the Queue.
- dequeue: it is used to remove an item from a queue.
- is_empty: it returns True if the queue is empty and returns false if the queue is not empty.
- size: as the name suggests, it returns the number of items in a queue.