Updated June 9, 2023
Introduction to Swift Queue
The Swift queue is the same as any other programming language. The queue is a data structure used to store element; in the queue, we can store data from one end and delete it using another end. Queue works and follows FIFO, which means first in, first out. We have so many examples where we do this thing like when we buy tickets from any station etc. Queue in swift support operation, which is used to handle the data or elements.
Syntax:
As we know, queue in swift is used to store the data. To store any data, we require predefined methods from the queue and to delete data, there are some methods from the queue, but first, we will see the syntax of how we can define a queue in swift while doing programming.
var variable_name:[Type] = []
As you can see in the above line of syntax we it is very simple to define a queue just like any other collection. First, we have to give the variable name followed by the type it is going to store, and we can initialize it if we want.
Example:
Code:
var myqueue :[Int] = []
Here we will see how we can add elements and remove elements from the queue in swift.
How to Implement Queue in Swift?
As we already know, that queue is a data structure that is used to store elements. To perform any operation on queue, it provides us with a different method for this. It is predefined, or we can say in build in swift, so we do not require to include any library for this. We can perform different operations like an insertion in the queue and delete an element from the queue.
Here we will see both these methods available in a queue in detail.
1. Enqueue
This is the method available in the queue. This method is used to add elements to the queue. We can simply call this method on the queue or array we created.
syntax:
var myqueue : [Int] = []
myqueue.append(your element here )
As you can see in the above syntax for enqueue method, we are calling this method on the queue variable we have created in the first line of code. We can add as many elements as we want to the queue variable.
2. Dequeue / Remove
This method is used to remove an element from the queue. This is also a predefined method available in a queue data structure of swift.
Syntax:
var myqueue : [Int] = []
myqueue.remove(your element here )
As you can see in the above syntax for the dequeue method, we are calling this method on the queue variable we have created in the first line of code. We can remove as many elements as we want to the queue variable. To perform the dequeue operation, we have to call the remove() method on it.
Now we will see one example to use both the method in the program to know it internal working in detail.
Example:
Code:
import Foundation
import Glibc
import Foundation
struct Queue{
var myqueue:[Int] = []
mutating func enqueue(element: Int)
{
myqueue.append(element)
}
}
var obj = Queue()
obj.enqueue(element : 200)
print(obj)
As you can see in the above lines of code, we are creating a struct Queue to use the queue functionality. Inside the struct, we are carpeting our object named as ‘myqueue’, which takes up only the Integer value as the data to store in the queue. After this, we are trying to use the ‘enqueue’ function from the queue in swift. This function will add a new element to the queue object we have created. To add the object, we have the append method available. We just have to call the append method on the queue object; inside this method, we can pass our element to the object.
How to remove an element from the queue in swift?
Code:
import Foundation
import Glibc
import Foundation
struct Queue{
var myqueue:[Int] = []
mutating func enqueue(element: Int)
{
myqueue.append(element)
}
mutating func dequeue() -> Int?
{
if myqueue.isEmpty {
return nil
}
else{
myqueue.remove(at: 0)
return 0
}
}
}
var obj = Queue()
obj.enqueue(element : 200)
print(obj)
obj.dequeue()
print(obj)
As you can see from the above lines of code, we are creating a queue. All the things are the same, but now we have added one new function to our queue and is that it removes the element from the queue. If our queue object is empty, then it will return NIL; otherwise, we will call the remove() method to remove the particular element from the queue. To call this method, we can create the object and remove them from the queue.
Example of Swift Queue
In this example, we are trying to create a queue in swift and adding elements to it. We are creating several queue objects and adding the same object to show how we can use this while programming.
Code:
import Foundation
import Glibc
import Foundation
struct Queue{
var myqueue1 :[Int] = []
var myqueue2 :[Int] = []
var myqueue3 :[Int] = []
mutating func enqueue(element: Int)
{
myqueue1.append(element)
myqueue2.append(element)
myqueue3.append(element)
}
}
print("Demo to show the queue ceration in swift !!")
var obj = Queue()
obj.enqueue(element : 200)
obj.enqueue(element : 200)
obj.enqueue(element : 200)
obj.enqueue(element : 200)
obj.enqueue(element : 200)
print("prinitng result of queue !!")
print(obj)
Output:
Conclusion
A queue is used to store element as we know. It comes up with two methods that can be used to perform operations on the queue element named as ‘enqueue’ and ‘dequeue’. They are very easy to implement, readable, and convenient to handle by the developers.
Recommended Articles
We hope that this EDUCBA information on “Swift Queue” was beneficial to you. You can view EDUCBA’s recommended articles for more information.