Double LinkedList

master
3009594 2024-08-24 22:33:41 +02:00
parent f560c17a38
commit d0be8d868c
9 changed files with 125 additions and 3 deletions

View File

@ -0,0 +1,72 @@
package LinkedList.Doubly;
public class ElementenList {
Node head;
Node newNode;
public ElementenList(){
newNode = new Node();
}
public void print() {
Node temp = head;
System.out.print("[");
while(temp != null) {
System.out.print(temp.value + " ");
temp = temp.next;
}
System.out.print("]");
}
// Add Methoden
public Node addLast(int value) {
Node newNode = new Node();
newNode.value = value;
if (head == null) {
head = newNode;
return newNode;
}
Node temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
return newNode;
}
public Node addFirst(int value) {
Node newNode = new Node();
newNode.value = value;
if (head == null) {
head = newNode;
return newNode;
}
newNode.next = head;
head = newNode;
return newNode;
}
public Node addAtIndex(int value,int index) {
if (head == null) {
head = newNode;
return newNode;
}
if (index < 0)
return null;
newNode.value = value;
Node temp = head;
for (int i = 0; i < index - 1 && temp.next != null; i++)
temp = temp.next;
newNode.next = temp.next;
newNode.prev = temp;
temp.next = newNode;
return newNode;
}
}

View File

@ -0,0 +1,8 @@
package LinkedList.Doubly;
public class Node {
int value;
Node next;
Node prev;
}

View File

@ -0,0 +1,24 @@
package LinkedList.Doubly;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
class Test {
private ElementenList list;
@BeforeEach
public void list() {
list = new ElementenList();
}
@org.junit.jupiter.api.Test
void testaddLast() {
Node temp = list.addLast(12);
temp = list.addLast(112);
assertEquals(112,11);
}
}

View File

@ -1,4 +1,4 @@
package Linked_List; package LinkedList.Single;
public class ElementList { public class ElementList {
Node head; Node head;

View File

@ -1,4 +1,4 @@
package Linked_List; package LinkedList.Single;
public class Node { public class Node {
int value; int value;

View File

@ -1,4 +1,4 @@
package Linked_List; package LinkedList.Single;
public class Test { public class Test {

View File

@ -0,0 +1,18 @@
package LinkedList.Single;
import LinkedList.Doubly.ElementenList;
public class Teste {
public static void main(String[] args) {
ElementenList list = new ElementenList();
list.addLast(5);
list.addLast(4);
list.addLast(3);
list.addAtIndex(10, 5);
list.print();
}
}