Skip to content

Commit de971c5

Browse files
committed
Small tweaks to singly linked list.
1 parent 89aed30 commit de971c5

File tree

14 files changed

+19
-91
lines changed

14 files changed

+19
-91
lines changed

README.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ Often just using the built-in `Array`, `Dictionary`, and `Set` types is sufficie
110110

111111
### Lists
112112

113-
- Singly Linked List
114-
- Doubly Linked List
113+
- [Singly Linked List](Singly Linked List/). A list where each node only has a pointer to the next node.
114+
- Doubly Linked List. A list where each node has pointers to the previous and the next node.
115115

116116
### Trees
117117

SinglyLinkedList/LinkedListTests/LinkedListTests.xcodeproj/project.pbxproj renamed to Singly Linked List/LinkedListTests/LinkedListTests.xcodeproj/project.pbxproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
7B18E0E11C5E5A27005A2B8E /* SinglyLinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B18E0E01C5E5A27005A2B8E /* SinglyLinkedList.swift */; };
1011
E63E399B1C5D1963005F7332 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E63E399A1C5D1963005F7332 /* AppDelegate.swift */; };
1112
E63E399D1C5D1963005F7332 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E63E399C1C5D1963005F7332 /* Assets.xcassets */; };
1213
E63E39A01C5D1963005F7332 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = E63E399E1C5D1963005F7332 /* MainMenu.xib */; };
1314
E63E39AB1C5D1963005F7332 /* LinkedListTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E63E39AA1C5D1963005F7332 /* LinkedListTests.swift */; };
14-
E63E39BA1C5D19DF005F7332 /* SinglyLinkedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = E63E39B91C5D19DF005F7332 /* SinglyLinkedList.swift */; };
1515
/* End PBXBuildFile section */
1616

1717
/* Begin PBXContainerItemProxy section */
@@ -25,6 +25,7 @@
2525
/* End PBXContainerItemProxy section */
2626

2727
/* Begin PBXFileReference section */
28+
7B18E0E01C5E5A27005A2B8E /* SinglyLinkedList.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SinglyLinkedList.swift; path = ../../SinglyLinkedList.swift; sourceTree = "<group>"; };
2829
E63E39971C5D1963005F7332 /* LinkedListTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LinkedListTests.app; sourceTree = BUILT_PRODUCTS_DIR; };
2930
E63E399A1C5D1963005F7332 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3031
E63E399C1C5D1963005F7332 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -33,7 +34,6 @@
3334
E63E39A61C5D1963005F7332 /* LinkedListTestsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LinkedListTestsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3435
E63E39AA1C5D1963005F7332 /* LinkedListTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedListTests.swift; sourceTree = "<group>"; };
3536
E63E39AC1C5D1963005F7332 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
36-
E63E39B91C5D19DF005F7332 /* SinglyLinkedList.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SinglyLinkedList.swift; sourceTree = "<group>"; };
3737
/* End PBXFileReference section */
3838

3939
/* Begin PBXFrameworksBuildPhase section */
@@ -57,7 +57,6 @@
5757
E63E398E1C5D1963005F7332 = {
5858
isa = PBXGroup;
5959
children = (
60-
E63E39B91C5D19DF005F7332 /* SinglyLinkedList.swift */,
6160
E63E39991C5D1963005F7332 /* LinkedListTests */,
6261
E63E39A91C5D1963005F7332 /* LinkedListTestsTests */,
6362
E63E39981C5D1963005F7332 /* Products */,
@@ -78,8 +77,9 @@
7877
children = (
7978
E63E399A1C5D1963005F7332 /* AppDelegate.swift */,
8079
E63E399C1C5D1963005F7332 /* Assets.xcassets */,
81-
E63E399E1C5D1963005F7332 /* MainMenu.xib */,
8280
E63E39A11C5D1963005F7332 /* Info.plist */,
81+
E63E399E1C5D1963005F7332 /* MainMenu.xib */,
82+
7B18E0E01C5E5A27005A2B8E /* SinglyLinkedList.swift */,
8383
);
8484
path = LinkedListTests;
8585
sourceTree = "<group>";
@@ -193,7 +193,7 @@
193193
isa = PBXSourcesBuildPhase;
194194
buildActionMask = 2147483647;
195195
files = (
196-
E63E39BA1C5D19DF005F7332 /* SinglyLinkedList.swift in Sources */,
196+
7B18E0E11C5E5A27005A2B8E /* SinglyLinkedList.swift in Sources */,
197197
E63E399B1C5D1963005F7332 /* AppDelegate.swift in Sources */,
198198
);
199199
runOnlyForDeploymentPostprocessing = 0;

SinglyLinkedList/README.md renamed to Singly Linked List/README.markdown

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
Singly linked lists are basic data structures that enable linking of related elements. Linked lists are not unlike arrays. They provide insertion, retrieval, updating, and removal of their elements. The elements of a linked list are referred to as 'nodes.' Each node has two properties: a value, and pointer the the next node in the list.They provide O(n) time for storage and lookup. See below for the memory implications and differences between singly and doubly linked lists.
44

5-
### Linked List vs. Array
5+
## Linked List vs. Array
66

77
One major difference between linked lists and arrays are that the elements of a linked list are not stored 'contiguously' in memory as in an array. This means that elements can be inserted or removed without having to reorganize their entire structure. Conversely, linked lists do not random access to the data or efficient indexing of their elements.
88

9-
#### Singly linked list vs. Doubly linked list
9+
## Singly linked list vs. Doubly linked list
10+
1011
A singly linked list's nodes contain only their key (or data) and a pointer to the next node in the list. A Doubly linked list contains nodes that have a key, a pointer to the next node, and also a pointer to the previous node in the list.
1112
Doubly linked lists require more memory per node, but can provide easier manipulation than singly linked lists.
1213

@@ -18,6 +19,7 @@ class Node {
1819
var next: Node?
1920
}
2021
```
22+
2123
Note the type of the 'next' property. If we are at the end of our list or if our list is empty, there is no next link. Because of this, 'next' is of type optional node. What if we want a linked list of something other than integers, or support nil-values for keys? We can make our data structure generic.
2224

2325
```swift
@@ -27,7 +29,8 @@ class Node<T> {
2729
}
2830
```
2931

30-
#### Head and Tail
32+
## Head and Tail
33+
3134
A linked list's first node is referred to as its 'head'. The term 'tail' can be used to refer to the last node in the list, or to all the nodes in the list after the head.
3235

3336
To create a linked list of nodes, we create a class with a generic type. Because we'll need to compare the nodes on the basis of equality, we constrain our generic type 'T' to conform to Swift's Equatable protocol.
@@ -39,6 +42,7 @@ class SinglyLinkedList<T: Equatable> {
3942
var head = Node<T>()
4043
}
4144
```
45+
4246
To support insertion of nodes to the list, we write a function that takes a key of type 'T'.
4347

4448
```swift
@@ -64,6 +68,7 @@ To support insertion of nodes to the list, we write a function that takes a key
6468
}
6569
}
6670
```
71+
6772
Before we can write a function that removes a node by its index, we'll need to know the count of nodes in our list. We'll also include a computed property that will tell us if a list is empty.
6873

6974
```swift
@@ -83,7 +88,6 @@ var count: Int {
8388
}
8489
```
8590

86-
8791
To support deletion of nodes in the list, we write a function that accepts the index of the node to remove. We'll have to reorganize the nodes that surround the link that is removed.
8892

8993
```swift
@@ -125,4 +129,7 @@ To support deletion of nodes in the list, we write a function that accepts the i
125129
}
126130
}
127131
```
128-
Linked lists are relatively simple structures, but can be used to implement other common data types like stacks and queues. It can be helpful to better understand how they differ from the Array type and what memory and efficiency implications are associated with their use.
132+
133+
Linked lists are relatively simple structures, but can be used to implement other common data types like stacks and queues. It can be helpful to better understand how they differ from the Array type and what memory and efficiency implications are associated with their use.
134+
135+
*Written for Swift Algorithm Club by [Mac Bellingrath](https://github.com/macbellingrath)*

0 commit comments

Comments
 (0)