-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
CircularLinkedList.cs
155 lines (140 loc) · 4.5 KB
/
CircularLinkedList.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
using System;
namespace DataStructures.LinkedList.CircularLinkedList
{
/// <summary>
/// CircularLinkedList.
/// @author Mohit Singh. <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
/// </summary>
/// <typeparam name="T">The generic type parameter.</typeparam>
public class CircularLinkedList<T>
{
/// <summary>
/// Points to the last node in the Circular Linked List.
/// </summary>
private CircularLinkedListNode<T>? tail;
/// <summary>
/// Initializes a new instance of the <see cref="CircularLinkedList{T}"/> class.
/// </summary>
public CircularLinkedList()
{
tail = null;
}
/// <summary>
/// Gets the head node (tail.Next) of the Circular Linked List.
/// </summary>
public CircularLinkedListNode<T>? GetHead()
{
return tail?.Next;
}
/// <summary>
/// Determines whether the Circular Linked List is empty.
/// </summary>
/// <returns>True if the list is empty; otherwise, false.</returns>
public bool IsEmpty()
{
return tail == null;
}
/// <summary>
/// Inserts a new node at the beginning of the Circular Linked List.
/// </summary>
/// <param name="data">The data to insert into the new node.</param>
public void InsertAtBeginning(T data)
{
var newNode = new CircularLinkedListNode<T>(data);
if (IsEmpty())
{
tail = newNode;
tail.Next = tail;
}
else
{
newNode.Next = tail!.Next;
tail.Next = newNode;
}
}
/// <summary>
/// Inserts a new node at the end of the Circular Linked List.
/// </summary>
/// <param name="data">The data to insert into the new node.</param>
public void InsertAtEnd(T data)
{
var newNode = new CircularLinkedListNode<T>(data);
if (IsEmpty())
{
tail = newNode;
tail.Next = tail;
}
else
{
newNode.Next = tail!.Next;
tail.Next = newNode;
tail = newNode;
}
}
/// <summary>
/// Inserts a new node after a specific value in the list.
/// </summary>
/// <param name="value">The value to insert the node after.</param>
/// <param name="data">The data to insert into the new node.</param>
public void InsertAfter(T value, T data)
{
if (IsEmpty())
{
throw new InvalidOperationException("List is empty.");
}
var current = tail!.Next;
do
{
if (current!.Data!.Equals(value))
{
var newNode = new CircularLinkedListNode<T>(data);
newNode.Next = current.Next;
current.Next = newNode;
return;
}
current = current.Next;
}
while (current != tail.Next);
}
/// <summary>
/// Deletes a node with a specific value from the list.
/// </summary>
/// <param name="value">The value of the node to delete.</param>
public void DeleteNode(T value)
{
if (IsEmpty())
{
throw new InvalidOperationException("List is empty.");
}
var current = tail!.Next;
var previous = tail;
do
{
if (current!.Data!.Equals(value))
{
if (current == tail && current.Next == tail)
{
tail = null;
}
else if (current == tail)
{
previous!.Next = tail.Next;
tail = previous;
}
else if (current == tail.Next)
{
tail.Next = current.Next;
}
else
{
previous!.Next = current.Next;
}
return;
}
previous = current;
current = current.Next;
}
while (current != tail!.Next);
}
}
}