Reputation: 53
Hi for my project I'm trying to remove all occurences of the item and return the number of items removed. All subsequent items are moved to the left and if the item is not found, this method will return 0.
Here is my code :
public class LinkedList<T> implements LinkedListInterface<T> {
private Node head;
private Node tail;
private int count;
public LinkedList() {
head = null;
tail = null;
count = 0;
}
class Node {
T data;
Node next;
Node(T data) {
this.data = data;
next = null;
}
}
public Node getHead() {
return head;
}
public int contains(T item) {
Node nd = this.head;
for (int pos = 1; pos <= count; pos++) {
if (nd.data == item) {
return pos;
}
nd = nd.next;
}
return 0;
}
public T remove(int pos) throws ListException {
if (pos < 1 || pos > count) {
throw new ListException("Invalid position to remove from");
}
Node removedItem = null;
if (count == 1) {
removedItem = head;
head = null;
tail = null;
}
else if (pos == 1) {
removedItem = head;
head = head.next;
}
else if (pos == count) {
removedItem = tail;
Node prev = jump(pos - 2);
prev.next = null;
tail = prev;
}
else {
Node prev = jump(pos - 2);
removedItem = prev.next;
prev.next = prev.next.next;
}
count--;
return removedItem.data;
}
public int remove(T item) {
Node numRemoved = 0;
let pos = -1; // cannot find symbol
while ((pos = contains(item)) > 0) {
remove(pos);
numRemoved++; // bad operand type LinkedList<T>.Node for unary operator '++'
}
return numRemoved; // LinkedList<T>.Node cannot be converted to int
}
public void replace(T item, int pos) throws ListException {
if (pos < 1 || pos > count + 1) {
throw new ListException("Invalid position to insert at");
}
}
}
Under ...
public int remove(T item) {
Node numRemoved = 0;
let pos = -1; // cannot find symbol
while ((pos = contains(item)) > 0) {
remove(pos);
numRemoved++; // bad operand type LinkedList<T>.Node for unary operator '++'
}
return numRemoved; // LinkedList<T>.Node cannot be converted to int
}
I put a comment line that contains the error and some things I tried to do to fix was instead of
let pos = -1
I put
int pos = -1;
but it made my while loop have an error as well. I think it may have to do with the declaration of the data types for varibles numRemoved and pos. There is also a bad operand type for
numRemoved++
Any ideas on how to fix? Thank you so much.
Hello, update, this is what it went down to:
public int remove(T item) {
int numRemoved = 0;
int pos = -1;
try {
while ((pos = contains(item)) > 0) {
remove(pos);
numRemoved++;
}
} catch (ListException e){
System.out.print(e);
}
return numRemoved;
}
However, when I tested it out, any matching name(s) in my program couldn't be removed, any reason why?
Upvotes: -2
Views: 58
Reputation: 552
There is no such keyword as 'let' in Java. 'int' is the appropriate declaration, as you discovered.
There's nothing wrong with the while-statement itself, it's one of the statements that happens to be in the loop body. And that is very clear - you cannot apply the '++' operator to a Node. That operator is for incrementing integer values. What do you expect it to do on a Node? Or, equivalently - why have you declared 'numRemoved' to be a Node, when it's obviously supposed to count something? 'int' is appropriate here.
Also, you said that fixing the first error "made my while loop have an error as well". It is fairly common that some errors prevent the compiler from subsequent analysis (the code is already known to be wrong) so that other errors are not seen until you fix the known problems.
Upvotes: 1