Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions vsfe/src/main/java/com/example/vsfe/item24/LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions vsfe/src/main/java/com/example/vsfe/item27/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.vsfe.item27;

import lombok.Getter;

@Getter
public class Node <E>{
E data;
Node<E> next, prev;

public Node (E data) {
this.data = data;
}
}
49 changes: 49 additions & 0 deletions vsfe/src/main/java/com/example/vsfe/item27/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.vsfe.item27;

public class Queue <E> {
private Node<E> 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<E> 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;
}
}
45 changes: 45 additions & 0 deletions vsfe/src/main/java/com/example/vsfe/item27/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.vsfe.item27;

public class Stack <E> {
private Node<E> 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<E> 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;
}
}
31 changes: 31 additions & 0 deletions vsfe/src/test/java/com/example/vsfe/item27/QueueTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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());
}
}
29 changes: 29 additions & 0 deletions vsfe/src/test/java/com/example/vsfe/item27/StackTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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());
}
}