-
Notifications
You must be signed in to change notification settings - Fork 2
/
Doubly Linked List Implementation.cs
195 lines (182 loc) · 4.5 KB
/
Doubly Linked List Implementation.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System.Collections.Generic;
using System;
class Solution
{
static void Main(string[] args)
{
// do some examples code to test it
}
}
public class DoublyLinkedList
{
private class Node
{
public int data;
public Node next;
public Node prev;
public Node(int val)
{
this.data = val;
this.next = null;
this.prev = null;
}
}
Node head = null;
public void Append(int val)
{
Node newNode = new Node(val);
if (head == null)
{
head = newNode;
}
else
{
Node temp = head;
while (temp.next != null)
{
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
}
}
public void Insert_at_pos(int val, int pos)
{
Node newNode = new Node(val);
// empty LinkedList
if (head == null)
{
head = newNode;
return;
}
if (pos == 0)
{
newNode.next = head;
head.prev = newNode;
head = newNode;
return;
}
Node temp = head;
for (int i = 0; i < pos && temp.next != null; i++) // to get the node before target node , second condition to make sure it's still at range of linkedlist
{
temp = temp.next;
}
// special case if user entered position bigger than the range of linked list
if (temp == null)
{
this.Append(val); // append it to last of list,don't care about position
return;
}
newNode.prev = temp.prev;
temp.prev.next = newNode;
temp.prev = newNode;
newNode.next = temp;
}
public void Display()
{
Node temp = head;
while (temp != null)
{
Console.WriteLine(temp.data);
temp = temp.next;
}
}
public void Remove(int val)
{
if (head == null)
{
return;
}
Node temp = head;
if (head.data == val)
{
head = head.next;
if (head != null) // if the linked list have only one Node Skip this condition
{
head.prev = null;
}
return;
}
while (temp != null && temp.data != val)
{
temp = temp.next;
}
if (temp == null) // finished searched and don't found target Node
{
return;
}
else
{
temp.prev.next = temp.next;
if (temp.next != null) // if the deleted element at the last of list Skip this condition
{
temp.next.prev = temp.prev;
}
}
}
public void Remove_at_pos(int pos)
{
if (head == null)
{
return;
}
if (pos == 0)
{
head = head.next;
if (head != null) // if the linked list have only one Node Skip this condition
{
head.prev = null;
}
return;
}
else
{
Node temp = head;
for (int i = 0; i < pos && temp.next != null; i++) // to get the node before target node , second condition to make sure it's still at range of linkedlist
{
temp = temp.next;
}
if (temp == null) // finished searched and don't found target Node
{
return;
}
else
{
temp.prev.next = temp.next;
if (temp.next != null) // if the deleted element at the last of list Skip this condition
{
temp.next.prev = temp.prev;
}
}
}
}
public void Reverse()
{
Node temp = head;
// to get final Node
while (temp.next != null)
{
temp = temp.next;
}
// return the list in reverse way
while (temp != null)
{
temp = temp.prev;
}
}
public void Reverse_Display()
{
Node temp = head;
// to get final Node , store it in Temp
while (temp.next != null)
{
temp = temp.next;
}
// return the list in reverse way
while (temp != null)
{
Console.WriteLine(temp.data);
temp = temp.prev;
}
}
}