Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab3;
- import java.util.Arrays;
- public class Main2 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] array = { 2, 2, 5, 4, 5, 20, 10, 5, 5, 3 };
- System.out.println(Arrays.toString(array));
- System.out.println(Arrays.toString(delDuplicates(array)));
- int input[] = { 1, 9, 0, 0, 0, 0, 9,3, 2, 17, 13 };
- int size = input.length;
- SLNode head = null;
- for (int i = 0; i < size; i++) {
- SLNode newNode = createSLNode(input[i]);
- head = addNode(head, newNode);
- }
- SLNode fic = createSLNode(-200);
- SLNode tail = head;
- while (tail.next != null) {
- tail = tail.next;
- }
- tail.next = fic;
- fic.next = head;
- printList(head);
- printList(deleteBetween(head, 9));
- }
- private static int[] delDuplicates(int[] arr)
- {
- if(arr == null) return new int[0];
- int size = arr.length;
- int [] buffArr = new int[size];
- int newLen = 0;
- for(int i = 0; i < size; i++)
- {
- if(getIElement(buffArr, arr[i]) == -1)
- {
- buffArr[newLen] = arr[i];
- newLen++;
- }
- }
- int [] newArray = new int[newLen];
- copyArray(buffArr, newArray);
- return newArray;
- }
- private static SLNode deleteBetween(SLNode head, int a)
- {
- SLNode x = head;
- SLNode fic = getFic(head);
- while(x.data != a && x != fic)
- {
- x=x.next;
- }
- if(x == fic) return head;
- SLNode firstEnter = x;
- SLNode secondEnter = firstEnter.next;
- while(secondEnter.data != a && secondEnter != fic)
- {
- secondEnter = secondEnter.next;
- }
- if(secondEnter == fic) return head;
- firstEnter.next = secondEnter;
- return head;
- }
- private static SLNode getFic(SLNode head)
- {
- if (head == null || head.next == null) return null;
- SLNode fic = head.next;
- int safeCounter = 0;
- while(fic.next != head && safeCounter < 1000000 )
- {
- fic = fic.next;
- safeCounter++;
- }
- if(safeCounter == 1000000) return null;
- return fic;
- }
- private static int getIElement(int []arr, int a)
- {
- for(int i = 0; i < arr.length; i++)
- {
- if(arr[i] == a)
- return i;
- }
- return -1;
- }
- private static void copyArray(int from[], int []to)
- {
- if(from == null || to == null) return;
- int size = 0;
- if(from.length > to.length) size = to.length;
- else size = from.length;
- for(int i = 0; i < size; i++)
- {
- to[i] = from[i];
- }
- }
- private static void printList(SLNode list) {
- if(list == null) System.out.println("empty list");
- SLNode fic = getFic(list);
- while (list != fic) {
- System.out.print(list.data + " ");
- list = list.next;
- }
- System.out.println();
- }
- private static SLNode createSLNode(int data) {
- SLNode newNode = new SLNode();
- newNode.data = data;
- newNode.next = null;
- return newNode;
- }
- private static SLNode addNode(SLNode head, SLNode node) {
- SLNode x = head;
- if (x == null)
- return node;
- while (x.next != null)
- x = x.next;
- x.next = node;
- return head;
- }
- private static SLNode removeAfter(SLNode x) {
- if (x == null)
- return null;
- if (x.next == null) {
- SLNode y = x.next;
- x.next = null;
- return y;
- }
- SLNode y = x.next;
- x.next = y.next;
- return y;
- }
- private static SLNode removeHead(SLNode head) {
- if (head == null || head.next == null)
- return null;
- SLNode y = head.next;
- head.next = null;
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement