This commit is contained in:
Libkyy
2022-04-21 14:46:04 +02:00
5 changed files with 232 additions and 42 deletions

View File

@@ -13,14 +13,21 @@ public class FindPathsMatrix
{6, 7, 8, 10},
{2, 5, 2, 8}
};
GetPaths(M1);
GetPaths(M1,3);
}
public static void GetPaths(int[][] M)
public static void GetPaths(int[][] M, int length)
{
int[][] output = MultiplyMatrix(M,M);
int count = 2;
int[][] output = M;
while(count <= length) {
output = MultiplyMatrix(output, M);
assert output != null;
count++;
}
for (int[] ints : output) System.out.println(Arrays.toString(ints));
}
public static int[][] MultiplyMatrix(int[][] M1, int[][] M2)

View File

@@ -1,31 +1,42 @@
package ScuffedLinkedList;
public class linkedlist
{
public class linkedlist {
public node head;
public node tail;
public int size;
<<<<<<< HEAD
linkedlist()
{
size = 0;
=======
linkedlist() {
size = -1;
>>>>>>> aae40d7982df045ed4b77fc7a23e6e6ebb893eb7
}
linkedlist(int value)
{
node inserted =new node(value);
linkedlist(int value) {
node inserted = new node(value);
this.head = inserted;
this.tail = inserted;
<<<<<<< HEAD
size = 1;
=======
head.prev = tail;
tail.next = head;
size = 0;
>>>>>>> aae40d7982df045ed4b77fc7a23e6e6ebb893eb7
}
public node getNode(int index)
{
public node getNode(int index) {
if(index == 0)
if (index == 0)
return head;
<<<<<<< HEAD
else if(index == size-1)
return tail;
@@ -40,28 +51,31 @@ public class linkedlist
else if(index <= size/2)
{
=======
else if (index == size)
return tail;
else
{
>>>>>>> aae40d7982df045ed4b77fc7a23e6e6ebb893eb7
node current = head;
for(int count = 0; count < index-1; count++)
for (int count = 0; count < index-1; count++)
current = current.next;
return current;
}
else
return null;
}
public void add(int value, int index)
{
if(index == 0)
{
public void add(int value, int index) {
if (index == 0) {
addFirst(value);
}
else if(index == size)
{
} else if (index == size) {
addLast(value);
}
else
{
else {
node inserted = new node(value);
node target = getNode(index);
@@ -72,50 +86,81 @@ public class linkedlist
size++;
}
public void addLast(int value)
{
node target = tail;
public void addLast(int value) {
node inserted = new node(value);
target.next = inserted;
inserted.prev = target;
tail.next = inserted;
inserted.prev = tail;
inserted.next = head;
tail = inserted;
head.prev = tail;
tail.next = head;
size++;
}
public void addFirst(int value)
{
public void addFirst(int value) {
node target = head;
node inserted = new node(value);
target.prev = inserted;
inserted.prev = tail;
inserted.next = target;
head = inserted;
head.prev = tail;
tail.next = head;
size++;
}
public void delete(int index)
{
public void delete(int index) {
node target = getNode(index);
node prev,next;
node prev, next;
prev = target.prev;
next = target.next;
if(prev == null)
if (prev == tail)
head = next;
else if(next == null)
else if (next == head)
tail = prev;
else
{
else {
prev.next = next;
next.prev = prev;
}
size--;
}
public void spin2()
{
int count = 0;
while(count != 2)
{
node temp = head;
while(temp.next != head)
{
System.out.print(temp + " ");
temp = temp.next;
}
count++;
}
}
public void backwards()
{
int count = 0;
while(count != 2)
{
node temp = tail;
while(temp.prev != tail)
{
System.out.print(temp + " ");
temp = temp.prev;
}
count++;
}
}
// Get factorial of number
}

View File

@@ -31,4 +31,37 @@ class linkedlistTest
L2.delete(L2.size);
}
@Test
void spin2()
{
linkedlist L1 = new linkedlist(50);
L1.add(49,1);
L1.add(48,1);
L1.add(47,1);
L1.add(46,1);
L1.add(45,1);
L1.add(44,1);
L1.add(43,1);
L1.add(42,1);
L1.add(41,1);
L1.add(40,1);
L1.add(39,1);
L1.add(38,1);
L1.add(37,1);
L1.spin2();
}
@Test
void backwards()
{
linkedlist L1 = new linkedlist(50);
L1.addLast(49);
L1.addLast(48);
L1.addLast(47);
L1.addLast(46);
L1.addLast(45);
L1.backwards();
}
}

View File

@@ -0,0 +1,58 @@
package ScuffedLinkedList;
public class stinkyStack {
node top;
node head;
public void push(int value)
{
if(head == null)
{
head = new node(value);
top = head;
}
else
{
node inserted = new node(value);
top.next = inserted;
top = inserted;
}
}
public void pop() {
if (top == null)
System.out.println("Stack underflow");
else if (top == head) {
System.out.println(head);
head = null;
top = null;
} else {
node pointer = head;
System.out.println(top);
while (pointer.next != top) {
pointer = pointer.next;
}
pointer.next = null;
top = pointer;
}
}
public void display()
{
node pointer = head;
while(pointer != null)
{
System.out.println(pointer);
pointer = pointer.next;
}
}
}

View File

@@ -0,0 +1,47 @@
package ScuffedLinkedList;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class stinkyStackTest {
@Test
void push()
{
stinkyStack stack = new stinkyStack();
stack.push(50);
assertEquals(50, stack.head.getValue());
assertEquals(50, stack.top.getValue());
stack.push(40);
assertEquals(50, stack.head.getValue());
assertEquals(40,stack.top.getValue());
}
@Test
void pop() {
stinkyStack stack = new stinkyStack();
stack.pop();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(50);
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
}
@Test
void display()
{
stinkyStack stack = new stinkyStack();
stack.pop();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(50);
stack.display();
}
}