С помощью односвязного списка напишите реализацию LinkedList.
У пользователя должны быть доступны методы добавления, вставки и удаления из списка элементов.
Условия:
- Нельзя использовать шаблоны(готовый LinkedList, методы для работы с ним)
Java
Помогите с java пожалуйста!!
public class LinkedList {
private Node head; // головной/первый элемент списка
private int size; // размер списка
public LinkedList() {
head = null;
size = 0;
}
// вставка элемента в начало списка
public void addFirst(int value) {
head = new Node(value, head);
size++;
}
// вставка элемента в конец списка
public void addLast(int value) {
Node newNode = new Node(value, null);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
size++;
}
// вставка элемента по указанному индексу
public void add(int value, int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
if (index == 0) {
addFirst(value);
} else if (index == size) {
addLast(value);
} else {
Node current = head;
for (int i = 1; i < index; i++) {
current = current.getNext();
}
Node newNode = new Node(value, current.getNext());
current.setNext(newNode);
size++;
}
}
// удаление первого элемента из списка
public void removeFirst() {
if (head == null) {
throw new NoSuchElementException();
}
head = head.getNext();
size–;
}
// удаление последнего элемента из списка
public void removeLast() {
if (head == null) {
throw new NoSuchElementException();
}
if (head.getNext() == null) {
head = null;
} else {
Node current = head;
while (current.getNext().getNext() != null) {
current = current.getNext();
}
current.setNext(null);
}
size–;
}
// удаление элемента по указанному индексу
public void remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
if (index == 0) {
removeFirst();
} else if (index == size - 1) {
removeLast();
} else {
Node current = head;
for (int i = 1; i < index; i++) {
current = current.getNext();
}
current.setNext(current.getNext().getNext());
size–;
}
}
// получение элемента по указанному индексу
public int get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
Node current = head;
for (int i = 0; i < index; i++) {
current = current.getNext();
}
return current.getValue();
}
// размер списка
public int size() {
return size;
}
// вывод всего списка на экран
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.getValue() + " ");
current = current.getNext();
}
System.out.println();
}
private static class Node {
private int value;
private Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}
private Node head; // головной/первый элемент списка
private int size; // размер списка
public LinkedList() {
head = null;
size = 0;
}
// вставка элемента в начало списка
public void addFirst(int value) {
head = new Node(value, head);
size++;
}
// вставка элемента в конец списка
public void addLast(int value) {
Node newNode = new Node(value, null);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
size++;
}
// вставка элемента по указанному индексу
public void add(int value, int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
if (index == 0) {
addFirst(value);
} else if (index == size) {
addLast(value);
} else {
Node current = head;
for (int i = 1; i < index; i++) {
current = current.getNext();
}
Node newNode = new Node(value, current.getNext());
current.setNext(newNode);
size++;
}
}
// удаление первого элемента из списка
public void removeFirst() {
if (head == null) {
throw new NoSuchElementException();
}
head = head.getNext();
size–;
}
// удаление последнего элемента из списка
public void removeLast() {
if (head == null) {
throw new NoSuchElementException();
}
if (head.getNext() == null) {
head = null;
} else {
Node current = head;
while (current.getNext().getNext() != null) {
current = current.getNext();
}
current.setNext(null);
}
size–;
}
// удаление элемента по указанному индексу
public void remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
if (index == 0) {
removeFirst();
} else if (index == size - 1) {
removeLast();
} else {
Node current = head;
for (int i = 1; i < index; i++) {
current = current.getNext();
}
current.setNext(current.getNext().getNext());
size–;
}
}
// получение элемента по указанному индексу
public int get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
Node current = head;
for (int i = 0; i < index; i++) {
current = current.getNext();
}
return current.getValue();
}
// размер списка
public int size() {
return size;
}
// вывод всего списка на экран
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.getValue() + " ");
current = current.getNext();
}
System.out.println();
}
private static class Node {
private int value;
private Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}
Л Олег
Спасибо огромное!
с такими вопросами в чатЖэПэГэ иди спроси
Похожие вопросы
- Помогите сделать java приложение! { СРОЧНО }
- Помогите написать java программу нахождения максимального числа из 4-х
- РЕБЯТ, ПОМОГИТЕ С ЗАДАЧЕЙ ПОЖАЛУЙСТА!
- Ошибка с java в intellij idea помогите пожалуйста
- Всем привет. Помогите плз. Мне нужна помощь тех кто действительно хорошо знает Java т. к мне нужно выбрать один из курсов
- Помогите доделать код на java. В форму пользователь вводит символ, который нужно заменить на #.
- Подскажите пожалуйста, как в данном коде Java сделать так, чтоб при нажатии цифры 3 программа завершала свою работу?
- Помогите понять как решить задание по Java.
- Не выводит изображение в JAVA
- Помогите разобрать программу java