Help with Interfaces, Generics and pushing onto Stacks
The setup I have goes something like this:
I have an interface called StkInterface with methods that are coded in
my linked list based stack called ListStack. (im not sure i've set my
stack up correctly but it looks like it should work). I want to be
able to add any kind of data type to my stack so i'm trying to use
generics.
My interface looks like this:
public interface StkInterface<E> {
public void push(E object);
//other methods pop, isEmpty, etc.
}
the ListStack looks like this:
public class ListStack<E> implements StkInterface<E> {
private Node<E> head;
public void push(E object){
Node<E> newNode = new Node<E>(object)
newNode.next=head;
head=newNode;
}
//other methods
class Node<E>{
private E data;
private Node<E> next;
public Node(E obj)
{
this.data=obj;
this.next=null;
} //end class Node
} //end class ListStack
The problem I have arises when I try to add elements of a specified
data type to my stack while using another class. (The data type im
trying to push is defined in a GrdCell class. its constructor just
takes two int coordinate parameters) I try to write something like
this:
public class Solver {
private StkInterface<GrdCell> stack; //make a stack that takes
<GrdCell> data type items?
GrdCell first = new GrdCell(0,0); //make a generic
GrdCell
stack.push(first); //<------
problems arise here. Just trying to put that onto the stack.
im still very new with Generics, Interfaces and Linked Lists so I
don't know what im doing wrong. I'm convinced it's just a dumb mistake
im making somewhere, but I dont know if its something with generics,
something with my ListStack code, or problems with initializing the
stack.
Any insight would be greatly appreciated...