forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
155 lines (137 loc) · 3.54 KB
/
Copy pathLinkedList.java
File metadata and controls
155 lines (137 loc) · 3.54 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
146
147
148
149
150
151
152
153
154
155
public class LinkedList {
public class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public static Node head = null;
public static Node tail = null;
public static int size = 0;
public void addFirst(int data) {
Node newNode = new Node(data);
if(head == null) {
head = tail = newNode;
size++;
return;
}
newNode.next = head;
head = newNode;
size++;
}
public void addMiddle(int idx, int data) {
if(head == null || idx == 0) {
addFirst(data);
return;
} else if(idx >= size) {
addLast(data);
return;
}
Node newNode = new Node(data);
Node temp = head;
for(int i=0; i<idx-1; i++) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
size++;
}
public void addLast(int data) {
Node newNode = new Node(data);
if(head == null) {
head = tail = newNode;
size++;
return;
}
tail.next = newNode;
tail = newNode;
size++;
}
public int removeFirst() {
if(head == null) {
System.out.println("Linked List is empty");
return -1;
} else if(head.next == null) {
int val = head.data;
head = tail = null;
size--;
return val;
}
int val = head.data;
head = head.next;
size--;
return val;
}
public int removeMiddle(int idx) {
if(head == null || idx == 0) {
return removeFirst();
} else if(idx >= size-1) {
return removeLast();
} else {
Node temp = head;
for(int i=0; i<idx-1; i++) {
temp = temp.next;
}
int val = temp.next.data;
temp.next = temp.next.next;
size--;
return val;
}
}
public int removeLast() {
if(head == null) {
System.out.println("Linked List is empty");
return -1;
} else if(head.next == null) {
int val = head.data;
head = tail = null;
size--;
return val;
}
Node temp = head;
while(temp.next != tail) {
temp = temp.next;
}
int val = tail.data;
tail = temp;
tail.next = null;
size--;
return val;
}
public void print() {
if(head == null) {
System.out.println("Linked List is empty");
return;
}
Node temp = head;
while(temp != null) {
if(temp.next == null) {
System.out.print(temp.data);
return;
}
System.out.print(temp.data + " -> ");
temp = temp.next;
}
}
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.addFirst(2);
ll.addLast(3);
ll.addMiddle(0, 1);
ll.print();
System.err.println();
System.out.println("LinkedList size: " + size);
System.err.println();
ll.removeMiddle(1);
ll.print();
System.err.println();
ll.removeFirst();
ll.removeLast();
ll.print();
System.err.println();
System.out.println("LinkedList size: " + size);
System.err.println();
}
}