Advertisement
Guest User

Untitled

a guest
May 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. class main {
  2.  
  3. public static void main(String[] args) {
  4. Stack<Integer> s = new Stack<>();
  5. s.push(1);
  6. s.push(2);
  7. s.push(3);
  8. s.push(4);
  9. System.out.println(s.pop());
  10. System.out.println(s.pop());
  11. System.out.println(s.peek());
  12. System.out.println(s.pop());
  13. System.out.println(s.isEmpty());
  14. System.out.println(s.pop());
  15. System.out.println(s.isEmpty());
  16.  
  17. }
  18.  
  19. }
  20.  
  21.  
  22. class Stack<T> {
  23. class Node<T> {
  24. private T data;
  25. private Node<T> next;
  26.  
  27. public Node(T data) {
  28. this.data = data;
  29. }
  30. }
  31.  
  32. private Node<T> top;
  33.  
  34. public T pop() {
  35. if(top == null) {
  36.  
  37. }
  38.  
  39. T item = top.data;
  40. top = top.next;
  41.  
  42. return item;
  43. }
  44.  
  45. public void push(T item) {
  46. Node<T> t = new Node<T>(item);
  47. t.next = top;
  48. top = t;
  49. }
  50.  
  51. public T peek() {
  52. if (top == null) {
  53.  
  54. }
  55. return top.data;
  56. }
  57.  
  58. public boolean isEmpty() {
  59. return top == null;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement