-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
180a3dd
commit 3db2f0b
Showing
8 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>23K0815</artifactId> | ||
<groupId>ru.mirea.practice</groupId> | ||
<version>2024.1</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<artifactId>23K0815-p17</artifactId> | ||
<description>Массивы</description> | ||
</project> |
37 changes: 37 additions & 0 deletions
37
...23K0815/23K0815-p17/src/main/java/ru/mirea/practice/s0000001/task1/CardCatalogTester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ru.mirea.practice.s0000001.task1; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public abstract class CardCatalogTester { | ||
public static void main(String[] args) { | ||
// Используем try-with-resources для автоматического закрытия Scanner | ||
try (Scanner scanner = new Scanner(System.in)) { | ||
List<Node> nodes = new ArrayList<>(); | ||
int choice; | ||
|
||
do { | ||
System.out.println("\nМеню:"); | ||
System.out.println("1. Добавить элемент"); | ||
System.out.println("0. Выход"); | ||
System.out.print("Выберите действие: "); | ||
choice = scanner.nextInt(); | ||
scanner.nextLine(); // Для очистки буфера | ||
|
||
switch (choice) { | ||
case 1: | ||
Node node = new Node(scanner); // Создаем новый узел, передавая Scanner | ||
nodes.add(node); | ||
break; | ||
case 0: | ||
System.out.println("Выход из программы."); | ||
break; | ||
default: | ||
System.out.println("Неверный выбор, попробуйте снова."); | ||
break; | ||
} | ||
} while (choice != 0); | ||
} // Scanner будет закрыт автоматически здесь | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
students/23K0815/23K0815-p17/src/main/java/ru/mirea/practice/s0000001/task1/ListUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ru.mirea.practice.s0000001.task1; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class ListUtil { | ||
private List<Node> nodes; | ||
|
||
// Конструктор для создания пустого списка | ||
public ListUtil() { | ||
this.nodes = new ArrayList<>(); | ||
} | ||
|
||
// Функция добавления элемента (узла) списка | ||
public void addNode(Node node) { | ||
nodes.add(node); | ||
} | ||
|
||
// Функция удаления элемента из списка | ||
public void removeNode(int index) { | ||
if (index >= 0 && index < nodes.size()) { | ||
nodes.remove(index); | ||
System.out.println("Элемент удален."); | ||
} else { | ||
System.out.println("Индекс вне диапазона."); | ||
} | ||
} | ||
|
||
// Функция вывода элемента (узла) списка на экран | ||
public void displayNodes() { | ||
if (isEmpty()) { | ||
System.out.println("Список пуст."); | ||
return; | ||
} | ||
for (int i = 0; i < nodes.size(); i++) { | ||
System.out.print("Элемент " + (i + 1) + ": "); | ||
nodes.get(i).displayAttributes(); | ||
} | ||
} | ||
|
||
// Функция очистки списка | ||
public void clearList() { | ||
nodes.clear(); | ||
System.out.println("Список очищен."); | ||
} | ||
|
||
// Функция проверки списка на пустоту | ||
public boolean isEmpty() { | ||
return nodes.isEmpty(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
students/23K0815/23K0815-p17/src/main/java/ru/mirea/practice/s0000001/task1/Node.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ru.mirea.practice.s0000001.task1; | ||
|
||
import java.util.Scanner; | ||
|
||
class Node { | ||
private String name; | ||
private int age; | ||
|
||
// Конструктор класса, принимающий Scanner | ||
public Node(Scanner scanner) { | ||
inputAttributes(scanner); | ||
} | ||
|
||
// Метод для считывания атрибутов объекта с консоли | ||
public void inputAttributes(Scanner scanner) { | ||
System.out.print("Введите имя: "); | ||
this.name = scanner.nextLine(); | ||
System.out.print("Введите возраст: "); | ||
this.age = scanner.nextInt(); | ||
scanner.nextLine(); // Для очистки буфера | ||
} | ||
|
||
// Метод для вывода атрибутов на экран | ||
public void displayAttributes() { | ||
System.out.println("Имя: " + name + ", Возраст: " + age); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...3K0815/23K0815-p17/src/main/java/ru/mirea/practice/s0000001/task2/CircularLinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package ru.mirea.practice.s0000001.task2; | ||
|
||
class CircularLinkedList { | ||
private Node head; // Указатель на первый узел | ||
|
||
public CircularLinkedList() { | ||
this.head = null; // Изначально список пуст | ||
} | ||
|
||
// Метод добавления элемента в конец списка | ||
public void append(int data) { | ||
Node newNode = new Node(data); | ||
if (head == null) { // Если список пуст | ||
head = newNode; | ||
newNode.next = head; // Связываем последний узел с первым | ||
} else { | ||
Node current = head; | ||
while (current.next != head) { // Находим последний узел | ||
current = current.next; | ||
} | ||
current.next = newNode; // Добавляем новый узел | ||
newNode.next = head; // Связываем новый узел с первым | ||
} | ||
} | ||
|
||
// Метод отображения элементов списка | ||
public void display() { | ||
if (head == null) { // Если список пуст | ||
System.out.println("Список пуст"); | ||
return; | ||
} | ||
Node current = head; | ||
do { | ||
System.out.print(current.data + " "); // Выводим данные узла | ||
current = current.next; | ||
} while (current != head); // Пока не вернемся к началу | ||
System.out.println(); | ||
} | ||
|
||
// Метод удаления элемента | ||
public void delete(int key) { | ||
if (head == null) { // Если список пуст | ||
return; | ||
} | ||
|
||
Node current = head; | ||
Node previous = null; | ||
do { | ||
if (current.data == key) { // Если нашли узел с данными key | ||
if (previous != null) { // Если это не первый узел | ||
previous.next = current.next; | ||
} else { // Если это первый узел | ||
if (current.next == head) { // Если в списке только один узел | ||
head = null; | ||
} else { | ||
Node last = head; | ||
while (last.next != head) { // Находим последний узел | ||
last = last.next; | ||
} | ||
last.next = current.next; // Удаляем узел | ||
head = current.next; // Обновляем голову | ||
} | ||
} | ||
return; | ||
} | ||
previous = current; | ||
current = current.next; | ||
} while (current != head); // Если вернулись к началу, выходим | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
students/23K0815/23K0815-p17/src/main/java/ru/mirea/practice/s0000001/task2/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ru.mirea.practice.s0000001.task2; | ||
|
||
public abstract class Main { | ||
public static void main(String[] args) { | ||
CircularLinkedList circularList = new CircularLinkedList(); | ||
circularList.append(1); | ||
circularList.append(2); | ||
circularList.append(3); | ||
|
||
circularList.display(); // Вывод: 1 2 3 | ||
|
||
circularList.delete(2); | ||
circularList.display(); // Вывод: 1 3 | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
students/23K0815/23K0815-p17/src/main/java/ru/mirea/practice/s0000001/task2/Node.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ru.mirea.practice.s0000001.task2; | ||
|
||
class Node { | ||
int data; // Данные узла | ||
Node next; // Ссылка на следующий узел | ||
|
||
Node(int data) { | ||
this.data = data; | ||
this.next = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters