Becoming an Expert in Python (Ch.3)
In programming, a list is a fundamental data structure used to store and manipulate collections of elements. Three important types of lists are arrays, stacks, and queues, each serving distinct purposes.
An array is a fixed-size collection of elements stored in contiguous memory locations, allowing efficient indexing and retrieval. It is ideal for situations requiring fast access to elements based on their position.
A stack follows the Last In, First Out (LIFO) principle, meaning the last element added is the first to be removed. It is widely used in function calls, undo mechanisms, and backtracking algorithms.
A queue operates on the First In, First Out (FIFO) principle, ensuring that elements are processed in the order they arrive. This structure is essential in scheduling tasks, handling requests, and managing buffers.
Each of these structures plays a crucial role in optimizing data handling and algorithm efficiency in software development.
Simple Case: Queue in Python (Movie Ticket Line)
In real life, queue systems are used in various places, such as movie ticket counters. Below is a simple Queue (FIFO — First In, First Out) implementation in Python to simulate a movie ticket queue.
Get ainulyaqins’s stories in your inbox
Join Medium for free to get updates from this writer.
from collections import deque
# Create an empty queue
ticket_queue = [ ]
# Add customers to the queue
ticket_queue.append(“Alin”)
ticket_queue.append(“Boo”)
ticket_queue.append(“Chantique”)
# Display the current queue
print(ticket_queue)
# Serve the first customer in the queue
ticket_queue.pop(0)
print(ticket_queue)
# Alin out
# Serve the second customer in the queue and add another customer
ticket_queue.pop(0)
ticket_queue.append(“Rossa”)
#Boo out and Rossa in
Each data structure — arrays, stacks, and queues — serves a unique purpose in programming. Arrays provide direct access to elements using indexing, making them ideal for fast data retrieval. Stacks operate on a Last In, First Out (LIFO) basis, essential for tasks like function calls and undo operations. Queues, on the other hand, process data in a First In, First Out (FIFO) order, ensuring fairness in sequential execution. Mastering these fundamental structures is crucial for efficient algorithm design and effective software development.




