Junior — Middle
Can you list the main types of queues used in programming?
sobes.tech AI
Answer from AI
In programming, the main types of queues that are commonly used include:
- FIFO (First In, First Out) — a classic queue where elements are processed in the order they arrive.
- LIFO (Last In, First Out) — a stack where the last added element is removed first.
- Priority queue — elements are removed based on their priority rather than 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 task management:
- Serial queue — tasks are executed one at a time.
- Concurrent queue — tasks can be executed simultaneously.
Example of creating and using a serial queue in Swift:
let serialQueue = DispatchQueue(label: "com.example.serialQueue")
serialQueue.async {
print("Executing task 1")
}
serialQueue.async {
print("Executing task 2")
}