- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift Program to Implement the queue data structure
A queue is a data structure which worked upon FIFO(first in first out) principle. In a queue, both ends are open, so that we can add a new element from one end called the rear or tail, this operation is known as enqueue and remove element from another end called the front or head, this operation is known as dequeue.
Although Swift does not support any in-built queue data structure, still we can implement queue using various ways like link-list, structure, class, array, etc. You can use any of the methods to implement the queue data structure according to your requirements.
Queue supports the following operations −
Enqueue − It is used to add elements in the queue from the rare or tail end. So in Swift, we achieve enqueue operation by the append() method.
Syntax
func Enqueue(_ items: T) { item.append(items) }
Here, the append() function adds a new element in the queue.
Dequeue − It is used to remove elements from the queue from the head or front end. So in Swift, we achieve a dequeue operation by the removeLast() method.
Syntax
func Dequeue() -> T? { if item.isEmpty{ return nil } else { return item.removeFirst() } }
Here, the removeFirst() function is used to remove elements from the queue.
Top − It is used to get the peek element from the queue. So in Swift, we achieve top operation by using the first property.
Syntax
func Top() -> T? { return item.first }
Here, the first property is used to find the peek element from the stack.
isEmpty − It is used to check if the queue is empty or not. So in Swift, we achieve the isEmpty operation by using the isEmpty property.
Syntax
func IsEmpty() -> Bool { return item.isEmpty }
Here, the isEmpty property is used to check if the queue is empty or not. If it returns true, then that means the stack is empty. Otherwise false.
Size − It is used to check the size of the queue. So in Swift, we achieve size operation by using the count property.
Syntax
func Size()->Int{ return item.count }
Here, the count property is used to find the total number of elements present in the queue.
Note − The implementation of a queue using an array is not the most efficient method due to frequent enqueue and dequeue operations.
Example 1
In the following Swift program, we will implement a queue data structure using structure. Here we define a queue structure with enqueue, dequeue, top, size and isEmpty methods. Then we create an instance of the queue struct to add some elements in the queue using the Enqueue() function and then remove them in FIFO order using the Dequeue() function. Finally, display the output.
import Foundation import Glibc // Implementing stack using structure struct queue<T> { private var item = [T]() mutating func Enqueue(_ items: T) { item.append(items) } mutating func Dequeue() -> T? { if item.isEmpty{ return nil } else { return item.removeFirst() } } func IsEmpty() -> Bool { return item.isEmpty } func Size()->Int{ return item.count } func Top() -> T? { return item.first } } var myQueue = queue<Int>() myQueue.Enqueue(34) myQueue.Enqueue(42) myQueue.Enqueue(1) myQueue.Enqueue(89) myQueue.Enqueue(32) myQueue.Enqueue(8) print("Size of the queue is:", myQueue.Size()) print("Top Element of the queue is:", myQueue.Top()!) print("Dequeue Elements are:") while !myQueue.IsEmpty() { if let c = myQueue.Dequeue() { print(c) } }
Output
Size of the queue is: 6 Top Element of the queue is: 34 Dequeue Elements are: 34 42 1 89 32 8
Example 2
In the following Swift program, we will implement a queue data structure using class. Here we define a queue class with enqueue, dequeue, top, size and isEmpty methods. Then we create an instance of the queue class to add some elements in the queue using the Enqueue() function and then remove them in FIFO order using the Dequeue() function. Finally, display the output.
import Foundation import Glibc // Implementing stack using class class queue<T> { private var item = [T]() func Enqueue(_ items: T) { item.append(items) } func Dequeue() -> T? { if item.isEmpty{ return nil } else { return item.removeFirst() } } func IsEmpty() -> Bool { return item.isEmpty } func Size()->Int{ return item.count } func Top() -> T? { return item.first } } var myQueue = queue<Character>() myQueue.Enqueue("Q") myQueue.Enqueue("U") myQueue.Enqueue("E") myQueue.Enqueue("U") myQueue.Enqueue("E") print("Size of the queue is:", myQueue.Size()) print("Top Element of the queue is:", myQueue.Top()!) print("Dequeue Elements are:") while !myQueue.IsEmpty() { if let c = myQueue.Dequeue() { print(c) } }
Output
Size of the queue is: 5 Top Element of the queue is: Q Dequeue Elements are: Q U E U E
Conclusion
So this is how we can implement queue data structure. A queue data structure is useful to schedule tasks, perform BFS algorithms, Asynchronous operations, etc. The flexibility and simplicity of the queue data structure make it more versatile for managing data and performing some specific algorithms very effectively.