简体   繁体   中英

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

I have this project where I have to implement my LinkedList data structure in a stack and queue classes. All the classes including my LinkedList are of generic type <E> . The problem is that I am getting a bunch of overflow and nullpointerexceptions errors. Here is my logistic:

My Stack and Queue classes are supposed to implement the LinkedList class I previously wrote, which runs flawlessly (I thoroughly tested it). My LinkedList is at the same time implementing the LinkedListImplementation, which only contains the "blueprints" of the methods I am supposed to use. In addition, I would like to say that all these files -> LinkedListImplementation.java, LinkedList.java and Stack.java are inside a package called package1 .

Here are the methods (without code, since I am 110% sure it runs flawlessly):

//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

As you can see, I could have used the LinkedList class in java, but I am not allowed to use any libraries from java.util that are constructed and have default behavior ie LinkedList, Stack and Queue. I am supposed to write the logistic and implementations myself simulating their behavior.

Now, I want to implement a Stack that is comprised of Nodes, implementing my LinkedList. Here is my attempt:

//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

That's my code. I have not even try approaching the Queue class because once I get the Stack one up and running, it will be really easy to implement the Queue one.

Whenever I run my code, I get the following error:

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...

I assume that the error happens because of the for loop I have inside the testStack method and some kind of class instantiating mistake I have in my code. First error (Line 24) refers to the LinkedList class constructor. Second Error (Line 9) refers to the Stack class constructor. Third error (Line 25) refers to the instantiation of the stack inside the runTests() method, which is the following line: Stack<Integer> testStack = new Stack<Integer>(); . Fourth Error (Line 11) refers to the testStack call inside the Stack() class default constructor.

Could someone help me to get to the correct approach of achieving my goal? Which again is to get a Stack structure up and running with my LinkedList data structure.

Thanks a lot for your help and time in advance!

Cheers!


Edit1:

The push method was wrong (it was the pop method). The pop method was added as well. These are the correct ones (they are corrected above as well)

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

You had defined your Stack constructor like below:

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

And your testStack() method is like below, it's missing ()

//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 

Your Stack() constructor is calling testStack() method, which itself is initializing a Stack in the statement: Stack<Integer> testStack = new Stack<Integer>(); That becomes recursive and you run into a StackOverflowError

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM