-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathLinkedList.cs
182 lines (164 loc) · 4.47 KB
/
LinkedList.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SinglyLinkedList
{
class Node
{
public int data;
public Node next;
public Node(int value)
{
data = value;
next = null;
}
}
class LinkedList
{
private Node head;
public int length;
public void AddNodeToFront(int value)
{
Node nodeToAdd = new Node(value);
nodeToAdd.next = head;
head = nodeToAdd;
length++;
}
public void AppendToLast(int value)
{
if (head == null)
{
head = new Node(value);
head.next = null;
length++;
}
else
{
Node nodeToAdd = new Node(value);
Node current = head;
while (current.next != null)
{
current = current.next;
}
current.next = nodeToAdd;
length++;
}
}
public void Insert(int index, int value)
{
if (index == 0)
{
AddNodeToFront(value);
}
else if (index >= length)
{
AppendToLast(value);
}
else
{
int counter = 0;
Node newNode = new Node(value);
Node pointer = head;
Node temp;
length++;
while (pointer != null)
{
if (counter == index - 1)
{
temp = pointer.next;
pointer.next = newNode;
newNode.next = temp;
break;
}
else
{
pointer = pointer.next;
counter++;
}
}
}
}
public void Remove(int index)
{
if (index == 0)
{
Node current = head.next;
head.next = null;
head = current;
length--;
}
else
{
if (index >= length)
index = length - 1;
Node current = head;
int counter = 0;
Node temp;
while (current != null)
{
if (counter == index - 1)
{
temp = current.next;
current.next = temp.next;
length--;
break;
}
else
{
current = current.next;
counter++;
}
}
}
}
public void Reverse()
{
if (length > 1)
{
Node first = head;
Node second = first.next;
Node temp;
while (second != null)
{
temp = second.next;
second.next = first;
first = second;
second = temp;
}
head.next = null;
head = first;
}
}
public void PrintAllNodes()
{
Node point = head;
while (point != null)
{
Console.WriteLine(point.data);
point = point.next;
}
}
}
class Program
{
static void Main(string[] args)
{
LinkedList myLinkedList = new LinkedList();
myLinkedList.AddNodeToFront(8);
myLinkedList.AppendToLast(7);
myLinkedList.AppendToLast(6);
myLinkedList.Insert(2, 4);
myLinkedList.Insert(3, 12);
myLinkedList.Remove(1);
Console.WriteLine("Linked list before reversal:");
myLinkedList.PrintAllNodes();
myLinkedList.Reverse();
Console.WriteLine("Linked list after reversal:");
myLinkedList.PrintAllNodes();
Console.WriteLine("Length of the linked list: {0}", myLinkedList.length);
Console.Read();
}
}
}