2

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.

ViktorG
  • 505
  • 1
  • 7
  • 30

1 Answers1

3
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 );
Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • Thank you for your fast anwer :) But it seems like I get a `java.lang.StackOverflowError` now for some reason. Do you know why? – ViktorG Apr 24 '15 at 09:14
  • Post the stack trace, I guess. StackOverflowError will probably be repeating, so just maybe a sample of like 5 lines which repeat. – Radiodef Apr 24 '15 at 09:30
  • I get the error after i compile the class. `Exception in thread "main" java.lang.StackOverflowError at I1_ViSaEl.Stack.(Stack.java:60) at I1_ViSaEl.Stack.(Stack.java:60)` I1_ViSaEl is a package and `at I1_ViSaEl.Stack.(Stack.java:60)` is repeated several times – ViktorG Apr 24 '15 at 09:33
  • 1
    Oh, I see. It is because `supportStack` is an instance field. So when you create a new `Stack`, it creates a new `supportStack`, which creates a new `supportStack`, and so on. Move `supportStack` to a local variable inside `equals`. `boolean equals(...){ Stack supportStack = new Stack(); ...}` – Radiodef Apr 24 '15 at 09:46