Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package lab3;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Main2 {
  6.  
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9. int[] array = { 2, 2, 5, 4, 5, 20, 10, 5, 5, 3 };
  10. System.out.println(Arrays.toString(array));
  11. System.out.println(Arrays.toString(delDuplicates(array)));
  12.  
  13. int input[] = { 1, 9, 0, 0, 0, 0, 9,3, 2, 17, 13 };
  14. int size = input.length;
  15. SLNode head = null;
  16. for (int i = 0; i < size; i++) {
  17. SLNode newNode = createSLNode(input[i]);
  18. head = addNode(head, newNode);
  19. }
  20.  
  21. SLNode fic = createSLNode(-200);
  22. SLNode tail = head;
  23. while (tail.next != null) {
  24. tail = tail.next;
  25. }
  26. tail.next = fic;
  27. fic.next = head;
  28. printList(head);
  29. printList(deleteBetween(head, 9));
  30. }
  31.  
  32. private static int[] delDuplicates(int[] arr)
  33. {
  34. if(arr == null) return new int[0];
  35. int size = arr.length;
  36. int [] buffArr = new int[size];
  37. int newLen = 0;
  38. for(int i = 0; i < size; i++)
  39. {
  40. if(getIElement(buffArr, arr[i]) == -1)
  41. {
  42. buffArr[newLen] = arr[i];
  43. newLen++;
  44. }
  45. }
  46.  
  47. int [] newArray = new int[newLen];
  48. copyArray(buffArr, newArray);
  49.  
  50. return newArray;
  51. }
  52.  
  53. private static SLNode deleteBetween(SLNode head, int a)
  54. {
  55. SLNode x = head;
  56. SLNode fic = getFic(head);
  57. while(x.data != a && x != fic)
  58. {
  59. x=x.next;
  60. }
  61. if(x == fic) return head;
  62. SLNode firstEnter = x;
  63. SLNode secondEnter = firstEnter.next;
  64. while(secondEnter.data != a && secondEnter != fic)
  65. {
  66. secondEnter = secondEnter.next;
  67. }
  68. if(secondEnter == fic) return head;
  69. firstEnter.next = secondEnter;
  70. return head;
  71.  
  72. }
  73.  
  74. private static SLNode getFic(SLNode head)
  75. {
  76. if (head == null || head.next == null) return null;
  77.  
  78. SLNode fic = head.next;
  79. int safeCounter = 0;
  80. while(fic.next != head && safeCounter < 1000000 )
  81. {
  82. fic = fic.next;
  83. safeCounter++;
  84. }
  85. if(safeCounter == 1000000) return null;
  86. return fic;
  87. }
  88.  
  89. private static int getIElement(int []arr, int a)
  90. {
  91. for(int i = 0; i < arr.length; i++)
  92. {
  93. if(arr[i] == a)
  94. return i;
  95. }
  96. return -1;
  97. }
  98.  
  99. private static void copyArray(int from[], int []to)
  100. {
  101. if(from == null || to == null) return;
  102. int size = 0;
  103. if(from.length > to.length) size = to.length;
  104. else size = from.length;
  105. for(int i = 0; i < size; i++)
  106. {
  107. to[i] = from[i];
  108. }
  109. }
  110.  
  111. private static void printList(SLNode list) {
  112. if(list == null) System.out.println("empty list");
  113. SLNode fic = getFic(list);
  114. while (list != fic) {
  115. System.out.print(list.data + " ");
  116. list = list.next;
  117. }
  118. System.out.println();
  119. }
  120.  
  121. private static SLNode createSLNode(int data) {
  122. SLNode newNode = new SLNode();
  123. newNode.data = data;
  124. newNode.next = null;
  125. return newNode;
  126. }
  127.  
  128. private static SLNode addNode(SLNode head, SLNode node) {
  129. SLNode x = head;
  130. if (x == null)
  131. return node;
  132. while (x.next != null)
  133. x = x.next;
  134. x.next = node;
  135. return head;
  136. }
  137.  
  138. private static SLNode removeAfter(SLNode x) {
  139. if (x == null)
  140. return null;
  141. if (x.next == null) {
  142. SLNode y = x.next;
  143. x.next = null;
  144. return y;
  145. }
  146. SLNode y = x.next;
  147. x.next = y.next;
  148. return y;
  149. }
  150.  
  151. private static SLNode removeHead(SLNode head) {
  152. if (head == null || head.next == null)
  153. return null;
  154. SLNode y = head.next;
  155. head.next = null;
  156. return y;
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement