I have a generic LinkedStack with a Node class and a number of methods
LinkedStack<T>{
class Node<T>{}//
void push(T item){}//
void push(FancyStack<T> s){}//
T pop(){}
boolean isEmpty(LinkedStack<T> stk){}
T peek(){}//
LinkedStack<T> rev(){}//reverses elements in this stack
}
When I create a new LinkedStack, push items onto it, and print the stack, I get memory addresses and not items in the stack.
What am I doing wrong?
Here's my code
import java.util.NoSuchElementException; //import java.util.Stack; public class LinkedStack<T>{ public class Node<T>{ private T data; private Node<T> link; public Node(T data,Node<T>link){ this.data=data; this.link=link; }//Node constructor public void SetData(T info){data =info;} public T getData(){return data;} public void setLink(Node<T> newLink){link=newLink;} public Node<T> getLink(){return link;} }//end of node private Node<T> high=null;// public void push(T thing){//this push high=new Node<T>(thing,high); } public void push(LinkedStack<T> stk){ final int MAX= stk.size(); for(int i=0;i<MAX;i++){ this.push(stk.pop());}}// public T peek(){ if(high==null){throw new NoSuchElementException();} else return high.getData(); } public T pop(){ T thing=peek(); high=high.link; return thing; } public boolean isEmpty(LinkedStack<T> stack){ return(high==null); } int hesabu=0; public int size(){ hesabu++; return hesabu; } public LinkedStack<T> rev(){ LinkedStack<T> revStk= new LinkedStack<T>(); final int LEN= this.size(); for(int j=0;j<LEN;j++){ revStk.push(this.pop());} return revStk;//end of method } }//end of LinkedStack
So for example when I create a new LinkedStack object
as in;
and push things onto it as inLinkedStack<String> stk=new LinkedStack<String>();
When I print out the stack likestk.push("Me"); stk.push("Me too"); stk.push("Me me me"); stk.push("Me myself and I");
I get an erorr of the sortSystem.out.println(stk)
LinkedStack@56r3t333
I'm confused so any help is appreciated