Junior — Middle
Can you list the main types of queues encountered in programming?
sobes.tech AI
Answer from AI
In programming, the following main types of queues are commonly encountered:
- FIFO (First In, First Out) — a classic queue where elements are processed in the order they arrive. For example, a task queue for execution.
- LIFO (Last In, First Out) — a stack where the last added element is retrieved first.
- Priority queue — elements have priorities, and the element with the highest priority is retrieved regardless of the order of addition.
- Deque (Double-Ended Queue) — allows adding and removing elements from both ends.
In iOS, GCD (Grand Central Dispatch) queues are often used for asynchronous programming:
- Main queue — a serial queue that executes tasks on the main thread, used for UI updates.
- Global queues — concurrent queues with different priorities (background, userInitiated, etc.).
- Custom serial queues — serial queues for ordered execution of tasks.
Example of creating and using a queue in Swift:
let serialQueue = DispatchQueue(label: "com.example.serialQueue")
serialQueue.async {
print("Executing task in serial queue")
}