Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@ public class hw09_LinkedList {
public static void main(String[] args) {
// Пример использования
LinkedList myList = new LinkedList();
myList.append(1);
myList.append(2);
myList.append(4);
myList.append(6);

myList.prepend(1);
myList.print();
myList.insertAt(5,3);
myList.print();
myList.insertAt(3,2);
myList.print();
myList.insertAt(0,0);
myList.print();

myList.remove(5);
myList.print();
myList.remove(5);
myList.print();

myList.removeAt(0);
myList.print();
myList.removeAt(4);
myList.print();

myList.clear();
myList.print();
}
}
Expand Down Expand Up @@ -38,29 +60,32 @@ public void append(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
tail = newNode;
size++;
}

// Вставка элемента в начало списка
public void prepend(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
head = newNode;
size++;
}

// Вставка элемента по индексу
public void insertAt(int data, int index) {
// FIXME: доработайте метод, добавив проверку на выход за границы списка
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index " + index + " is out of bounds for this array");
}

if (index == 0) {
prepend(data);
} else if (index == size) {
Expand All @@ -81,19 +106,43 @@ public void insertAt(int data, int index) {

// Удаление элемента по значению
public Integer remove(int data) {
// TODO: напишите реализацию метода
return 0;
int index = 0;
for (Node current = head; current != null; current = current.next, index++) {
if (Objects.equals(data, current.data)) {
return removeAt(index);
}
}
return -1;
}

// Удаление элемента по индексу
public Integer removeAt(int index) {
// TODO: напишите реализацию метода
return 0;
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index " + index + " is out of bounds for this array");
}

Node current = head;
if (index == 0) {
head = head.next;
} else {
Node previous = null;
for (int i = 0; i < index; i++) {
previous = current;
current = current.next;
}
previous.next = current.next;
}
size--;
return current.data;
}

// Получение элемента по индексу
public Integer getAt(int index) {
// FIXME: доработайте метод, добавив проверку на выход за границы списка
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index " + index + " is out of bounds for this array");
}

Node current = head;
for (int i = 0; i < index; i++) {
current = current.next;
Expand All @@ -114,6 +163,9 @@ public int getSize() {
// Очистка списка
public void clear() {
// TODO: напишите реализацию метода
head = null;
tail = null;
size = 0;
}

// Вывод списка в консоль
Expand All @@ -125,4 +177,4 @@ public void print() {
}
System.out.println("null");
}
}
}