简体   繁体   中英

Creating a generic stack in generic stack class

I'm trying to teach myself some java and im stuck on a problem that seems kind of easy but i still don't seem to find a solution.

What I have so far:

Interface:

public interface ADTStack<T> {


public boolean isEmpty();


public void push(T element);


public T top() throws IllegalStateException;


public void pop() throws IllegalStateException;
}

Class Stack:

public class Stack<T> implements ADTStack<T> {

private java.util.LinkedList<T> data;  



public Stack() {
    data = new java.util.LinkedList<T>();
}

@Override
public boolean isEmpty() {
    return data.isEmpty();
}

@Override
public void push(T element) {
    data.add(0, element);
}

@Override
public T top() throws IllegalStateException {
    if (isEmpty()) {
        throw new IllegalStateException("Stack is emtpy.");
    }
    return data.getFirst();
}

@Override
public void pop() throws IllegalStateException {
    if (isEmpty()) {
        throw new IllegalStateException("Stack is empty.");
    }
    data.remove(0);
}

Alright , so here is what I'm trying to do. I'm trying to write a methode equals to compare two Stacks. My idea was to use a third Stack to be able to bring both stacks into their original state after comparing them.

Here's what I have:

    Stack supportStack = new Stack();

public boolean equals(ADTStack<T> s){
    if (data.isEmpty() != s.isEmpty()){         
        return false;
    }
    if (data.isEmpty() && s.isEmpty()){     
        return true;
    }

    T element_a  =  this.top();             
    T element_b  = s.top();


    if( (element_a ==null && (element_b !=null) || !element_a.equals(element_b) || element_a != null && element_b == null)){
        return false;
    }

    data.pop();
    s.pop();                        
    supportStack.push(element_a);       
    boolean result = data.equals(s);    

    while (!supportStack.isEmpty()){        
        data.push(supportStack.top());   
        s.push(supportStack.top());
        supportStack.pop();
    }
    return result;                      
}

I get a lot of errors when I compile the code and it seems that something is wrong with :

Stack supportStack = new Stack();

I don't really know what's wrong and how to solve the error. I made a runner-class and I tried the constructor and it worked so I'm confused at what's wrong.

public class Runner {

   public static void main(String[] args){
      Stack test = new Stack();
      test.push(12);
      System.out.println(test.top());
   }
}

I gladly take any advice or constructive criticism since I'm teaching myself and if anything seems unclear feel free to ask.

Stack supportStack = new Stack();

Stack is called a raw type : it's like not using generics. You need to use:

Stack<T> supportStack = new Stack<T>();

But, as a hint: you don't need to do this. You can just do:

return this.data.equals( s.data );

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