Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. public static class MyLinkedList {
  2. protected Element head;
  3. protected Element tail;
  4.  
  5. void insertAt(int n){
  6.  
  7. }
  8. public Element find(int mydata){
  9. Element starter = head;
  10. while(starter != null){
  11. if(starter.data == mydata){
  12. return starter;
  13. }
  14. starter = starter.next;
  15. }
  16. return null;
  17. }
  18. public String toString(){
  19. String result = "[ ";
  20. Element starter = head;
  21. while(starter != tail){
  22. result = result + String.valueOf(starter.data) + ", ";
  23. starter = starter.next;
  24. }
  25. result = result + String.valueOf(starter.data);
  26. result += " ]";
  27. return result;
  28. }
  29. public void append(int mydata) {
  30. Element element = new Element(mydata, null);
  31. if (head == null) head = element;
  32. else tail.next = element;
  33. tail = element;
  34. }
  35.  
  36. public void prepend(int mydata) {
  37. Element element = new Element(mydata, head);
  38. if (head == null) tail = element;
  39. head = element;
  40. }
  41.  
  42. public int countNodes() {
  43. int count = 0;
  44. Element e = head;
  45. while (e != null) {
  46. count++;
  47. e = e.next;
  48. }
  49. return count;
  50. }
  51.  
  52. public int countNodes(int mydata) {
  53. int count = 0;
  54. Element e = head;
  55. while (e != null) {
  56. if (e.data == mydata)
  57. count++;
  58. e = e.next;
  59. }
  60. return count;
  61. }
  62.  
  63. public void extractFirst() {
  64. if (head == null) throw new IllegalArgumentException("item not found");
  65. head = head.next;
  66. if (head == null) tail = null;
  67. }
  68.  
  69. public void extractLast() {
  70. if (tail == null) throw new IllegalArgumentException("item not found");
  71. if (head == tail) head = tail = null;
  72. else {
  73. Element previous = head;
  74. while (previous.next != tail) previous = previous.next;
  75. previous.next = null;
  76. tail = previous;
  77. }
  78. }
  79.  
  80. public void extract(int mydata) {
  81. Element element = head;
  82. Element previous = null;
  83. while (element != null && !(element.getData() == mydata)) {
  84. previous = element;
  85. element = element.next;
  86. }
  87. if (element == null) throw new IllegalArgumentException("item not found");
  88. if (element == head) head = element.next;
  89. else previous.next = element.next;
  90. if (element == tail) tail = previous;
  91. }
  92.  
  93. public final class Element {
  94. int data;
  95. Element next;
  96.  
  97. Element(int mydata, Element element) {
  98. data = mydata;
  99. next = element;
  100. }
  101.  
  102. public int getData() {
  103. return data;
  104. }
  105.  
  106. public Element getNext() {
  107. return next;
  108. }
  109.  
  110. public void insertBefore(int mydata) {
  111. Element element = new Element(mydata, this);
  112. if (this == head) {
  113. head = element;
  114. return;
  115. }
  116. Element previous = head;
  117. while (previous.next != this) {
  118. previous = previous.next;
  119. }
  120. previous.next = element;
  121. }
  122.  
  123. public void insertAfter(int mydata) {
  124. next = new Element(mydata, next);
  125. if (this == tail)
  126. tail = next;
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement