簡體   English   中英

如何在Java中的Stack / Queue類中實現我的通用LinkedList

[英]How to implement my generic LinkedList in my Stack/Queue class in Java

我有這個項目,我必須在堆棧和隊列類中實現我的LinkedList數據結構。 包括我的LinkedList在內的所有類都是通用類型<E> 問題是我遇到了一堆溢出和nullpointerexceptions錯誤。 這是我的物流:

我的Stack和Queue類應該實現我之前編寫的LinkedList類,該類可以完美運行(我已經對其進行了全面測試)。 我的LinkedList同時實現了LinkedListImplementation,它僅包含我應該使用的方法的“藍圖”。 另外,我想說所有這些文件-> 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)的庫。 我應該自己編寫邏輯和實現來模擬它們的行為。

現在,我想實現一個由節點組成的堆棧,並實現我的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 1。

每當我運行代碼時,都會出現以下錯誤:

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行)是指Stack()類默認構造函數內的testStack調用。

有人可以幫助我找到實現目標的正確方法嗎? 再次是要建立一個Stack結構並與我的LinkedList數據結構一起運行。

非常感謝您的幫助和提前的時間!

干杯!


EDIT1:

push方法是錯誤的(這是pop方法)。 彈出方法也被添加。 這些是正確的(上面也已更正)

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

您已經定義了您的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM