Skip to content
Open
Show file tree
Hide file tree
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
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
95 changes: 95 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>

<groupId>org.example</groupId>
<artifactId>taskCore1</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write versions in properties

<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>hippodrome</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.8.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>
32 changes: 32 additions & 0 deletions src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.example;


public class Main {
public static void main(String[] args) {
MyLinkedList list = new MyLinkedList();
list.add(0,"aaa");
list.add(1,"eee");
list.add(2,"bbb");
list.add(3,"rrr");
//list.printAll();

//list.addFirst("yyy");
//list.addLast("qqq");
//list.printAll();


// System.out.println(list.getFirst().getValue());
// System.out.println(list.getLast().getValue());
System.out.println(list.get(1).getValue());

// list.removeFirst();
// list.printAll();

// list.removeLast();
// list.printAll();

// list.remove(2);
// list.printAll();

}
}
136 changes: 136 additions & 0 deletions src/main/java/org/example/MyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package org.example;

public class MyLinkedList<T> {

private Node first = new Node();
private Node last = new Node();


public MyLinkedList() {
first.next = last;
last.prev = first;
}

public void printAll() {
Node current = first.next;
while ((current != null) && (current != last)) {
System.out.println(current.value);
current = current.next;
}
}

// size() - returns the size of the list
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not use comments. turn it into javaDock

public int size() {
Node current = first.next;
int count = 0;
while ((current != null) && (current != last)) {
count++;
current = current.next;
}
return count;
}

// addFirst(el) - adds the element in the beginning of the list
public void addFirst(T value) {
Node current = new Node();
current.value = value;
current.prev = first;
current.next = first.next;
first.next = current;
}

// addLast(el) - adds the element in the end of the list
public void addLast(T value) {
Node current = new Node();
current.value = value;
current.prev = last.prev;
current.next = last;
last.prev.next = current;
last.prev = current;
}

// add(index, el) - adds the element in the list by index
public void add(int index, T value) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better return somthing.

Node current = new Node();
current.value = value;
Node previous = first;
if (index == 0) {
current.prev = first;
current.next = last;

first.next = current;
last.prev = current;
} else {
for (int i = 0; i < index; i++) {
previous = previous.next;
if (previous == last) {
throw new StringIndexOutOfBoundsException("Index out of bounds");
}
}
current.prev = previous;
current.next = previous.next;
previous.next.prev = current;
previous.next = current;
}
}

// getFirst() - returns the first element of the list
public Node getFirst() {
return first.next;
}

// getLast() - returns the last element of the list
public Node getLast() {
return last.prev;
}

// get(index) - returns the element by index
public Node get(int index) {
Node previous = first;
for (int i = 0; i < index; i++) {
if (previous == last.prev) {
throw new NullPointerException("Index out of bounds");
}
previous = previous.next;
}
return previous.next;
}

// removeFirst() - retrieve and remove the first element of the list
public void removeFirst() {
first.next = first.next.next;
first.next.prev = first;
}

// removeLast() - retrieve and remove the last element of the list
public void removeLast() {
last.prev = last.prev.prev;
last.prev.next = last;
}

// remove(index) - retrieve and remove the element of the list by index
public void remove(int index) {
Node previous = first;
for (int i = 0; i < index; i++) {
if (previous == last.prev) {
throw new NullPointerException("Index out of bounds");
}
previous = previous.next;
}
previous.next = previous.next.next;
previous.next.prev = previous;
}


public static class Node<T> {
private Node prev;
private T value;
private Node next;

public T getValue() {
return value;
}
}

}

Loading