I use the InteIIij IDEA
Complete or addmodify MyQueue.java and MyStack.java to enable MyQueueJUnitTest.java and MyStackJUnitTest.java to run
No need to be confused because maybe I didn't write it well
MyStack:
package utils; import org.w3c.dom.Node; import java.util.EmptyStackException; /***************************************************************** * This class MyStack implements a LIFO data structure using a * Singly Linked List (SLL). *****************************************************************/ public class MyStack { private Node first; private Node last; private int size; public MyStack() { first = null; last = null; size = 0; } //TODO : Complete Body with Data Fields, Methods and Classes public boolean isEmpty() { return last == null && size == 0; } public E peek() { if (isEmpty()) { return (E) "Empty"; } else { return last.data; } } private void detach() { Node oldLast = last; last = last.next; oldLast.next = null; } public E pop() { E lastItem = last.data; detach(); size--; return lastItem; } public E push(E item) { Node oldLast = last; Node newNode = new Node<>(oldLast, item); last = newNode; if (isEmpty()) first = newNode; size++; return item; } public int size() { return size; } public String toString() { if (isEmpty()) { return "[]"; } else { StringBuilder result = new StringBuilder("[" + last.data); for (Node node = last.next; node != null; node = node.next) result.append(", ").append(node.data); return result + "]"; } } static class Node { E data; Node next; public Node(E data) { this(null, data); } public Node(Node next, E data) { this.data = data; this.next = next; } } }
MyQueue:
package utils; import org.w3c.dom.Node; import java.util.NoSuchElementException; /***************************************************************** * This class MyQueue implements a FIFO data structure using a * Singly Linked List (SLL). *****************************************************************/ public class MyQueue { Node top; public class Node { E data; Node next; Node(E data) { this.data = data; next = null; } } //TODO : Complete Body with Data Fields, Methods and Classes public MyQueue() { top = null; } public void add(E n) { Node temp = new Node(n); if (top == null) { top = temp; } else { Node t = top; while (t.next != null) { t = t.next; } t.next = temp; } } public E remove() { if (top == null) { System.out.println("Queue is Empty"); return null; } else { E ans = top.data; top = top.next; return ans; } } public E peek() { return top.data; } public boolean isEmpty() { return top == null; } public int size() { int s = 0; if (top == null) { return s; } else { Node t = top; while (t != null) { s++; t = t.next; } } return s; } public static void evensBeforeOdds(MyQueue queue) { MyQueue even=new MyQueue(); MyQueue odd=new MyQueue(); Node temp=queue.top; while(temp!=null) { int n=(int) temp.data; if(n%2==0) { even.add(n); } else { odd.add(n); } temp=temp.next; } System.out.println("Queue after modifying:"+' '); Node evens=even.top; while(evens!=null) { System.out.print(evens.data+" "); evens=evens.next; } Node odds=odd.top; while(odds!=null) { System.out.print(odds.data+" "); odds=odds.next; } } public void display() { if (top == null) { System.out.println("Queue is empty"); } Node temp = top; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } }
MyStackJUnitTest:
package tests.junit; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; import utils.MyStack; import java.util.EmptyStackException; public class MyStackJUnitTest { @Test void testMyStack() { MyStack stack = new MyStack<>(); System.out.println("---------------- Test Default Constructor( ) ----------------"); System.out.println("size before: " + stack.size()); System.out.println("Stack Object Created "); assertEquals(0, stack.size()); assertTrue(stack.isEmpty()); System.out.println(stack); System.out.println("size after: " + stack.size()); System.out.println(); } @Test void testPushE() { MyStack stack = new MyStack<>(); System.out.println("---------------- Test push(item) ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(stack); System.out.println("size: " + stack.size()); assertEquals(7, stack.size()); System.out.println(); } @Test void testIsEmpty() { System.out.println(); MyStack stack = new MyStack<>(); System.out.println("-------------- Test isEmpty() --------------"); System.out.println(stack); assertEquals(0, stack.size()); assertTrue(stack.isEmpty()); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); assertEquals(7, stack.size()); assertFalse(stack.isEmpty()); System.out.println(stack); } @Test void testPeekE() { System.out.println(); MyStack stack = new MyStack<>(); System.out.println("---------------- Test peek() ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(stack); System.out.println("size before: " + stack.size()); assertEquals("Violet", stack.peek()); assertEquals("Violet", stack.pop()); assertEquals("Indigo", stack.peek()); assertEquals("Indigo", stack.pop()); assertEquals("Blue" , stack.peek()); assertEquals("Blue" , stack.pop()); System.out.println(stack); System.out.println("size after: " + stack.size()); assertEquals("Green" , stack.pop()); assertEquals("Yellow", stack.pop()); assertEquals("Orange", stack.pop()); assertEquals("Red" , stack.pop()); assertThrows(EmptyStackException.class, stack::pop); System.out.println(); } @Test void testPopE() { System.out.println(); MyStack stack = new MyStack<>(); System.out.println("---------------- Test pop() ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(stack); System.out.println("size before: " + stack.size()); assertEquals("Violet", stack.pop()); assertEquals("Indigo", stack.pop()); assertEquals("Blue" , stack.pop()); assertEquals("Green" , stack.pop()); assertEquals("Yellow", stack.pop()); assertEquals("Orange", stack.pop()); assertEquals("Red" , stack.pop()); System.out.println(stack); System.out.println("size after: " + stack.size()); //Throws Exception if Empty. assertThrows(EmptyStackException.class, stack::pop); System.out.println(); } @Test void testSize() { MyStack stack = new MyStack<>(); System.out.println("---------------- Test size() ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(stack); assertEquals(7, stack.size()); System.out.println("size end: " + stack.size()); System.out.println(); } @Test void testToString() { MyStack stack = new MyStack<>(); System.out.println("---------------- Test toString Display() ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(); System.out.println(); System.out.println("==== T E S T L I F O ==="); System.out.println(); //THIS IS DONE USING FIX (SEE LATER, IF NOT IMPLEMENTED IN THIS LAB) String value = "[Red, Orange, Yellow, Green, Blue, Indigo, Violet]"; assertEquals(value , stack.toString()); System.out.println(stack); assertEquals("Violet", stack.pop()); assertEquals("[Red, Orange, Yellow, Green, Blue, Indigo]" , stack.toString()); System.out.println(stack); assertEquals("Indigo", stack.pop()); assertEquals("[Red, Orange, Yellow, Green, Blue]" , stack.toString()); System.out.println(stack); assertEquals("Blue" , stack.pop()); assertEquals("[Red, Orange, Yellow, Green]" , stack.toString()); System.out.println(stack); assertEquals("Green" , stack.pop()); assertEquals("[Red, Orange, Yellow]" , stack.toString()); System.out.println(stack); assertEquals("Yellow", stack.pop()); assertEquals("[Red, Orange]" , stack.toString()); System.out.println(stack); assertEquals("Orange", stack.pop()); assertEquals("[Red]" , stack.toString()); System.out.println(stack); assertEquals("Red" , stack.pop()); assertEquals("[]" , stack.toString()); System.out.println(); } }
MyQueueJUnitTest:
package tests.junit; import org.junit.jupiter.api.Test; import utils.MyQueue; import java.util.NoSuchElementException; import static org.junit.jupiter.api.Assertions.*; public class MyQueueJUnitTest{ @Test void testMyQueue() { System.out.println(); MyQueue queue = new MyQueue<>(); System.out.println("---------------- Test Default Constructor( ) ----------------"); System.out.println("size before: " + queue.size()); System.out.println("queue Object Created "); assertEquals(0, queue.size()); assertTrue(queue.isEmpty()); System.out.println(queue); System.out.println("size after: " + queue.size()); System.out.println(); } @Test void testAdd() { System.out.println(); MyQueue queue = new MyQueue<>(); System.out.println("---------------- Test add(item) ----------------"); assertTrue(queue.add("Red")); assertTrue(queue.add("Orange")); assertTrue(queue.add("Yellow")); assertTrue(queue.add("Green")); assertTrue(queue.add("Blue")); assertTrue(queue.add("Indigo")); assertTrue(queue.add("Violet")); System.out.println(queue); System.out.println("size: " + queue.size()); assertEquals(7, queue.size()); System.out.println(); } @Test void testIsEmpty() { System.out.println(); MyQueue queue = new MyQueue<>(); System.out.println("-------------- Test isEmpty() --------------"); System.out.println(queue); assertEquals(0, queue.size()); assertTrue(queue.isEmpty()); assertTrue(queue.add("Red")); assertTrue(queue.add("Orange")); assertTrue(queue.add("Yellow")); assertTrue(queue.add("Green")); assertTrue(queue.add("Blue")); assertTrue(queue.add("Indigo")); assertTrue(queue.add("Violet")); assertEquals(7, queue.size()); assertFalse(queue.isEmpty()); System.out.println(queue); } @Test void testPeekE() { System.out.println(); MyQueue queue = new MyQueue<>(); System.out.println("---------------- Test remove() ----------------"); assertTrue(queue.add("Red")); assertTrue(queue.add("Orange")); assertTrue(queue.add("Yellow")); assertTrue(queue.add("Green")); assertTrue(queue.add("Blue")); assertTrue(queue.add("Indigo")); assertTrue(queue.add("Violet")); System.out.println(queue); System.out.println("size before: " + queue.size()); assertEquals("Red" , queue.peek()); assertEquals("Red" , queue.remove()); assertEquals("Orange" , queue.peek()); assertEquals("Orange" , queue.remove()); assertEquals("Yellow" , queue.peek()); assertEquals("Yellow" , queue.remove()); System.out.println(queue); System.out.println("size after: " + queue.size()); assertEquals("Green" , queue.remove()); assertEquals("Blue" , queue.remove()); assertEquals("Indigo" , queue.remove()); assertEquals("Violet" , queue.remove()); //Test for an Empty Queue assertNull(queue.peek()); System.out.println(); } @Test void testRemoveE() { System.out.println(); MyQueue queue = new MyQueue<>(); System.out.println("---------------- Test remove() ----------------"); assertTrue(queue.add("Red")); assertTrue(queue.add("Orange")); assertTrue(queue.add("Yellow")); assertTrue(queue.add("Green")); assertTrue(queue.add("Blue")); assertTrue(queue.add("Indigo")); assertTrue(queue.add("Violet")); System.out.println(queue); System.out.println("size before: " + queue.size()); assertEquals("Red" , queue.remove()); assertEquals("Orange" , queue.remove()); assertEquals("Yellow" , queue.remove()); assertEquals("Green" , queue.remove()); assertEquals("Blue" , queue.remove()); assertEquals("Indigo" , queue.remove()); assertEquals("Violet" , queue.remove()); System.out.println(queue); System.out.println("size after: " + queue.size()); //Throws Exception if Empty. assertThrows(NoSuchElementException.class, queue::remove); System.out.println(); } @Test void testSize() { MyQueue queue = new MyQueue<>(); System.out.println("---------------- Test size() ----------------"); assertTrue(queue.add("Red")); assertTrue(queue.add("Orange")); assertTrue(queue.add("Yellow")); assertTrue(queue.add("Green")); assertTrue(queue.add("Blue")); assertTrue(queue.add("Indigo")); assertTrue(queue.add("Violet")); System.out.println(queue); assertEquals(7, queue.size()); System.out.println("size end: " + queue.size()); System.out.println(); } @Test void testToString() { MyQueue queue = new MyQueue<>(); System.out.println("---------------- Test toString Display() ----------------"); assertTrue(queue.isEmpty()); assertTrue(queue.add("Red")); assertTrue(queue.add("Orange")); assertTrue(queue.add("Yellow")); assertTrue(queue.add("Green")); assertTrue(queue.add("Blue")); assertTrue(queue.add("Indigo")); assertTrue(queue.add("Violet")); System.out.println(); System.out.println(); System.out.println("==== T E S T F I F O ==="); System.out.println(); String value = "[Red, Orange, Yellow, Green, Blue, Indigo, Violet]"; assertEquals(value , queue.toString()); System.out.println(queue); assertEquals("Red", queue.remove()); assertEquals("[Orange, Yellow, Green, Blue, Indigo, Violet]" , queue.toString()); System.out.println(queue); assertEquals("Orange", queue.remove()); assertEquals("[Yellow, Green, Blue, Indigo, Violet]" , queue.toString()); System.out.println(queue); assertEquals("Yellow" , queue.remove()); assertEquals("[Green, Blue, Indigo, Violet]" , queue.toString()); System.out.println(queue); assertEquals("Green" , queue.remove()); assertEquals("[Blue, Indigo, Violet]" , queue.toString()); System.out.println(queue); assertEquals("Blue", queue.remove()); assertEquals("[Indigo, Violet]" , queue.toString()); System.out.println(queue); assertEquals("Indigo" , queue.remove()); assertEquals("[Violet]" , queue.toString()); System.out.println(queue); assertEquals("Violet" , queue.remove()); assertEquals("[]" , queue.toString()); System.out.println(); } }