-
Notifications
You must be signed in to change notification settings - Fork 267
/
singlyLinkedList.swift
115 lines (102 loc) · 3.28 KB
/
singlyLinkedList.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Type: Equatable makes it so that you can have Nodes of any type and their values can be compared
// Use of generic "Type" makes it so you can have a Linked List of any type
class Node<Type: Equatable>
{
var value: Type
var next: Node?
init(value: Type) {
self.value = value
self.next = nil
}
}
class LinkedList<Type: Equatable> {
var head: Node<Type>?
init() {
self.head = nil
}
init (value: Type) {
insertLeft(value: value)
}
func insertLeft(value: Type) {
let node = Node(value: value)
node.next = self.head
self.head = node
}
func insertRight(value: Type) {
var currentNode = self.head
if (currentNode != nil) {
while currentNode?.next != nil {
currentNode = currentNode?.next
}
currentNode?.next = Node(value: value)
}
else { // Empty list
self.head = Node(value: value)
}
}
func printList() {
var currentNode = self.head
while currentNode != nil {
currentNode?.next == nil ? print(currentNode!.value) : print(currentNode!.value, terminator: ", ")
currentNode = currentNode!.next
}
}
func remove(value: Type) {
var currentNode = self.head
var previousNode: Node<Type>? = nil
while currentNode != nil && currentNode?.value != value {
previousNode = currentNode
currentNode = currentNode!.next
}
if (currentNode != nil) {
if (previousNode == nil) { // Item to be removed is the head of the list
self.head = currentNode?.next
}
else {
previousNode?.next = currentNode?.next
}
}
}
func pop(value: Type) -> Type? {
var currentNode = self.head
var previousNode: Node<Type>? = nil
while currentNode != nil && currentNode?.value != value {
previousNode = currentNode
currentNode = currentNode!.next
}
if (currentNode != nil) {
let returnValue: Type
if (previousNode == nil) { // Item to be removed is the head of the list
returnValue = currentNode!.value
self.head = currentNode?.next
}
else {
returnValue = currentNode!.value
previousNode?.next = currentNode?.next
}
return returnValue
}
else {
return nil
}
}
}
func main() {
// Example of creating a linked list of integers
let integersLinkedList: LinkedList<Int> = LinkedList()
integersLinkedList.insertLeft(value: 2)
integersLinkedList.insertLeft(value: 1)
integersLinkedList.insertRight(value: 3)
integersLinkedList.printList()
// Example of creating a linked list of Strings
let stringsLinkedList: LinkedList<String> = LinkedList()
stringsLinkedList.insertLeft(value: "String 1")
stringsLinkedList.insertRight(value: "String 2")
stringsLinkedList.insertRight(value: "String 3")
// Removing an element
stringsLinkedList.remove(value: "String 2")
// Popping an element
stringsLinkedList.pop(value: "String 1")
stringsLinkedList.printList()
}
main()