Java LinkedList程序

时间:2012-10-08 05:17:06

标签: java linked-list

所以我正在尝试为我创建的程序编译我的LinkedList,并且它会返回错误,我觉得它们确实是错误(如果这样做的话)。我想知道是否有人可以看看这个,并提出他们为什么我在编译时会遇到错误的想法,以及他们对我应该做些什么来摆脱它们的想法:

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 LinkedListDS() {
        head = tail = null;
        currentSize = 0;
        }

    public void addFirst(E obj) {
        if(head == null)
            head = tail = newNode;
        else {
            newNode.next = head;
            head = newNode;
            }
        currentSize++;
        }   

// Adds the Object obj to the end of the list 
    public void addLast(E o) {
        if(head == null)
            head = newNode;
            tail = newNode;
        currentSize++;
        return;
        {
        tail.next = newNode;
        tail = newNode;
        currentSize++;
        }
    }   

// Removes the first Object in the list and returns it.
// Returns null if the list is empty.
    public E removeFirst() {
        if(head == null) 
            return null;
        E tmp = head.data;
        head = head.next;
        {
        if (head == null)
            tail = null;
            currentSize++;
        return tmp;
        }
    }   

//  Removes the last Object in the list and returns it.
//  Returns null if the list is empty.
    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;
        }

// Returns the first Object in the list, but does not remove it.
//  Returns null if the list is empty.
    public E peekFirst() {
        if(head == null)
            return null;
        return head.data;
        }

//  Returns the last Object in the list, but does not remove it.
//  Returns null if the list is empty.
    public E peekLast() {
        if(tail == null)
            return null;
        return tail.data;
        }

//  Removes the specific Object obj from the list, if it exists.
//  Returns true if the Object obj was found and removed, otherwise false
    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,
    }
}    

//  The list is returned to an empty state.
    public void makeEmpty() {
        head = tail = null;
        currentSize = 0;


//  Returns true if the list contains the Object obj, otherwise false
    public boolean contains(E obj) {
        Node<E> current = head;
        while(current != null)
        {
            if(current.data.equals(obj))
            {
                return true;
                }
            current - current.next;
            }
        return false;
        }


//  Returns true if the list is empty, otherwise false
    public boolean isEmpty() {
        return = null;
        }

//  Returns true if the list is full, otherwise false
    public boolean isFull() {
        return false;
        }

//  Returns the number of Objects currently in the list.
    public int size() {
        return currentSize;
        }

    public iterator<E> iterator() {
        return new iterator;
            }

    class IteratorHelper implements Iterator<E> {
        Node<E> iteratorPointer

        public IteratorHelper() {
            iteratorPointer = head;
            }
        public boolean hasNext() {
            return iteratorPointer != null;
            }
        public E next() {
            if(!hasNext())
                throw new NoSuchElementException();
            E tmp = iteratorPointer.data;
            iteratorPointer = iteratorPointer.next;
            return tmp;
            }
        public void remove() {
            throw new UnsupportedOperationException();
            }
        }

}

此外,有没有一种方法可以使这段代码更容易阅读,理解,但不是很长但仍然有相同的任务要完成?请随时分享。

1 个答案:

答案 0 :(得分:4)

您已定义了一个接口并将其用作类..

public interface LinkedListDS<E> implements ListADT<E> {

上述声明中的问题: -

  • 接口没有实现其他接口,它们扩展,就像一个类扩展另一个类..
  • 界面未定义methodsconstructors

所以,你想要实际使用一个类..

将上述声明更改为: -

public class LinkedListDS<E> implements ListADT<E> {

此外,您还没有关闭iterator方法..

public iterator<E> iterator() {
        return new iterator;

添加一个右大括号.. 实际上,你有这么多......重新缩进你的代码以检查匹配的大括号..这是你不在的时候遇到的一个主要问题缩进代码..