Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package blatt7;
  2.  
  3. public class HeadList {
  4.  
  5. Entry head;
  6.  
  7. /**
  8. * constructor empty HeadList
  9. */
  10. public HeadList() {
  11. head = null;
  12. }
  13.  
  14. /**
  15. * Appends a new element with value info to the end of this list
  16. *
  17. * @param info value of the new element
  18. */
  19. public void add(int info) {
  20. if (head != null) {
  21. add(head, info);
  22. } else {
  23. head = new Entry(null, null, info);
  24. head.first = head;
  25. }
  26. }
  27.  
  28. private void add(Entry node, int info) {
  29. if (node.next == null) {
  30. node.next = new Entry(node.first, null, info);
  31. } else {
  32. add(node.next, info);
  33. }
  34. }
  35.  
  36. /**
  37. * Removes and returns the element at position index from this list.
  38. *
  39. * @param index position of the element that is removed
  40. * @return value of the removed element
  41. */
  42. public int remove(int index) {
  43. int counter=0;
  44. Entry current = head;
  45. Entry current3 = head;
  46. while (current3.next != null) {
  47. current3=current3.next;
  48. counter++;
  49. }
  50. if (index>counter) {
  51. return Integer.MIN_VALUE;
  52. }
  53. if (index < 0 ) {
  54. return Integer.MIN_VALUE;
  55. }
  56. Entry current1 = head;
  57.  
  58. if (index == 0) {
  59. head = head.next;
  60. current1.next = null;
  61. head.first = head;
  62. setHead(head);
  63. //czy zmieniam wszystkie first czy tylko dla nastepnego?
  64. return current.elem;
  65. }
  66. if (index == 1){
  67. for (int i = 0; i < index; i++) {
  68. current = current.next;
  69. }
  70. current1.next = current1.next.next;
  71. current.next = null;
  72. //setHead(head.next);
  73. return current.elem;}
  74.  
  75. for (int i = 0; i < index; i++) {
  76. current1 = current1.next;
  77. }
  78. for (int i = 0; i < index-1; i++) {
  79. current = current.next;
  80. }
  81. current.next=current.next.next;
  82. current1.next=null;
  83. return current1.elem;
  84.  
  85. }
  86.  
  87. /**
  88. * sets the head of each list element to newHead
  89. *
  90. * @param newHead reference to the new head
  91. */
  92. private void setHead(Entry newHead) {
  93. int counter = 0;
  94. Entry current = head;
  95. Entry current1 = head;
  96. current.first = newHead;
  97. System.out.println(current.first);
  98. while (current1.next != null) {
  99. current1=current1.next;
  100. counter++;
  101. }
  102. for (int i = 0; i < counter; i++) {
  103. current = current.next;
  104. current.first = newHead;
  105. System.out.println(current.first);
  106. }
  107. }
  108. private Entry getTail() {
  109. Entry entry = head;
  110.  
  111. while (entry.next != null) {
  112. entry = entry.next;
  113. }
  114.  
  115. return entry;
  116. }
  117. /**
  118. * reverse the list example: [1,2,3,4,5] --> [5,4,3,2,1], [] --> [], [1] -->
  119. * [1]
  120. */
  121. public void reverse() {
  122. if (head!=null){
  123. Entry prev = null;
  124. Entry current = head;
  125. Entry next = current.next;
  126.  
  127. while (current != null) {
  128. current.next=prev;
  129. prev=current;
  130. current=next;
  131. next = next == null ? null : next.next;
  132. }
  133.  
  134. head=prev;
  135. setHead(getTail());}
  136. }
  137.  
  138. @Override
  139. public String toString() {
  140. String out = "[";
  141. if (head != null) {
  142. out += head.elem;
  143. Entry tmp = head.next;
  144. while (tmp != null) {
  145. out = out + "," + tmp.elem;
  146. tmp = tmp.next;
  147. }
  148. }
  149. out += "]";
  150. return out;
  151. }
  152.  
  153.  
  154.  
  155. public static void main(String[] args) {
  156. HeadList l = new HeadList();
  157. l.add(3);
  158. l.add(4);
  159. l.add(5);
  160. l.add(6);
  161. l.add(7);
  162. //System.out.println(l.remove(555));
  163. l.reverse();
  164. //l.getTail();
  165. System.out.println("empty list: " + l);
  166.  
  167. // Test implementation
  168.  
  169. }
  170.  
  171. class Entry {
  172.  
  173. Entry first;
  174. Entry next;
  175. int elem;
  176.  
  177. public Entry(Entry first, Entry next, int elem) {
  178. this.first = first;
  179. this.next = next;
  180. this.elem = elem;
  181. }
  182.  
  183. }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement