From 059c8ae8560d8e4540323a884dbd7f9a7597e0c0 Mon Sep 17 00:00:00 2001 From: VSFe Date: Wed, 24 Mar 2021 18:14:52 +0900 Subject: [PATCH] item 27 Complete --- .../com/example/vsfe/item24/LinkedList.java | 3 ++ .../java/com/example/vsfe/item27/Node.java | 13 +++++ .../java/com/example/vsfe/item27/Queue.java | 49 +++++++++++++++++++ .../java/com/example/vsfe/item27/Stack.java | 45 +++++++++++++++++ .../com/example/vsfe/item27/QueueTest.java | 31 ++++++++++++ .../com/example/vsfe/item27/StackTest.java | 29 +++++++++++ 6 files changed, 170 insertions(+) create mode 100644 vsfe/src/main/java/com/example/vsfe/item27/Node.java create mode 100644 vsfe/src/main/java/com/example/vsfe/item27/Queue.java create mode 100644 vsfe/src/main/java/com/example/vsfe/item27/Stack.java create mode 100644 vsfe/src/test/java/com/example/vsfe/item27/QueueTest.java create mode 100644 vsfe/src/test/java/com/example/vsfe/item27/StackTest.java diff --git a/vsfe/src/main/java/com/example/vsfe/item24/LinkedList.java b/vsfe/src/main/java/com/example/vsfe/item24/LinkedList.java index e11e737..b869564 100644 --- a/vsfe/src/main/java/com/example/vsfe/item24/LinkedList.java +++ b/vsfe/src/main/java/com/example/vsfe/item24/LinkedList.java @@ -12,6 +12,7 @@ public LinkedList() {} public void insert(int x) { Node next = new Node(x); + size++; if(this.head != null) { head.prev = next; next.next = head; @@ -22,6 +23,8 @@ public void insert(int x) { public int remove() { if (isEmpty()) throw new IllegalStateException(); + size--; + int result = head.getData(); head = head.next; head.prev = null; diff --git a/vsfe/src/main/java/com/example/vsfe/item27/Node.java b/vsfe/src/main/java/com/example/vsfe/item27/Node.java new file mode 100644 index 0000000..0c7d8eb --- /dev/null +++ b/vsfe/src/main/java/com/example/vsfe/item27/Node.java @@ -0,0 +1,13 @@ +package com.example.vsfe.item27; + +import lombok.Getter; + +@Getter +public class Node { + E data; + Node next, prev; + + public Node (E data) { + this.data = data; + } +} \ No newline at end of file diff --git a/vsfe/src/main/java/com/example/vsfe/item27/Queue.java b/vsfe/src/main/java/com/example/vsfe/item27/Queue.java new file mode 100644 index 0000000..d23ed21 --- /dev/null +++ b/vsfe/src/main/java/com/example/vsfe/item27/Queue.java @@ -0,0 +1,49 @@ +package com.example.vsfe.item27; + +public class Queue { + private Node frontNode, rearNode; + private int length; + + public Queue () { + frontNode = null; + rearNode = null; + length = 0; + } + + public boolean isEmpty() { return length == 0; } + public int getLength() { return length; } + + public void push(E data) { + Node node = new Node<>(data); + + if (isEmpty()) { + frontNode = node; + } else { + rearNode.next = node; + node.prev = rearNode; + } + rearNode = node; + length++; + } + + public E pop() { + if (isEmpty()) throw new IllegalStateException(); + E result = frontNode.data; + + length--; + frontNode = frontNode.next; + + if (isEmpty()) { + rearNode = null; + } else { + frontNode.prev = null; + } + + return result; + } + + public E front() { + if (isEmpty()) throw new IllegalStateException(); + return frontNode.data; + } +} diff --git a/vsfe/src/main/java/com/example/vsfe/item27/Stack.java b/vsfe/src/main/java/com/example/vsfe/item27/Stack.java new file mode 100644 index 0000000..4f4a0db --- /dev/null +++ b/vsfe/src/main/java/com/example/vsfe/item27/Stack.java @@ -0,0 +1,45 @@ +package com.example.vsfe.item27; + +public class Stack { + private Node topNode; + private int length; + + public Stack() { + length = 0; + this.topNode = null; + } + + public boolean isEmpty() { return length == 0; } + public int getLength() { return length; } + + public void push(E data) { + Node node = new Node<>(data); + + if(!isEmpty()) { + topNode.next = node; + node.prev = topNode; + } + topNode = node; + length++; + } + + public E pop() { + if(isEmpty()) throw new IllegalStateException(); + E result = topNode.data; + + length--; + topNode = topNode.prev; + + if (!isEmpty()) { + topNode.next = null; + } + + return result; + } + + public E top() { + if(isEmpty()) throw new IllegalStateException(); + + return topNode.data; + } +} diff --git a/vsfe/src/test/java/com/example/vsfe/item27/QueueTest.java b/vsfe/src/test/java/com/example/vsfe/item27/QueueTest.java new file mode 100644 index 0000000..309e967 --- /dev/null +++ b/vsfe/src/test/java/com/example/vsfe/item27/QueueTest.java @@ -0,0 +1,31 @@ +package com.example.vsfe.item27; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class QueueTest { + @Test + public void ArrayTest() { + Queue queue = new Queue<>(); + queue.push(1); + queue.push(2); + queue.push(3); + queue.push(4); + queue.push(5); + + assertEquals(5, queue.getLength()); + assertEquals(1, queue.front()); + + assertEquals(1, queue.pop()); + assertEquals(2, queue.pop()); + assertEquals(3, queue.pop()); + assertEquals(4, queue.pop()); + + assertEquals(5, queue.front()); + assertFalse(queue.isEmpty()); + queue.pop(); + + assertTrue(queue.isEmpty()); + } +} diff --git a/vsfe/src/test/java/com/example/vsfe/item27/StackTest.java b/vsfe/src/test/java/com/example/vsfe/item27/StackTest.java new file mode 100644 index 0000000..9fd9c1b --- /dev/null +++ b/vsfe/src/test/java/com/example/vsfe/item27/StackTest.java @@ -0,0 +1,29 @@ +package com.example.vsfe.item27; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class StackTest { + @Test + public void ArrayTest() { + Stack stack = new Stack<>(); + stack.push(5); + stack.push(1); + stack.push(2); + stack.push(3); + + assertEquals(4, stack.getLength()); + assertEquals(3, stack.top()); + + assertEquals(3, stack.pop()); + assertEquals(2, stack.pop()); + assertEquals(1, stack.pop()); + + assertEquals(5, stack.top()); + assertFalse(stack.isEmpty()); + stack.pop(); + + assertTrue(stack.isEmpty()); + } +}