Advertisement
matthewentwistle

Checkpoint3

Dec 8th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. class Node{
  2. private String payload;
  3. private Node next;
  4.  
  5. public Node (String payload){
  6. this.payload = payload;
  7. this.next = null;
  8. }
  9.  
  10. public Node (String payload, Node next){
  11. this.payload = payload;
  12. this.next = next;
  13. }
  14.  
  15. public String getPayload (){
  16. return this.payload;
  17. }
  18. public Node getNext(){
  19. return next;
  20. }
  21.  
  22. public void setPayload (String payload){
  23. this.payload = payload;
  24. }
  25. public void setNext(Node next){
  26. this.next = next;
  27. }
  28. }
  29.  
  30. class LinkedList{
  31.  
  32. Node head;
  33.  
  34. public LinkedList(){
  35. head = null;
  36. }
  37.  
  38. public static void main (String [] args){
  39. Node node1 = new Node("Node 1");
  40. Node node2 = new Node("Node 2", node1);
  41. Node node3 = new Node("Node 3", node2);
  42.  
  43. Node currentNode = node3;
  44. while(currentNode.getNext() != null){
  45. System.out.println(currentNode.getPayload());
  46. currentNode = currentNode.getNext();
  47. }
  48. System.out.println(currentNode.getPayload());
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement