forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.cs
157 lines (133 loc) · 4.77 KB
/
SinglyLinkedList.cs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
namespace DataStructures.LinkedList.SinglyLinkedList
{
public class SinglyLinkedList<T>
{
// points to the start of the list
private SinglyLinkedListNode<T>? Head { get; set; }
/// <summary>
/// Adds new node to the start of the list,
/// time complexity: O(1),
/// space complexity: O(1).
/// </summary>
/// <param name="data">Contents of newly added node.</param>
/// <returns>Added list node.</returns>
public SinglyLinkedListNode<T> AddFirst(T data)
{
var newListElement = new SinglyLinkedListNode<T>(data)
{
Next = Head,
};
Head = newListElement;
return newListElement;
}
/// <summary>
/// Adds new node to the end of the list,
/// time complexity: O(n),
/// space complexity: O(1),
/// where n - number of nodes in the list.
/// </summary>
/// <param name="data">Contents of newly added node.</param>
/// <returns>Added list node.</returns>
public SinglyLinkedListNode<T> AddLast(T data)
{
var newListElement = new SinglyLinkedListNode<T>(data);
// if head is null, the added element is the first, hence it is the head
if (Head is null)
{
Head = newListElement;
return newListElement;
}
// temp ListElement to avoid overwriting the original
var tempElement = Head;
// iterates through all elements
while (tempElement.Next is not null)
{
tempElement = tempElement.Next;
}
// adds the new element to the last one
tempElement.Next = newListElement;
return newListElement;
}
/// <summary>
/// Returns element at index <paramref name="index" /> in the list.
/// </summary>
/// <param name="index">Index of an element to be returned.</param>
/// <returns>Element at index <paramref name="index" />.</returns>
public T GetElementByIndex(int index)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
var tempElement = Head;
for (var i = 0; tempElement is not null && i < index; i++)
{
tempElement = tempElement.Next;
}
if (tempElement is null)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
return tempElement.Data;
}
public int Length()
{
// checks if there is a head
if (Head is null)
{
return 0;
}
var tempElement = Head;
var length = 1;
while (tempElement.Next is not null)
{
tempElement = tempElement.Next;
length++;
}
return length;
}
public IEnumerable<T> GetListData()
{
// temp ListElement to avoid overwriting the original
var tempElement = Head;
// all elements where a next attribute exists
while (tempElement is not null)
{
yield return tempElement.Data;
tempElement = tempElement.Next;
}
}
public bool DeleteElement(T element)
{
var currentElement = Head;
SinglyLinkedListNode<T>? previousElement = null;
// iterates through all elements
while (currentElement is not null)
{
// checks if the element, which should get deleted is in this list element
if (currentElement.Data is null && element is null ||
currentElement.Data is not null && currentElement.Data.Equals(element))
{
// if element is head just take the next one as head
if (currentElement.Equals(Head))
{
Head = Head.Next;
return true;
}
// else take the prev one and overwrite the next with the one behind the deleted
if (previousElement is not null)
{
previousElement.Next = currentElement.Next;
return true;
}
}
// iterating
previousElement = currentElement;
currentElement = currentElement.Next;
}
return false;
}
}
}