package link; public class MyStackLink { public Node head=new Node(); public int top=0; public class Node{ public Object data; public Node next; } public void push(Object obj) { Node newNode=new Node(); newNode.data=obj; while (head.next!=null){ newNode.next=head.next; } head.next=newNode; top++; } public void pop(){ head.next=head.next.next; top--; } public String toString(){ StringBuilder sb=new StringBuilder("["); Node node=head.next; while (node.next!=null){ sb.append(node.data+","); node=node.next; } sb.deleteCharAt(sb.length()-1); sb.append("]"); //sb.replace(sb.length()-1,sb.length(),"]"); return sb.toString(); } }