University of the People Programming Assignment unit 3 CS 3303 - Data Structures Instructions: Develop an algorithm using the Java programming language that implements a basic stack data structure. Your stack must have the ability to push entries on the stack. Your stack must also have the ability to pop entries off of the stack. import java.util.EmptyStackException;
Stack Implementation in Java class Stack { private Node top; // Top of the stack // Node class for linked list implementation private class Node { int inspectionNumber; Node next; Node(int inspectionNumber) { } this.inspectionNumber = inspectionNumber; } // Method to push items onto the stack (Adapted from Shaffer, 2011) public void push(int inspectionNumber) { Node newNode = new Node(inspectionNumber); newNode.next = top; top = newNode; }
// Method to pop items off of the stack (Adapted from Shaffer, 2011) public int pop() { if (isEmpty()) { throw new EmptyStackException(); } int inspectionNumber = top.inspectionNumber; top = top.next; return inspectionNumber; } // Method to check if the stack is empty public boolean isEmpty() { return top == null; } // Method to print the content of the stack public void printStack() { Node current = top; while (current != null) { System.out.println("Inspection: " + current.inspectionNumber); current = current.next; }