cant get LinkedList to compile
So here is my interface for my LinkedList:
Code Java:
package data_structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
public interface ListADT<E> extends Iterable<E> {
// Adds the Object obj to the beginning of the list
public void addFirst(E obj);
// Adds the Object obj to the end of the list
public void addLast(E o);
// Removes the first Object in the list and returns it.
// Returns null if the list is empty.
public E removeFirst();
// Removes the last Object in the list and returns it.
// Returns null if the list is empty.
public E removeLast();
// Returns the first Object in the list, but does not remove it.
// Returns null if the list is empty.
public E peekFirst();
// Returns the last Object in the list, but does not remove it.
// Returns null if the list is empty.
public E peekLast();
// Removes the specific Object obj from the list, if it exists.
// Returns true if the Object obj was found and removed, otherwise fa$
public boolean remove(E obj);
// The list is returned to an empty state.
public void makeEmpty();
// Returns true if the list contains the Object obj, otherwise false
public boolean contains(E obj);
// Returns true if the list is empty, otherwise false
public boolean isEmpty();
// Returns true if the list is full, otherwise false
public boolean isFull();
// Returns the number of Objects currently in the list.
public int size();
// Returns an Iterator of the values in the list, presented in
// the same order as the list.
public Iterator<E> iterator();
}
I tried to compile my LinkedList but get the following error: data_structures/LinkedListDS.java:6: data_structures.LinkedListDS is not abstract and does not override abstract method iterator() in data_structures.ListADT
public class LinkedListDS<E> implements ListADT<E> {
Here is my LinkedList:
Code java:
package data_structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedListDS<E> implements ListADT<E> {
class Node<E> {
E data;
Node<E> next;
public Node(E data) {
this.data = data;
next = null;
}
}
private Node<E> head, tail;
private int currentSize;
public void LinkedListDS() {
head=tail=null;
currentSize= 0;
}
public void addFirst(E obj){
Node<E> newNode = new Node<E>(obj);
if(head==null)
head = tail= null;
else {
newNode.next=head;
head = newNode;
}
currentSize=currentSize++;
}
public E removeFirst() {
if (head==null) return null;
E tmp=head.data;
head =head.next;
if (head==null) tail=null;
currentSize--;
return tmp;
}
}
Also i made a Stack code but get the following error when compiling: data_structures/Stack.java:7: '(' or '[' expected
list = new LinkedListDS<E>;
^
data_structures/Stack.java:17: '(' or '[' expected
}
^
Code java:
package data_structures;
public class Stack implements Iterable<E> {
private ListADT<E> list;
public Stack() {
list = new LinkedListDS<E>;
}
public void push(E obj) {
list.addFirst(obj);
}
public E pop() {
return list.removeFirst();
}
public Iterator<E> iterator() {
return new IteratorHelper
}
class IteratorHelper implements Iterator<E> {
Node<E> iteratorPointer;
public IteratorHelper() {
iteratorPointer = head;
}
public boolean hasNext() {
return iteratorPointer != null;
}
public E next() {
if (!hasNext())
throw NoSuchException();
E temp = iteratorPointer.data;
iteratorPointer = iteratorPointer.next;
return tmp;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
Any help would b greatly appreciated. I am new to programming and can't figure out what is wrong with my code.
Re: cant get LinkedList to compile
Quote:
LinkedListDS is not abstract and does not override abstract method iterator()
The compiler can't find a definition for the iterator() method in the LinkedListDS class.
If you implement an interface you must define all of its methods.
Quote:
'(' or '[' expected
Where is the ^ located in the error messages. The posting lost its position. Look at the location in the statement above the ^ for the problem. The compiler is expecting to see a '(' or a '[' there.
Re: cant get LinkedList to compile
Thanks for all your help:] I just have one more question. I am nearly done with my LinkedList here it is:
Code :
package data_structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedListDS<E> implements ListADT<E> {
class Node<E> {
E data;
Node<E> next;
public Node(E data) {
this.data = data;
next = null;
}
}
private Node<E> head, tail;
private int currentSize;
public void LinkedListDS() {
head=tail=null;
currentSize= 0;
}
public void addFirst(E obj){
Node<E> newNode = new Node<E>(obj);
if(head==null)
head = tail= null;
else {
newNode.next=head;
head = newNode;
}
currentSize=currentSize++;
}
public void addLast(E obj){
// Create a new node.
Node < E > newNode = new Node < E > (obj);
if(tail==null)
tail=head=null;
else {
tail.next = newNode; // Step 1
// Link the new node to the tail.
newNode.prev = tail; // Step 2
// The new node is the new tail.
tail = newNode; // Step 3
}
currentSize=currentSize++;
}
public E removeFirst() {
if (head==null) return null;
E tmp=head.data;
head =head.next;
if (head==null) tail=null;
currentSize--;
return tmp;
}
public E removeLast(){
if (tail==null) return null;
E tmp=tail.data;
tail = tail.prev;
if (tail==null) head=null;
currentSize--;
return tmp;
}
public E peekFirst(){
if(head == null) return null;
return head.data;
}
public E peekLast(){
if(tail == null) return null;
return tail.data;
}
public boolean remove(E obj){
if (head == null) return false;
if (head.data.equals(obj)) {
head = head.next;
return true;
currentSize--;
}
Node<E> current = head;
while (current.next != null) {
if (current.next.data.equals(obj)) {
current.next = current.next.next;
return true;
currentSize--;
}
current = current.next;
}
return false;
}
public void makeEmpty(){
head = tail = null;
currenSize = 0;
}
public boolean contains(E obj){
Node<E> current = head;
while (current != null) {
if (current.data.equals(obj)) {
return true;
}
current = current.next;
}
return false;
}
public boolean isEmpty(){
return first == null;
}
public boolean isFull(){
return false;
}
public int size(){
return currentSize;
public Iterator<E> iterator() {
return new IteratorHelper();
}
class IteratorHelper implements Iterator<E>{
Node<E> IteratorPointer;
public IteratorHelper(){
IteratorPointer=head;
}
}
}
}
I am getting the following errors when i compile:
data_structures/LinkedListDS.java:122: illegal start of expression
public Iterator<E> iterator() {
^
data_structures/LinkedListDS.java:122: ';' expected
public Iterator<E> iterator() {
^
2 errors
I have tried to change around the IteratorHelper class to see if that was the problem, but had no luck.
Re: cant get LinkedList to compile
Check that all the {s have a matching } and that no methods are defined inside of other methods.
Re: cant get LinkedList to compile
I am having such a hard time getting my Iterator method to work. I know that I must include a class for the iterator and this is what I have so far:
Code :
package data_structures;
public class LinkedListDS<E> implements ListADT<E> {
class Node<E> {
E data;
Node<E> next;
public Node(E data) {
this.data = data;
next = null;
}
}
private Node<E> head, tail;
private int currentSize;
public void LinkedListDS() {
head=tail=null;
currentSize= 0;
}
public void addFirst(E obj){
Node<E> newNode = new Node<E>(obj);
if(head==null)
head = tail= null;
else {
newNode.next=head;
head = newNode;
}
currentSize=currentSize++;
}
public void addLast(E obj){
Node<E> newNode = new Node<E>(obj);
if(head == null){
head = newNode;
tail = newNode;
currentSize++;
return;
}
tail.next = newNode;
tail = newNode;
currentSize++;
}
public E removeFirst() {
if (head==null) return null;
E tmp=head.data;
if (head==null) return null;
E tmp=head.data;
head =head.next;
if (head==null) tail=null;
currentSize--;
return tmp;
}
public E removeLast(){
Node<E> previous = null;
Node<E> current = head;
if(current == null) return null;
while(current.next != null){
previous = current;
current = current.next;
}
if(previous == null) return removeFirst();
previous.next = null;
tail = previous;
currentSize--;
return current.data;
}
public E peekFirst(){
if(head == null) return null;
return head.data;
}
public E peekLast(){
if(tail == null) return null;
return tail.data;
}
public boolean remove(E obj){
if (head == null) return false;
if (head.data.equals(obj)) {
head = head.next;
return true;
}
Node<E> current = head;
while (current.next != null) {
if (current.next.data.equals(obj)) {
current.next = current.next.next;
return true;
}
current = current.next;
}
currentSize--;
return false;
}
public void makeEmpty(){
head = tail = null;
currentSize = 0;
}
public boolean contains(E obj){
Node<E> current = head;
while (current != null) {
if (current.data.equals(obj)) {
return true;
}
current = current.next;
}
return false;
}
public boolean isEmpty(){
return head == null;
}
public boolean isFull(){
return false;
}
public int size(){
return currentSize;
}
public Iterator<E> iterator() {
return new IteratorHelper();
}
class IteratorHelper implements Iterator<E>{
Node<E> IteratorPointer;
public IteratorHelper(){
IteratorPointer=head;
}
}
}
And i get the following errors when I compile:
data_structures/LinkedListDS.java:125: cannot find symbol
symbol : class Iterator
location: class data_structures.LinkedListDS<E>
public Iterator<E> iterator() {
^
data_structures/LinkedListDS.java:128: cannot find symbol
symbol : class Iterator
location: class data_structures.LinkedListDS<E>
class IteratorHelper implements Iterator<E>{
^
data_structures/LinkedListDS.java:128: interface expected here
class IteratorHelper implements Iterator<E>{
Can you please help me write the class for my iterator or point me in the right direction? I am relatively new to programming
I simply need the iterator to return an Iterator of the values in the list, presented in the same order as the list.
Re: cant get LinkedList to compile
Quote:
cannot find symbol
symbol : class Iterator
You need an import statement so the compiler can find the definition of the Iterator interface.
Start by making a skeleton class that implements the interface and has all the methods.
Then work on them one at a time.
You need to fix the formatting of the posted code. There should not be a column of }s. Each } should be in its own column underneath the start of the statement with the matching {
Too many statements start in the first column. Unformated code is hard to read and understand.