linkedList done

This commit is contained in:
Libkyy
2022-03-18 19:41:10 +02:00
parent e9101d53ea
commit 9f2908cd46
7 changed files with 142 additions and 69 deletions

View File

@@ -1,8 +1,8 @@
package Phonebook; package Phonebook;
import org.junit.Test; import org.testng.annotations.Test;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.*;
public class PhonebookTest public class PhonebookTest
{ {

View File

@@ -1,8 +1,8 @@
package Phonebook; package Phonebook;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
public class YellowPagesTest public class YellowPagesTest
{ {

View File

@@ -2,79 +2,115 @@ package ScuffedLinkedList;
public class linkedlist public class linkedlist
{ {
private node head; public node head;
private node tail; public node tail;
private int size; public int size;
linkedlist()
{
}
linkedlist(int value) linkedlist(int value)
{ {
this.head = new node(value); node inserted =new node(value);
this.head = inserted;
this.tail = inserted;
} }
public node getNode(int index)
{
if(index == 0)
return head;
else if(index == size)
return tail;
else if(index > size/2 && size > 10)
{
node current = tail;
for(int count = size; count > index-2; count--)
current = current.prev;
return current;
}
else if(index <= size/2 || size < 10)
{
node current = head;
for(int count = 0; count < index-2; count++)
current = current.next;
return current;
}
else
return null;
}
public void add(int value, int index) public void add(int value, int index)
{ {
node inserted = new node(value);
if(index == 0) if(index == 0)
{ {
if(head.next == null) addFirst(value);
}
else if(index == size)
{ {
addLast(value);
} }
else else
{ {
inserted.next = head; node inserted = new node(value);
head.prev = inserted; node target = getNode(index);
inserted.next = target.next;
inserted.prev = target;
target.next = inserted;
} }
}
else if(index <= size)
{
if(index < size/2)
{
int counter = 0;
node previous = head;
node current = previous.prev;
while (counter < index - 1) {
counter++;
current = current.next;
previous = previous.next;
}
inserted.next = current;
inserted.prev = previous;
current.prev = inserted;
previous.next = inserted;
size++; size++;
} }
else
public void addLast(int value)
{ {
node target = tail;
node inserted = new node(value);
int counter = size; target.next = inserted;
node previous = tail; inserted.prev = target;
node current = previous.prev;
while (counter > index) {
counter--;
current = current.prev;
previous = previous.prev;
}
inserted.next = current;
inserted.prev = previous;
current.prev = inserted;
previous.next = inserted;
}
}
else if(index >= size)
{
inserted.prev = tail;
tail.next = inserted;
tail = inserted; tail = inserted;
} }
size++;
public void addFirst(int value)
{
node target = head;
node inserted = new node(value);
target.prev = inserted;
inserted.next = target;
head = inserted;
}
public void delete(int index)
{
node target = getNode(index);
node prev,next;
prev = target.prev;
next = target.next;
if(prev == null)
head = next;
else if(next == null)
tail = prev;
else
{
prev.next = next;
next.prev = prev;
}
size--;
} }
} }

View File

@@ -0,0 +1,33 @@
package ScuffedLinkedList;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class linkedlistTest
{
@Test
void getNode()
{
linkedlist L1 = new linkedlist(50);
L1.add(49,0);
assertEquals(50, L1.getNode(1).getValue());
L1.add(30,1);
assertEquals(30, L1.getNode(1).getValue());
}
@Test
void delete()
{
linkedlist L2 = new linkedlist(10);
L2.addFirst(20);
L2.addFirst(30);
assertEquals(10, L2.tail.getValue());
assertEquals(30, L2.head.getValue());
L2.delete(L2.size);
}
}

View File

@@ -20,7 +20,9 @@ public class node
this.data = data; this.data = data;
} }
public int getData()
public int getValue()
{ {
return data; return data;
} }

View File

@@ -1,10 +1,10 @@
package Stopwatch; package Stopwatch;
import org.junit.Test; import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*; import static org.testng.AssertJUnit.assertEquals;
public class StopWatchTest public class StopWatchTest
{ {

View File

@@ -1,14 +1,16 @@
package voltag; package voltag;
import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
public class voltTest public class voltTest
{ {
@org.junit.Test @Test
public void calc_mean() public void calc_mean()
{ {
Random r = new Random(); Random r = new Random();
@@ -23,7 +25,7 @@ public class voltTest
@org.junit.Test @Test
public void report() public void report()
{ {
Random r = new Random(); Random r = new Random();