Before diving into the implementation of Linked List in Swift. Let’s start with the basics first.
A linked list is a linear data structure that is made up of different elements called Nodes.
What is Node in Linked List?
A Node is an object which consists of two properties which are value and next. Here, next is the pointer or reference to the next Node which is connected with the current Node.
Node Class in Swift
class Node<Element> {
var value: Element
var next: Node?
init(_ value: Element, next: Node? = nil) {
self.value = value
self.next = next
}
}
let a = Node<Int>(3)
let b = Node<Int>(6)
let c = Node<Int>(9)
a.next = b // Connecting a to b
b.next = c // Connecting b to c
Explanation
- In the above code, I have created a generic Node class. With this class, we can create any type of Node object that holds different data types as its value. So, it can anything like Int, String etc.
- The first property of the Node class is value. It is the value of the current Node. It is compulsory to have some value to initialize the Node class.
- The second property is next. It is the pointer or reference to the next Node. I have declared it as Optional. As it can be nil as well.
- In last, I have created three objects which are a, b and c. Node a is connected to Node b and Node b is connected to Node c.
Also Read – Guard Statement in Swift 5 with Examples – Ultimate Guide
Head and Tail Nodes in Linked List – Swift
Head Node – The first Node in the Linked List is called Head Node.
Tail Node – The last Node in the Linked List is called Tail Node.
Linked List Structure in Swift
struct LinkedList<Element> {
var head: Node<Element>?
var tail: Node<Element>?
init() { }
}
Explanation
- I have declared a Generic struct for the Linked List. Its a Homogenous Linked List which means that All the elements of the Linked List will be of same type.
- First property is head which is of type Node and it holds the reference of some Node.
- Second property is tail which is also of type Node and it holds the reference of some Node as well.
- In Singly Linked List, Tail Node’s next property is always Nil. This is the last node of the Linked List.
Last Words
I hope you will understand the Basics of Linked List in Swift programming language. In the next tutorial, we will learn about the Operations on Linked List.
If you have any doubts or suggestions regarding this article then do comment below.
Ashish says
We had written as –
import Foundation
— Editable Code —
class Node {
var data : Int
var next : Node? = nil
init(_ data: Int) {
self.data = data
}
}
class LinkedList {
var count : Int = 0;
var head : Node? = nil
func insert(data: Int) -> Void {
let next : Node? = head
head = Node(data)
head?.next = next
count += 1
}
}
— End Editable Code —
— Un Editable Code —
let stdout = ProcessInfo.processInfo.environment[“OUTPUT_PATH”]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let arrCount = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError(“Bad input”) }
var list = LinkedList()
if arrCount>0{
for _ in 1…arrCount {
guard let arrItem = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError(“Bad input”) }
list.insert(data:arrItem)
}
}
let count = list.count
fileHandle.write(String(count).data(using: .utf8)!)
var temp:Node! = list.head
while temp != nil {
fileHandle.write(“\n”.data(using: .utf8)!)
fileHandle.write(String(temp.data).data(using: .utf8)!)
temp = temp.next
}
— End Un Editable Code —
We getting output as –
Input as 5 1 2 3 4 5
Our Output as 5 5 4 3 2 1
Expected Output as 5 1 2 3 4 5