Skip to content

Commit 87e58de

Browse files
committed
Implement Singly Linked List
1 parent bc22f16 commit 87e58de

File tree

5 files changed

+154
-6
lines changed

5 files changed

+154
-6
lines changed

TODO.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Here is Data Structures base on https://en.wikipedia.org/wiki/List_of_data_struc
88

99
### Abstract data types
1010
- [x] ArrayList
11-
- [ ] List
11+
- [x] List
1212
- [ ] Tuple
1313
- [ ] Associative array, Map
1414
- [ ] Multimap
@@ -41,7 +41,7 @@ Here is Data Structures base on https://en.wikipedia.org/wiki/List_of_data_struc
4141
- [ ] Iliffe vector
4242
- [ ] Variable-length array
4343
- [ ] List
44-
- [ ] Simple Linked List
44+
- [x] Singly Linked List
4545
- [ ] Doubly Linked List
4646
- [ ] Circular Linked List
4747
- [ ] Doubly Circular Linked List

src/ConsoleApp/Program.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using DataStructures.AbstractDataTypes.Arrays;
2+
using DataStructures.AbstractDataTypes.Lists;
23

34
//ArrayUsage();
45
//ArrayListUsage();
5-
ListUsage();
6+
//ListUsage();
7+
LinkedListUsage();
68

79

810
void ArrayUsage()
@@ -28,8 +30,24 @@ void ListUsage()
2830
3,
2931
4
3032
};
33+
3134
foreach (var item in list)
3235
{
3336
Console.WriteLine(item);
3437
}
35-
}
38+
}
39+
40+
void LinkedListUsage()
41+
{
42+
var ll = new NikSinglyLinkedList<int>();
43+
ll.AddLast(5);
44+
ll.AddLast(2);
45+
ll.AddLast(3);
46+
47+
foreach (var i in ll)
48+
{
49+
Console.WriteLine(i);
50+
}
51+
52+
ll.Remove(3);
53+
}

src/DataStructures/AbstractDataTypes/Arrays/NikArrayList.cs renamed to src/DataStructures/AbstractDataTypes/Lists/NikArrayList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections;
22
// ReSharper disable All
33

4-
namespace DataStructures.AbstractDataTypes.Arrays;
4+
namespace DataStructures.AbstractDataTypes.Lists;
55

66
public class NikArrayList
77
{

src/DataStructures/AbstractDataTypes/Arrays/NikList.cs renamed to src/DataStructures/AbstractDataTypes/Lists/NikList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// ReSharper disable ConvertToAutoPropertyWithPrivateSetter
44

5-
namespace DataStructures.AbstractDataTypes.Arrays;
5+
namespace DataStructures.AbstractDataTypes.Lists;
66

77
public class NikList<T> : IEnumerable<T>
88
{
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using System.Collections;
2+
3+
namespace DataStructures.AbstractDataTypes.Lists;
4+
5+
public class NikSinglyLinkedList<T> : IEnumerable<T>
6+
{
7+
public class Node
8+
{
9+
public Node(T value)
10+
{
11+
Value = value;
12+
}
13+
14+
public Node? Next { get; set; }
15+
public T Value { get; }
16+
}
17+
18+
private Node? _head;
19+
private int _count;
20+
21+
public int Count => _count;
22+
23+
public void AddFirst(T value)
24+
{
25+
var newNode = new Node(value);
26+
27+
if (_head is null)
28+
{
29+
_head = newNode;
30+
_count++;
31+
return;
32+
}
33+
34+
newNode.Next = _head;
35+
_head = newNode;
36+
}
37+
38+
public void AddLast(T value)
39+
{
40+
var newNode = new Node(value);
41+
42+
if (_head is null)
43+
{
44+
_head = newNode;
45+
_count++;
46+
return;
47+
}
48+
49+
var current = _head;
50+
while (current.Next is not null)
51+
{
52+
current = current.Next;
53+
}
54+
55+
current.Next = newNode;
56+
_count++;
57+
}
58+
59+
public bool Contains(T value)
60+
{
61+
return Find(value) != null;
62+
}
63+
64+
public bool Remove(T value)
65+
{
66+
if (_head is null)
67+
return false;
68+
69+
var comparer = EqualityComparer<T>.Default;
70+
if (comparer.Equals(_head.Value, value))
71+
{
72+
_head = _head.Next;
73+
_count--;
74+
return true;
75+
}
76+
77+
var current = _head;
78+
var prev = _head;
79+
while (current is not null)
80+
{
81+
if (comparer.Equals(current.Value, value))
82+
{
83+
prev.Next = current.Next;
84+
_count--;
85+
return true;
86+
}
87+
88+
prev = current;
89+
current = current.Next;
90+
}
91+
92+
return false;
93+
}
94+
95+
private Node? Find(T value)
96+
{
97+
if (_head is null)
98+
return null;
99+
100+
var current = _head;
101+
var comparer = EqualityComparer<T>.Default;
102+
while (current is not null)
103+
{
104+
if (comparer.Equals(current.Value, value))
105+
return current;
106+
107+
current = current.Next;
108+
}
109+
110+
return null;
111+
}
112+
113+
public IEnumerator<T> GetEnumerator()
114+
{
115+
if (_head is null)
116+
yield break;
117+
118+
var current = _head;
119+
while (current is not null)
120+
{
121+
yield return current.Value;
122+
current = current.Next;
123+
}
124+
}
125+
126+
IEnumerator IEnumerable.GetEnumerator()
127+
{
128+
return GetEnumerator();
129+
}
130+
}

0 commit comments

Comments
 (0)