- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathCircularLinkedList.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
usingSystem;
namespaceDataStructures.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>
publicclassCircularLinkedList<T>
{
/// <summary>
/// Points to the last node in the Circular Linked List.
/// </summary>
privateCircularLinkedListNode<T>?tail;
/// <summary>
/// Initializes a new instance of the <see cref="CircularLinkedList{T}"/> class.
/// </summary>
publicCircularLinkedList()
{
tail=null;
}
/// <summary>
/// Gets the head node (tail.Next) of the Circular Linked List.
/// </summary>
publicCircularLinkedListNode<T>?GetHead()
{
returntail?.Next;
}
/// <summary>
/// Determines whether the Circular Linked List is empty.
/// </summary>
/// <returns>True if the list is empty; otherwise, false.</returns>
publicboolIsEmpty()
{
returntail==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>
publicvoidInsertAtBeginning(Tdata)
{
varnewNode=newCircularLinkedListNode<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>
publicvoidInsertAtEnd(Tdata)
{
varnewNode=newCircularLinkedListNode<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>
publicvoidInsertAfter(Tvalue,Tdata)
{
if(IsEmpty())
{
thrownewInvalidOperationException("List is empty.");
}
varcurrent=tail!.Next;
do
{
if(current!.Data!.Equals(value))
{
varnewNode=newCircularLinkedListNode<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>
publicvoidDeleteNode(Tvalue)
{
if(IsEmpty())
{
thrownewInvalidOperationException("List is empty.");
}
varcurrent=tail!.Next;
varprevious=tail;
do
{
if(current!.Data!.Equals(value))
{
if(current==tail&¤t.Next==tail)
{
tail=null;
}
elseif(current==tail)
{
previous!.Next=tail.Next;
tail=previous;
}
elseif(current==tail.Next)
{
tail.Next=current.Next;
}
else
{
previous!.Next=current.Next;
}
return;
}
previous=current;
current=current.Next;
}
while(current!=tail!.Next);
}
}
}