如何在Java中的Stack / Queue类中实现我的通用LinkedList

时间:2015-04-11 00:43:02

标签: java generics linked-list stack implementation

我有这个项目,我必须在堆栈和队列类中实现我的LinkedList数据结构。包括我的LinkedList在内的所有类都是通用类型<E>。问题是我遇到了一堆溢出和nullpointerexceptions错误。这是我的后勤:

我的Stack和Queue类应该实现我之前编写的LinkedList类,它运行完美(我对它进行了彻底的测试)。我的LinkedList同时实现LinkedListImplementation,它只包含我应该使用的方法的“蓝图”。另外,我想说所有这些文件 - &gt; LinkedListImplementation.java,LinkedList.java和Stack.java位于名为package1的包中。

以下是方法(没有代码,因为我110%确定它运行完美):

//LinkedListImplementation.java:
package package1;
import java.util.Iterator;
public class LinkedListImplementation<E> extends Iterable<E>{
    //...
    //All the methods to be used go here. See the methods in LinkedList.java
}//End LinkedListImplementation Implementation Class




//LinkedList.java:
package package1;
import java.util.Iterator;
import java.lang.Comparable;
import java.util.NoSuchElementException;

public class LinkedList<E> implements LinkedListImplementation<E>{
    int size;
    Node<E> head, tail;

    public LinkedList(){
        head = null;
        tail = null;
    }

    class Node<E>{
        E data;
        Node<E> next;

        //Default Node constructor
        Node(E obj){
            this.data = obj;
            next = null;
        }
    }
    //Logic for all the LinkedList methods below.

    //This method contains its code, as an example...
    public void addLast(E obj){
        Node<E> newNode = new Node<E>(obj);
        if( isEmpty() ){
            head = newNode;
            tail = newNode;
            size++;
        }else{
            tail.next = newNode;
            tail = newNode;
            size++;
        }
    }

    public void addFirst(E obj){
        ...
    }

    public void insert(E obj, int location){
        ...
    }

    //... Other methods, such as removeFirst(), removeLast(), remove(int location)
    //... size(), get(int location), contains(E obj), locate(int location)
    //... clear(), isEmpty().
}//End LinkedList Class

正如您所看到的,我可以在java中使用LinkedList类,但我不允许使用java.util中构造的任何库,并且具有默认行为,即LinkedList,Stack和Queue。我应该自己编写逻辑和实现来模拟他们的行为。

现在,我想实现一个由Nodes组成的Stack,实现我的LinkedList。这是我的尝试:

//Stack.java:
package package1;
import java.util.Iterator;
public class Stack<E> extends LinkedList<E> implements Iterable<E>{
    LinkedList<E> list;

    Stack(){
        list = new LinkedList<E>();
        testStack();
    }

    //START declaration of Stack methods
    public void push(E obj){ list.addFirst(); }

    public E pop(){ return list.removeFirst() }

    public int size(){ return list.size(); }

    public boolean isEmpty(){ return list.isEmpty(); }

    public E peek(){ return list.get(size()); }

    public boolean contains(E obj){ return list.contains(obj); }

    public void makeEmpty(){ list.clear(); }

    public Iterator<E> iterator(){ return list.iterator(); } 
    //END declaration of Stack methods

    //My tests to see if the Stack works
    public void testStack{

        Stack<Integer> testStack = new Stack<Integer>();
        //Test push method
        for(int i = 1; i < 100; i++){
            stack.push(new Integer(i));
        }
    }//End testStack

    public static void main(String [] args) {
        try {
            new Stack();
        } catch(Exception e) {
            System.out.println("ERROR: " + e);
            e.printStackTrace();
        }
    }//End Main method

}//End Stack Class

这是我的代码。我甚至没有尝试接近Queue类,因为一旦我启动并运行了Stack,就可以很容易地实现Queue了。

每当我运行我的代码时,都会收到以下错误:

Exception in thread "main" java.lang.StackOverflowError
    at data_structures.LinearList.<init>(LinkedList.java:24)
    at data_structures.Stack.<init>(Stack.java:9)
    at data_structures.Stack.testStack(Stack.java:25)
    at data_structures.Stack.<init>(Stack.java:11)
    at data_structures.Stack.testStack(Stack.java:25)
    at data_structures.Stack.<init>(Stack.java:11)
    at data_structures.Stack.testStack(Stack.java:25)
    ...//A lot more of the same line 11 and 25 errors...

我认为错误的发生是因为我在testStack方法中的for循环以及我的代码中的某种类实例化错误。第一个错误(第24行)引用LinkedList类构造函数。第二个错误(第9行)引用Stack类构造函数。第三个错误(第25行)是指runTests()方法中堆栈的实例化,它是以下行:Stack<Integer> testStack = new Stack<Integer>();。第四个错误(第11行)引用testStack类默认构造函数中的Stack()调用。

有人可以帮助我找到实现目标的正确方法吗?这也是使用我的LinkedList数据结构启动和运行Stack结构。

非常感谢你的帮助和提前的时间!

干杯!


EDIT1:

推送方法错误(这是pop方法)。 pop方法也被添加了。这些是正确的(它们也在上面进行了修正)

public void push(){ list.addFirst(obj); } 
public E pop(){ return list.removeFirst(); }

1 个答案:

答案 0 :(得分:1)

您已定义了Stack构造函数,如下所示:

Stack(){
    list = new LinkedList<E>();
    testStack();
}

您的testStack()方法如下所示,它已丢失()

//My tests to see if the Stack works
public void testStack(){

    Stack<Integer> testStack = new Stack<Integer>();
    //Test push method
    for(int i = 1; i < 100; i++){
        stack.push(new Integer(i));
    }
}//End 

您的Stack()构造函数正在调用testStack()方法,该方法本身正在语句中初始化堆栈:Stack<Integer> testStack = new Stack<Integer>();这会变为递归并且您遇到StackOverflowError