package ru.vsu.cs.solution; import java.util.ArrayList; import java.util.List; import…
- User
package ru.vsu.cs.solution;
import java.util.ArrayList; import java.util.List;
import static java.lang.Long.compare;
public class MyList {
private MyListItem head = null; private MyListItem tail = null; private int count = 0;
public static MyList<java.lang.Integer> convertLinkedListToArray(int[] arr) throws Exception { MyList<java.lang.Integer> list = new MyList<>(); for (int i = 0; i < arr.length; i++) { list.add(arr[i]); } return list; }
private static int myComparator(int a, int b) { return compare(a, b); }
public void del() { if (head == null || head.next == head) { return; } while ((int) head.value % 2 == 0 && (int) head.next.value % 2 == 0) { removeFirst(); if (head == null) { return; } } MyListItem curr = head; MyListItem next = head.next; int del = 0; while (curr.next != head) { if ((int) curr.next.value % 2 == 0) { if (del == 0) { next = curr; } del += 1; curr = curr.next; } else if ((int) curr.next.value % 2 == 1) { if (del > 1) { next.next = curr; count -= del; } curr = curr.next; } if (del > 1) { next.next = curr; count -= del; } } tail = curr; }
public int[] convert() { return convertLinkedListToArray(); }
private int[] convertLinkedListToArray() { List<java.lang.Integer> list = new ArrayList<>(); if (head == null) return new int[]{};
MyListItem<Integer> current = head; do { list.add((java.lang.Integer) current.value); current = current.next; } while (current != head); int[] array = new int[list.size()]; for (int i = 0; i < list.size(); i++) { array[i] = list.get(i); } return array;}
private void checkEmpty() throws Exception { if (isEmpty()) { throw new Exception("List is empty"); } }
public Integer getFirst() throws Exception { checkEmpty(); return head.value; }
private void setFirst(Integer value) { head.value = value; }
public Integer getLast() throws Exception { checkEmpty(); return tail.value; }
private void setLast(Integer value) { tail.value = value; }
private void addLast(Integer value) { MyListItem newNode = new MyListItem<>(value, head); if (tail == null) { head = tail = newNode; tail.next = head; } else { tail.next = newNode; tail = newNode; } count++; }
public void clear() { head = tail = null; count = 0; }
public int size() { return count; }
public boolean isEmpty() { return size() == 0; }
public void add(int index, Integer value) throws Exception { if (index < 0 || index > count) { throw new Exception("Incorrect index"); } if (index == 0) { addLast(value); } else { MyListItem prev = getItem(index - 1); MyListItem newNode = new MyListItem<>(value, prev.next); prev.next = newNode; if (newNode.next == head) { tail = newNode; } count++; } }
public void set(int index, Integer value) throws Exception { if (index < 0 || index >= count) { throw new Exception("Incorrect index"); } MyListItem node = getItem(index); node.value = value; }
- Assistant
- User
что еще классное можно добавить в мой список
- Assistant