-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_List_00.java
More file actions
145 lines (120 loc) · 3.28 KB
/
Copy pathLinked_List_00.java
File metadata and controls
145 lines (120 loc) · 3.28 KB
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
// Linked List Implementation
class Linked_List {
Node head;
int size;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public void Add_First(int data) {
size++;
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}
public void Add_Last(int data) {
size++;
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = newNode;
}
public void addInMiddle(int index, int data) {
if (index > size || index < 0) {
System.out.println("Invalid Index value");
return;
}
Node newNode = new Node(data);
if (head == null || index == 0) {
newNode.next = head;
head = newNode;
size++;
return;
}
Node currNode = head;
// Loop stops at index - 1 to insert after it
for (int i = 1; i < index; i++) {
currNode = currNode.next;
}
newNode.next = currNode.next;
currNode.next = newNode;
size++;
}
public void Remove_First() {
if (head == null) {
System.out.println("List is empty");
return;
}
size--;
head = head.next; // Simply move the head to the next node
}
public void Remove_Last() {
if (head == null) {
System.out.println("List is empty");
return;
}
size--;
// If there's only one node in the list
if (head.next == null) {
head = null;
return;
}
Node curr = head;
// Stop at the SECOND TO LAST node
while (curr.next.next != null) {
curr = curr.next;
}
// Disconnect the last node
curr.next = null;
}
public void Print_Linked_List() {
if (head == null) {
System.out.println("List is empty");
return;
}
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " -> ");
curr = curr.next;
}
System.out.println("NULL");
}
}
public class Linked_List_00 {
public static void main(String[] args) {
Linked_List list = new Linked_List();
list.Add_First(1);
list.Add_First(2);
list.Add_First(3);
list.Add_First(4);
list.Add_First(5);
// Expected: 5 -> 4 -> 3 -> 2 -> 1 -> NULL
list.Print_Linked_List();
// Expected: 4 -> 3 -> 2 -> 1 -> NULL
list.Remove_First();
list.Print_Linked_List();
// Expected: 4 -> 3 -> 2 -> NULL
list.Remove_Last();
list.Print_Linked_List();
// Expected: 5 -> 4 -> 3 -> 2 -> NULL
list.Add_First(5);
list.Print_Linked_List();
// Expected: 5 -> 4 -> 3 -> 2 -> 1 -> NULL
list.addInMiddle(4, 1);
list.Print_Linked_List();
}
}