- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
Latest commit
151 lines (135 loc) · 4.3 KB
/
Copy pathLinkedList.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.
*/
classLinkedList<T> implementsLinkedListADT<T> {
privateListNode<T> front = null;
privateintnumberOfNodes = 0;
// Returns true if the linked list has no nodes, or false otherwise.
@Override
publicbooleanisEmpty() {
return (front == null);
}
// Deletes all of the nodes in the linked list.
// Note: ListNode objects will be automatically garbage collected by JVM.
@Override
publicvoidclear() {
front = null;
numberOfNodes = 0;
}
// Returns the number of nodes in the linked list
@Override
publicintsize() {
returnnumberOfNodes;
}
// Adds a node to the front of the linked list.
@Override
publicvoidaddFirst( Telement ) {
front = newListNode<T>( element, front );
numberOfNodes++;
}
// Returns a reference to the data in the first node, or null if the list is empty.
@Override
publicTpeekFirst() {
if ( isEmpty() )
returnnull;
returnfront.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")
publicTremoveFirst() {
TtempData;
// if the list is empty
if ( isEmpty() )
returnnull;
// if the list is not empty
tempData = front.getData();
front = front.getNext();
numberOfNodes--;
returntempData;
}
// Add an element to the end of the linked list
@Override
@SuppressWarnings("unchecked")
publicvoidaddLast( Telement ) {
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 = newListNode( element, null );
node.setNext(back);
}
numberOfNodes++;
}
// Remove an element from the end of the list
@Override
@SuppressWarnings("unchecked")
publicTremoveLast(){
ListNode<T> node = front;
ListNode<T> end = front;
Telement;
// list is empty
if (isEmpty())
returnnull;
// list has only 1 element
if (size() == 1){
element = node.getData();
front = null;
numberOfNodes--;
returnelement;
}
// 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--;
returnelement;
}
}
// Returns true if the linked list contains a certain element, or false otherwise.
@Override
@SuppressWarnings("unchecked")
publicbooleancontains( Tkey ) {
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")
publicStringtoString() {
ListNode<T> node;
StringlinkedList = "FRONT ==> ";
node = front;
while (node != null) {
linkedList += "[ " + node.getData() + " ] ==> ";
node = node.getNext();
}
returnlinkedList + "NULL";
}
}