-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
151 lines (135 loc) · 4.3 KB
/
LinkedList.java
File metadata and controls
151 lines (135 loc) · 4.3 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
/*
* Name: Thomas Cheng
*
* Date: May 2, 2018
*
* Dsscription: This is the LinkedList<T> class that implements the LinkedListADT<T> class.
*/
class LinkedList<T> implements LinkedListADT<T> {
private ListNode<T> front = null;
private int numberOfNodes = 0;
// Returns true if the linked list has no nodes, or false otherwise.
@Override
public boolean isEmpty() {
return (front == null);
}
// Deletes all of the nodes in the linked list.
// Note: ListNode objects will be automatically garbage collected by JVM.
@Override
public void clear() {
front = null;
numberOfNodes = 0;
}
// Returns the number of nodes in the linked list
@Override
public int size() {
return numberOfNodes;
}
// Adds a node to the front of the linked list.
@Override
public void addFirst( T element ) {
front = new ListNode<T>( element, front );
numberOfNodes++;
}
// Returns a reference to the data in the first node, or null if the list is empty.
@Override
public T peekFirst() {
if ( isEmpty() )
return null;
return front.getData();
}
// Removes a node from the front of the linked list (if there is one).
// Returns a reference to the data in the first node, or null if the list is empty.
@Override
@SuppressWarnings("unchecked")
public T removeFirst() {
T tempData;
// if the list is empty
if ( isEmpty() )
return null;
// if the list is not empty
tempData = front.getData();
front = front.getNext();
numberOfNodes--;
return tempData;
}
// Add an element to the end of the linked list
@Override
@SuppressWarnings("unchecked")
public void addLast( T element ) {
ListNode<T> node = front;
ListNode<T> back = front;
// if the list is empty
if (front == null){
addFirst( element );
return;
}
else{
// searches until the end of list
while (node.getNext() != null){
node = node.getNext();
back = node.getNext();
}
// add the node to the end of the list
back = new ListNode( element, null );
node.setNext(back);
}
numberOfNodes++;
}
// Remove an element from the end of the list
@Override
@SuppressWarnings("unchecked")
public T removeLast(){
ListNode<T> node = front;
ListNode<T> end = front;
T element;
// list is empty
if (isEmpty())
return null;
// list has only 1 element
if (size() == 1){
element = node.getData();
front = null;
numberOfNodes--;
return element;
}
// list has 2+ elements
else{
end = end.getNext();
// searches for the end of the list
while (end.getNext() != null){
node = node.getNext();
end = end.getNext();
}
// removing the last node from the list
element = end.getData();
node.setNext(null);
numberOfNodes--;
return element;
}
}
// Returns true if the linked list contains a certain element, or false otherwise.
@Override
@SuppressWarnings("unchecked")
public boolean contains( T key ) {
ListNode<T> searchNode;
searchNode = front;
while ( ( searchNode != null ) && ( !key.equals( searchNode.getData() ) ) ) {
searchNode = searchNode.getNext();
}
return ( searchNode != null );
}
// Return String representation of the linked list.
@Override
@SuppressWarnings("unchecked")
public String toString() {
ListNode<T> node;
String linkedList = "FRONT ==> ";
node = front;
while (node != null) {
linkedList += "[ " + node.getData() + " ] ==> ";
node = node.getNext();
}
return linkedList + "NULL";
}
}