So, top.getElement can be cast to an int, but I can't place that with the line following, where element of type T is tested for equality against something that can be cast to an int.
Is T a bound generic type? If so, what is the bound?
And what is top?

Yes, your post was a couple of seconds before mine.
Can you show us the LinearNode<T> class?
Until then, I can only assume that top.getElement() should deliver the T value in it, not something that can be cast to an int,

the Linear Node class is given by my teacher and here it can't be some mistakes

Piet Souris

Master Rancher

Posts: 3010

105

posted 1 year ago

Hmmm. I have some questions about this,
But first yet another question: can you also show us the full code of the LinkedStack<T> class? I like to see how variables of type T are added and, formost, removed (how does the stack retrieve the previous top?)

Don't double‑space your code throughout. Use empty lines to separate logical parts, not successive lines.
Does the index of an element on a stack have any meaning? You look at the top element (or top 2‑3) and would not usually seek elements a long way down the stack.

Constantin Cornea wrote:Finally, the only change that I did is just node.getElement() as you can see below :

Congratulations. I'm glad this worked for you. If you really do have data with null in it you'll need to incorporate Paul's suggestion.

A minor note: Having a trailing semi colon at the end of a for() loop is often a bug. In this case it is not and to emphasize this I put it on its own line below the for() to show that it's an empty statement. You could do the same thing (perhaps better) with an empty set of braces.