Advertisement
matthewentwistle

checkpoint5

Dec 8th, 2019
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 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 LinkedList2{
  31.  
  32. Node head;
  33.  
  34. public LinkedList2(){
  35. head = null;
  36. }
  37.  
  38. public void printList(){
  39. Node currentNode = head;
  40. while(currentNode.getNext() != null){
  41. System.out.println(currentNode.getPayload());
  42. currentNode = currentNode.getNext();
  43. }
  44. System.out.println(currentNode.getPayload());
  45. }
  46.  
  47. public void addHead(String value){
  48. head = new Node (value, head);
  49. }
  50.  
  51. public void removeHead(){
  52. if (head != null){
  53. head = head.getNext();
  54. }
  55. }
  56.  
  57. public static void main (String [] args){
  58. LinkedList2 list = new LinkedList2();
  59.  
  60. list.addHead("test1");
  61. list.addHead("test2");
  62. list.addHead("test3");
  63. list.removeHead();
  64. list.printList();
  65.  
  66.  
  67.  
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement