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 {
code - Assistant
package ru.vsu.cs.solution;
import java.util.ArrayList; import java.util.List;
import static java.lang.Long.compare;
public class MyList {
private MyListItem<Integer> head = null;
private MyListItem<Integer> 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<Integer> curr = head;
MyListItem<Integer> 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<Integer> 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<Integer> prev = getItem(index - 1);
MyListItem<Integer> 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<Integer> node = getItem(index);
node.value = value;
}
public void add(Integer value) throws Exception {
addLast(value);
}
public void sorted() throws Exception {
for (int i = 1; i < count; i++) {
Integer value = get(i);
int j;
for (j = i - 1; j >= 0 && myComparator((java.lang.Integer) get(j), (java.lang.Integer) value) > 0; j--) {
set(j + 1, get(j)); // shift element right
}
set(j + 1, value);
}
}
public Integer get(int index) throws Exception {
return getItem(index).value;
}
private MyListItem<Integer> getItem(int index) throws Exception {
if (index < 0 || index >= size()) {
throw new Exception("Incorrect index");
} else {
MyListItem<Integer> curr = head;
for (int i = 0; i < index; i++) {
curr = curr.next;
}
return curr;
}
}
public void removeFirst() {
if (head == null) return;
if (head == tail) {
head = tail = null;
} else {
head = head.next;
tail.next = head;
}
count--;
}
public void removeLast() throws Exception {
checkEmpty();
if (head == tail) {
removeFirst();
} else {
MyListItem<Integer> curr = head;
while (curr.next != tail) {
curr = curr.next;
}
curr.next = head;
tail = curr;
count--;
}
}
public void remove(int index) throws Exception {
if (index < 0 || index >= size()) {
throw new Exception("Incorrect index");
}
if (index == 0) {
removeFirst();
} else {
MyListItem<Integer> prev = getItem(index - 1);
prev.next = prev.next.next;
if (index == size()) {
tail = prev;
}
count--;
}
}
public void deleteUneven(MyList<java.lang.Integer> list) throws Exception {
MyListItem<Integer> curr = head;
for (int i = 0; i < size(); i++) {
if ((int) curr.value % 2 == 1) {
remove(i);
i--;
} else {
curr = curr.next;
}
}
}
public void deletePovtor() {
if (head == null) return;
MyListItem<Integer> curr = head;
do {
while (curr.next != head && curr.value.equals(curr.next.value)) {
curr.next = curr.next.next;
count--;
}
curr = curr.next;
} while (curr != head);
}
public void deleteElementForKToTail(int k) {
if (head == null || k >= size()) {
clear();
return;
}
MyListItem<Integer> curr = head;
for (int i = 0; i < k - 1; i++) {
curr = curr.next;
}
curr.next = head;
tail = curr;
count = k;
}
private class MyListItem<Integer> {
public Integer value;
public MyListItem<Integer> next;
public MyListItem(Integer value, MyListItem<Integer> next) {
this.value = value;
this.next = next;
}
public MyListItem(Integer value) {
this(value, null);
}
}
}на основе моего кода реализуй функцию быстрой сортировки моего листа (функцию назови werryWellQuick sort)
To implement a quick sort for your custom linked list structure, you'll need to define the werryWellQuickSort function and its helper functions. Quick sort works by selecting a 'pivot' element and partitioning the list into two sublists: one with elements less than the pivot and one with elements greater than the pivot. This approach is recursive and usually involves additional helper functions. Based on your current list implementation, here's how you could implement werryWellQuickSort:
public void werryWellQuickSort() throws Exception {
head = quickSort(head, tail);
// After sorting, update tail properly:
MyListItem<Integer> current = head;
while (current.next != head) {
current = current.next;
}
tail = current;
}
private MyListItem<Integer> quickSort(MyListItem<Integer> start, MyListItem<Integer> end) {
if (start == null || start == end || start == end.next) {
return start;
}
MyListItem<Integer>[] partitioned = partition(start, end);
MyListItem<Integer> newHead = partitioned[0]; // this is the new head
MyListItem<Integer> pivotPrev = partitioned[1]; // this is the node before the pivot
if (newHead != pivotPrev) {
MyListItem<Integer> tmp = newHead;
// Move till one before the pivot
while (tmp.next != pivotPrev) {
tmp = tmp.next;
}
// Set the new end of the first part to null
tmp.next = null;
// Recursively sort the lower part
newHead = quickSort(newHead, tmp);
// Connect the sorted lower part with the pivot
tmp = getTail(newHead);
tmp.next = partitioned[1];
}
// Now recursively sort the part after pivot
partitioned[1].next = quickSort(partitioned[1].next, end);
return newHead;
}
private MyListItem<Integer>[] partition(MyListItem<Integer> start, MyListItem<Integer> end) {
MyListItem<Integer> pivot = end;
MyListItem<Integer> prev = null, curr = start, tail = pivot;
// Initially, pivotPrev points to last node
MyListItem<Integer> pivotPrev = start;
MyListItem<Integer> newEnd = tail;
while (curr != pivot) {
if (myComparator((java.lang.Integer) curr.value, (java.lang.Integer) pivot.value) < 0) {
if (pivotPrev == tail) {
pivotPrev = curr;
}
prev = curr;
curr = curr.next;
} else { // If curr is greater than pivot
if (prev != null) {
prev.next = curr.next;
}
MyListItem<Integer> tmp = curr.next;
curr.next = null;
newEnd.next = curr;
newEnd = curr;
curr = tmp;
}
}
if (pivotPrev != tail) {
start = pivot;
}
return new MyListItem[]{start, pivotPrev}; // Returning the new head and the pivotPrevious
}
private MyListItem<Integer> getTail(MyListItem<Integer> node) {
while (node != null && node.next != null) {
node = node.next;
}
return node;
}
quickSort function applies quicksort to each partition until the base case is reached (a segment of the list with zero or one elements).tail is updated after the sorting since it should be adjusted to point to the last sorted element.Please test the function thoroughly, as list manipulation and quicksort recursion can introduce logical complexities. Adjustments might be needed based on specific requirements or to optimize performance.
Not yet included in a published release.