-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentLinkedList.java
More file actions
95 lines (80 loc) · 2.1 KB
/
StudentLinkedList.java
File metadata and controls
95 lines (80 loc) · 2.1 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
public class StudentLinkedList {
private StudentNode head;
private StudentNode tail;
private int size;
// Adding values at FRONT
public void addFront(Student student) {
StudentNode node = new StudentNode(student);
if (head == null) {
tail = node;
} else {
head.setPrevious(node);
node.setNext(head);
}
head = node;
size++;
}
// Adding values at END
public void addToEnd(Student student) {
StudentNode node = new StudentNode(student);
if (tail == null) {
head = node;
} else {
tail.setNext(node);
node.setPrevious(tail);
}
tail = node;
size++;
}
// Removing values from FRONT
public StudentNode remvoeFromFront() {
if (isEmpty()) {
return null;
}
StudentNode removeNode = head;
if (head.getNext() == null) {
tail = null;
} else {
head.getNext().setPrevious(null);
}
head = head.getNext();
size--;
removeNode.setNext(null);
return removeNode;
}
// Removing values from END
public StudentNode removeFromEnd() {
if (isEmpty()) {
return null;
}
StudentNode removeNode = tail;
if (tail.getPrevious() == null) {
head = null;
} else {
tail.getPrevious().setNext(null);
}
tail = tail.getPrevious();
size--;
removeNode.setPrevious(null);
return removeNode;
}
// Getting the SIZE of the list
public int getSize() {
return size;
}
// Checking the is EMPTY or not
public boolean isEmpty() {
return head == null;
}
// Printing the list
public void printList() {
StudentNode current = head;
System.out.print("HEAD -> ");
while (current != null) {
System.out.print(current);
System.out.print(" <=> ");
current = current.getNext();
}
System.out.println("NULL");
}
}