Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public class stackofints<Item>
  2. {
  3. private static node topofstack=null;
  4.  
  5. private static class node{
  6. Item item;
  7. node next;
  8. }
  9.  
  10. public static void push(Item item){
  11. node oldtopofstack=topofstack;
  12. topofstack=new node();
  13. topofstack.item=item;
  14. topofstack.next=oldtopofstack;
  15. }
  16. public static int pop(){
  17. Item item=topofstack.item;
  18. topofstack=topofstack.next;
  19. return item;
  20. }
  21. public boolean isEmpty(){return topofstack==null;}
  22. public static Item size(){
  23. Item i=0;
  24. node iterate=topofstack;
  25. while(iterate!=null)
  26. {
  27. iterate=iterate.next;
  28. i++;
  29. }
  30. return i;
  31. }
  32.  
  33. public static void main(String[] args)
  34. {
  35. push(1);
  36. push(2);
  37. push(3);
  38. System.out.println(size());
  39.  
  40. }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement