Programmer's Python Data - Stacks, Queues and Deques |
Written by Mike James | ||||
Monday, 27 May 2024 | ||||
Page 2 of 3
The QueueThe queue data structure behaves is a similar way to a queue in the real world – people join at the end and leave at the front. The first element added to the queue is the first to leave and so it is also known as a First In First Out or FIFO stack. A queue doesn’t alter the order of items and is essentially nothing more than a buffer of items waiting to be processed. You can create the equivalent of a queue in Python using a list, but while appending an element to the end of the queue is fast, removing an element from the start is slow due to the need to move all of the elements up by one. For example, in: queue = [] queue.append("A") queue.append("B") queue.append("C") print(queue.pop(0)) print(queue.pop(0)) print(queue.pop(0)) the three appends add the elements to the end of the list as before, but now the pop operation takes the element at the start of the list. The result is: A B C which, as can be seen, is not reversed. A more efficient queue can be implemented using the deque object in the collections module - the reason for the name will become clear in the next section. The deque object has an append method that adds elements to the end of the queue (considered to be the right-hand end) and a popleft method which removes an element from the front of the queue (considered to be the left-hand end). Using the deque object we can implement the previous example as: from collections import deque queue = deque() queue.append("A") queue.append("B") queue.append("C") print(queue.popleft()) print(queue.popleft()) print(queue.popleft()) and it displays the items in the order that they joined the queue. The deque is faster than using a list and it also has methods that allow us to implement the next data structure – the double-ended queue. Double-Ended QueueA double-ended queue or deque is simply a queue that can accept new elements at either its start or its end and can pop elements from both ends. It is the most general of the stack or buffer objects and is capable of being used as a LIFO or FIFO stack by simply restricting where elements can be added and removed. It is is so called both as an abbreviation of double-ended queue and in reference to a deck of cards where you can place or take a card from either the top or the bottom of the deck. The methods related to adding and removing elements from a deque are:
Using these we can implement a LIFO stack by only using append and pop: from collections import deque stack = deque() stack.append("A") stack.append("B") stack.append("C") print(stack.pop()) print(stack.pop()) print(stack.pop()) and we have already used a deque to implement a simple queue in the previous section. As well as the simple append and pop methods there are also:
You can also use the deque as if it was a limited form of sequence, which means you can use in and indexing, but not slicing. |
||||
Last Updated ( Monday, 27 May 2024 ) |