Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public class Node {
  2.  
  3. public String data;
  4. public Node next;
  5.  
  6. public Node(String data)
  7. {
  8. this.data = data;
  9. }
  10.  
  11. public void displayNode()
  12. {
  13. System.out.println(data);
  14. }
  15. }
  16. //LISTA
  17.  
  18. class Lista {
  19.  
  20. Node first = null;
  21.  
  22. public String addFirst(String data) {
  23. Node newFirst = new Node(data);
  24. newFirst.next = first;
  25. first = newFirst;
  26. return first.data;
  27. }
  28.  
  29. public String removeFirst() {
  30. Node oldFirst = first;
  31. first = first.next;
  32. return oldFirst.data;
  33. }
  34.  
  35. public boolean isEmpty() {
  36. return first == null;
  37. }
  38. public String firstElem()
  39. {
  40. return first.data;
  41. }
  42. public void displayList()
  43. {
  44. Node current = first;
  45. while (current != null)
  46. {
  47. current.displayNode();
  48. current = current.next;
  49. }
  50. }
  51. }
  52.  
  53. //STOS
  54. class Stos {
  55.  
  56. private final Lista lista = new Lista();
  57.  
  58. public String push(String data)
  59. {
  60. return lista.addFirst(data);
  61. }
  62.  
  63. public String pop() {
  64. return lista.removeFirst();
  65. }
  66.  
  67. public boolean isEmpty() {
  68. return lista.isEmpty();
  69. }
  70.  
  71. public String toString() {
  72. return lista.toString();
  73. }
  74. public String peek()
  75. {
  76. return lista.firstElem();
  77. }
  78. public void display()
  79. {
  80. lista.displayList();
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement