Created
June 27, 2023 20:52
-
-
Save ulkomar/9673a3bd02a0152cd0508084a6c8fe1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node<T> { | |
var data: T | |
var next: Node? | |
init(data: T, next: Node? = nil) { | |
self.data = data | |
self.next = next | |
} | |
} | |
class LinkedList<T> { | |
var length = 0 | |
var head: Node<T>? | |
var tail: Node<T>? | |
func getData(at index: Int) -> T? { | |
if index >= length || index < 0 { print("Impossible to get"); return nil } | |
var previous = head | |
for _ in 0..<index { | |
previous = previous?.next | |
} | |
return previous?.data | |
} | |
func append(_ value: T) { | |
let newNode = Node(data: value) | |
if head == nil || tail == nil { | |
head = newNode | |
tail = newNode | |
} else { | |
tail?.next = newNode | |
tail = newNode | |
} | |
length += 1 | |
} | |
func insertAtHead(_ value: T) { | |
head = Node(data: value, next: head) | |
length += 1 | |
} | |
func delete(at index: Int) { | |
if index >= length || index < 0 { print("Impossible to delete"); return } | |
if index == 0 { head = head?.next } | |
var previous = head | |
for idx in 1..<index { | |
previous = previous?.next | |
if idx == index - 1 { | |
tail = previous | |
} | |
} | |
previous?.next = previous?.next?.next | |
length -= 1 | |
} | |
func insert(at index: Int, with value: T) { | |
if index >= length || index < 0 { print("Impossible to delete"); return } | |
let newNode = Node(data: value) | |
var previous = head | |
for _ in 0..<index { | |
previous = previous?.next | |
} | |
newNode.next = previous?.next | |
previous?.next = newNode | |
if index == length - 1 { | |
tail = previous?.next | |
} | |
length += 1 | |
} | |
func toArray() -> [T] { | |
var result = [T]() | |
var currentNode = self.head | |
while let node = currentNode { | |
result.append(currentNode!.data) | |
currentNode = node.next | |
} | |
return result | |
} | |
// func delete(at index: Int) { | |
// if index >= length || index < 0 { print("Impossible to delete"); return } | |
// if index == 0 { head = head?.next } | |
// | |
// var currentNode = head | |
// var previousNode = head | |
// | |
// for idx in 0...length { | |
// if idx == index { | |
// previousNode?.next = currentNode?.next | |
// if index == length - 1 { | |
// tail = previousNode | |
// } | |
// break | |
// } | |
// | |
// previousNode = currentNode | |
// currentNode = currentNode?.next | |
// } | |
// | |
// length -= 1 | |
// } | |
} | |
let linkedList = LinkedList<Int>() | |
linkedList.append(0) | |
linkedList.append(1) | |
linkedList.append(2) | |
linkedList.append(3) | |
linkedList.toArray() | |
linkedList.delete(at: 3) | |
linkedList.getData(at: 1) | |
linkedList.insert(at: 2, with: 10) | |
linkedList.toArray() | |
linkedList.insert(at: 1, with: 10) | |
linkedList.toArray() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment