-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.java
More file actions
198 lines (151 loc) · 4.7 KB
/
List.java
File metadata and controls
198 lines (151 loc) · 4.7 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
public abstract class List implements MyCollectionInterface<T> {
// Declare variables
private Node head;
private Node firstNode;
private int numberOfEntries;
private boolean result;
//*****************************************************************
/**
* Adds a new entry to the LinkedList
* @param newItem
* @return True if successful; false if not
*/
public boolean add(T newItem) {
Node newNode = new Node(newItem);
newNode.next = head;
head = newNode;
if(head.equals(newNode)) {
numberOfEntries++;
}
return result;
} // End add
//*****************************************************************
/**
*
* @param givenPosition
* @param newItem
* @return True if addition was succcessful; false if not
*/
public boolean add(int givenPosition, T newItem) {
result = false;
if((givenPosition >= 0) && (givenPosition <= numberOfEntries)) {
Node<T> newNode = newNode<T> (newItem);
if(givenPosition == 0)
{
newNode.setNextNode = head;
head = newNode;
numberOfEntries++;
add(newItem);
result = true;
} else {
Node<T> currentNode = head;
for(int i = 1; i < givenPosition; i++)
{
currentNode = currentNode.next;
} // End for loop
newNode.next = currentNode.next;
currentNode.next = newNode;
numberOfEntries++;
} // End else
} else {
throw new IndexOutOfBoundsException("The given position cannot be found.");
} // End else
return result;
} // End add method
//*****************************************************************
/**
* Removes an occurance of a given entry from the LinkedList
* @param The entry that is to be removed
* @return True if removal was successful; false if not
*/
public boolean remove(T anEntry) {
result = false;
// Store head node
Node temp = head;
//If head needs to be removed
if(numberOfEntries == 0)
{
temp.setNextNode(head);
return true;
}
return false;
} // End remove
//*****************************************************************
/**
* Removes all entries from the list
*/
public void clear() {
head = null;
numberOfEntries = 0;
} // End clear
//*****************************************************************
/**
* Gets the current number of entries in list
* @return The number of entries
*/
public int getCurrentSize() {
return numberOfEntries;
} // End getCurrentSize
//*****************************************************************
/**
* See if list is empty
* @return True if empty; false if not
*/
public boolean isEmpty() {
if(numberOfEntries == 0) {
return true;
} // End if
return false;
} // End isEmpty
//*****************************************************************
/**
* Counts the number of times a given entry appears in this collection
*
* @param anEntry The entry that is to be counted
* @return The number of times anEntry is in the collection
*/
public int getFrequencyOf(T anEntry) {
Node<T> currentNode = new Node<T> (anEntry);
int listCount = 0;
for(int i = 0; i <= numberOfEntries; i++) {
if(anEntry.equals(currentNode.getData()))
{
listCount++;
}
currentNode = currentNode.getNextNode();
}
return listCount;
} // End getFrequencyOf
//*****************************************************************
public boolean contains(T anEntry) {
result = false;
Node<T> currentNode = head;
while(!result && (currentNode != null)) {
if(anEntry.equals(currentNode.getData()))
{
result = true;
} else {
currentNode.currentNode.getNextNode();
}
} // End while loop
return result;
} // End contains method
//*****************************************************************
/**
* Retrives all entries that are in this collection
*
* @return A newly allocated array of all the entries in this collection
* Note: If the collection is empty, the returned array is empty
*/
public Object[] toArray() {
Object[] array = new Object[numberOfEntries];
int index = 0;
Node<T> currentNode = head;
while((index < numberOfEntries) && (currentNode != null)) {
array[index] = currentNode.getData();
currentNode = currentNode.getNextNode();
index++;
}
return array;
} // End toArray method
} // End class