Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 3db2f0b

Browse files
committed
Лабораторная работа №17
1 parent 180a3dd commit 3db2f0b

File tree

8 files changed

+225
-0
lines changed

8 files changed

+225
-0
lines changed

students/23K0815/23K0815-p17/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0815</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0815-p17</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
public abstract class CardCatalogTester {
8+
public static void main(String[] args) {
9+
// Используем try-with-resources для автоматического закрытия Scanner
10+
try (Scanner scanner = new Scanner(System.in)) {
11+
List<Node> nodes = new ArrayList<>();
12+
int choice;
13+
14+
do {
15+
System.out.println("\nМеню:");
16+
System.out.println("1. Добавить элемент");
17+
System.out.println("0. Выход");
18+
System.out.print("Выберите действие: ");
19+
choice = scanner.nextInt();
20+
scanner.nextLine(); // Для очистки буфера
21+
22+
switch (choice) {
23+
case 1:
24+
Node node = new Node(scanner); // Создаем новый узел, передавая Scanner
25+
nodes.add(node);
26+
break;
27+
case 0:
28+
System.out.println("Выход из программы.");
29+
break;
30+
default:
31+
System.out.println("Неверный выбор, попробуйте снова.");
32+
break;
33+
}
34+
} while (choice != 0);
35+
} // Scanner будет закрыт автоматически здесь
36+
}
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class ListUtil {
7+
private List<Node> nodes;
8+
9+
// Конструктор для создания пустого списка
10+
public ListUtil() {
11+
this.nodes = new ArrayList<>();
12+
}
13+
14+
// Функция добавления элемента (узла) списка
15+
public void addNode(Node node) {
16+
nodes.add(node);
17+
}
18+
19+
// Функция удаления элемента из списка
20+
public void removeNode(int index) {
21+
if (index >= 0 && index < nodes.size()) {
22+
nodes.remove(index);
23+
System.out.println("Элемент удален.");
24+
} else {
25+
System.out.println("Индекс вне диапазона.");
26+
}
27+
}
28+
29+
// Функция вывода элемента (узла) списка на экран
30+
public void displayNodes() {
31+
if (isEmpty()) {
32+
System.out.println("Список пуст.");
33+
return;
34+
}
35+
for (int i = 0; i < nodes.size(); i++) {
36+
System.out.print("Элемент " + (i + 1) + ": ");
37+
nodes.get(i).displayAttributes();
38+
}
39+
}
40+
41+
// Функция очистки списка
42+
public void clearList() {
43+
nodes.clear();
44+
System.out.println("Список очищен.");
45+
}
46+
47+
// Функция проверки списка на пустоту
48+
public boolean isEmpty() {
49+
return nodes.isEmpty();
50+
}
51+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.Scanner;
4+
5+
class Node {
6+
private String name;
7+
private int age;
8+
9+
// Конструктор класса, принимающий Scanner
10+
public Node(Scanner scanner) {
11+
inputAttributes(scanner);
12+
}
13+
14+
// Метод для считывания атрибутов объекта с консоли
15+
public void inputAttributes(Scanner scanner) {
16+
System.out.print("Введите имя: ");
17+
this.name = scanner.nextLine();
18+
System.out.print("Введите возраст: ");
19+
this.age = scanner.nextInt();
20+
scanner.nextLine(); // Для очистки буфера
21+
}
22+
23+
// Метод для вывода атрибутов на экран
24+
public void displayAttributes() {
25+
System.out.println("Имя: " + name + ", Возраст: " + age);
26+
}
27+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
class CircularLinkedList {
4+
private Node head; // Указатель на первый узел
5+
6+
public CircularLinkedList() {
7+
this.head = null; // Изначально список пуст
8+
}
9+
10+
// Метод добавления элемента в конец списка
11+
public void append(int data) {
12+
Node newNode = new Node(data);
13+
if (head == null) { // Если список пуст
14+
head = newNode;
15+
newNode.next = head; // Связываем последний узел с первым
16+
} else {
17+
Node current = head;
18+
while (current.next != head) { // Находим последний узел
19+
current = current.next;
20+
}
21+
current.next = newNode; // Добавляем новый узел
22+
newNode.next = head; // Связываем новый узел с первым
23+
}
24+
}
25+
26+
// Метод отображения элементов списка
27+
public void display() {
28+
if (head == null) { // Если список пуст
29+
System.out.println("Список пуст");
30+
return;
31+
}
32+
Node current = head;
33+
do {
34+
System.out.print(current.data + " "); // Выводим данные узла
35+
current = current.next;
36+
} while (current != head); // Пока не вернемся к началу
37+
System.out.println();
38+
}
39+
40+
// Метод удаления элемента
41+
public void delete(int key) {
42+
if (head == null) { // Если список пуст
43+
return;
44+
}
45+
46+
Node current = head;
47+
Node previous = null;
48+
do {
49+
if (current.data == key) { // Если нашли узел с данными key
50+
if (previous != null) { // Если это не первый узел
51+
previous.next = current.next;
52+
} else { // Если это первый узел
53+
if (current.next == head) { // Если в списке только один узел
54+
head = null;
55+
} else {
56+
Node last = head;
57+
while (last.next != head) { // Находим последний узел
58+
last = last.next;
59+
}
60+
last.next = current.next; // Удаляем узел
61+
head = current.next; // Обновляем голову
62+
}
63+
}
64+
return;
65+
}
66+
previous = current;
67+
current = current.next;
68+
} while (current != head); // Если вернулись к началу, выходим
69+
}
70+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
public abstract class Main {
4+
public static void main(String[] args) {
5+
CircularLinkedList circularList = new CircularLinkedList();
6+
circularList.append(1);
7+
circularList.append(2);
8+
circularList.append(3);
9+
10+
circularList.display(); // Вывод: 1 2 3
11+
12+
circularList.delete(2);
13+
circularList.display(); // Вывод: 1 3
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
class Node {
4+
int data; // Данные узла
5+
Node next; // Ссылка на следующий узел
6+
7+
Node(int data) {
8+
this.data = data;
9+
this.next = null;
10+
}
11+
}

students/23K0815/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
<module>23K0815-p14</module>
2525
<module>23K0815-p15</module>
2626
<module>23K0815-p16</module>
27+
<module>23K0815-p17</module>
2728
</modules>
2829
</project>

0 commit comments

Comments
 (0)