stinky stack

This commit is contained in:
linly
2022-04-05 14:09:58 +02:00
parent 43f5063196
commit aae40d7982
5 changed files with 217 additions and 55 deletions

View File

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

View File

@@ -1,68 +1,54 @@
package ScuffedLinkedList; package ScuffedLinkedList;
public class linkedlist public class linkedlist {
{
public node head; public node head;
public node tail; public node tail;
public int size; public int size;
linkedlist() linkedlist() {
{
size = -1; size = -1;
} }
linkedlist(int value) linkedlist(int value) {
{ node inserted = new node(value);
node inserted =new node(value);
this.head = inserted; this.head = inserted;
this.tail = inserted; this.tail = inserted;
head.prev = tail;
tail.next = head;
size = 0; size = 0;
} }
public node getNode(int index) public node getNode(int index) {
{
if(index == 0) if (index == 0)
return head; return head;
else if(index == size) else if (index == size)
return tail; return tail;
else if(index > size/2 && size > 10) else
{ {
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; node current = head;
for(int count = 0; count < index-1; count++) for (int count = 0; count < index-1; count++)
current = current.next; current = current.next;
return current; return current;
} }
else
return null;
} }
public void add(int value, int index) public void add(int value, int index) {
{ if (index == 0) {
if(index == 0)
{
addFirst(value); addFirst(value);
} } else if (index == size) {
else if(index == size)
{
addLast(value); addLast(value);
} }
else else {
{
node inserted = new node(value); node inserted = new node(value);
node target = getNode(index); node target = getNode(index);
@@ -73,48 +59,79 @@ public class linkedlist
size++; size++;
} }
public void addLast(int value) public void addLast(int value) {
{
node target = tail;
node inserted = new node(value); node inserted = new node(value);
target.next = inserted; tail.next = inserted;
inserted.prev = target; inserted.prev = tail;
inserted.next = head;
tail = inserted; tail = inserted;
head.prev = tail;
tail.next = head;
size++; size++;
} }
public void addFirst(int value) public void addFirst(int value) {
{
node target = head; node target = head;
node inserted = new node(value); node inserted = new node(value);
target.prev = inserted; target.prev = inserted;
inserted.prev = tail;
inserted.next = target; inserted.next = target;
head = inserted; head = inserted;
head.prev = tail;
tail.next = head;
size++; size++;
} }
public void delete(int index) public void delete(int index) {
{ node target = getNode(index);
node target = getNode(index); node prev, next;
node prev,next;
prev = target.prev; prev = target.prev;
next = target.next; next = target.next;
if(prev == null) if (prev == tail)
head = next; head = next;
else if(next == null) else if (next == head)
tail = prev; tail = prev;
else else {
{
prev.next = next; prev.next = next;
next.prev = prev; next.prev = prev;
} }
size--; 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++;
}
} }
} }

View File

@@ -31,4 +31,37 @@ class linkedlistTest
L2.delete(L2.size); 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();
}
}