From aae40d7982df045ed4b77fc7a23e6e6ebb893eb7 Mon Sep 17 00:00:00 2001 From: linly Date: Tue, 5 Apr 2022 14:09:58 +0200 Subject: [PATCH] stinky stack --- src/FindPathsPls/FindPathsMatrix.java | 15 ++- src/ScuffedLinkedList/linkedlist.java | 119 ++++++++++++--------- src/ScuffedLinkedList/linkedlistTest.java | 33 ++++++ src/ScuffedLinkedList/stinkyStack.java | 58 ++++++++++ src/ScuffedLinkedList/stinkyStackTest.java | 47 ++++++++ 5 files changed, 217 insertions(+), 55 deletions(-) create mode 100644 src/ScuffedLinkedList/stinkyStack.java create mode 100644 src/ScuffedLinkedList/stinkyStackTest.java diff --git a/src/FindPathsPls/FindPathsMatrix.java b/src/FindPathsPls/FindPathsMatrix.java index 1dede89..52cdd63 100644 --- a/src/FindPathsPls/FindPathsMatrix.java +++ b/src/FindPathsPls/FindPathsMatrix.java @@ -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); - assert output != null; + 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) diff --git a/src/ScuffedLinkedList/linkedlist.java b/src/ScuffedLinkedList/linkedlist.java index 28c18d1..0269386 100644 --- a/src/ScuffedLinkedList/linkedlist.java +++ b/src/ScuffedLinkedList/linkedlist.java @@ -1,68 +1,54 @@ package ScuffedLinkedList; -public class linkedlist -{ +public class linkedlist { public node head; public node tail; public int size; - linkedlist() - { + linkedlist() { size = -1; } - linkedlist(int value) - { - node inserted =new node(value); + linkedlist(int value) { + node inserted = new node(value); this.head = inserted; this.tail = inserted; + head.prev = tail; + tail.next = head; + size = 0; } - public node getNode(int index) - { + public node getNode(int index) { - if(index == 0) + if (index == 0) return head; - else if(index == size) + 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) - { + else + { 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); @@ -73,48 +59,79 @@ 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) - { - node target = getNode(index); - node prev,next; + public void delete(int index) { + node target = getNode(index); + node prev, next; - prev = target.prev; - next = target.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--; + 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++; + } } } diff --git a/src/ScuffedLinkedList/linkedlistTest.java b/src/ScuffedLinkedList/linkedlistTest.java index 9d35fca..ad06f69 100644 --- a/src/ScuffedLinkedList/linkedlistTest.java +++ b/src/ScuffedLinkedList/linkedlistTest.java @@ -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(); + + } } \ No newline at end of file diff --git a/src/ScuffedLinkedList/stinkyStack.java b/src/ScuffedLinkedList/stinkyStack.java new file mode 100644 index 0000000..e56b950 --- /dev/null +++ b/src/ScuffedLinkedList/stinkyStack.java @@ -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; + } + + } +} + + + + diff --git a/src/ScuffedLinkedList/stinkyStackTest.java b/src/ScuffedLinkedList/stinkyStackTest.java new file mode 100644 index 0000000..daa7d8f --- /dev/null +++ b/src/ScuffedLinkedList/stinkyStackTest.java @@ -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(); + + } +} \ No newline at end of file