//StackADT
//Interface: StackADT
public interface StackADT
public void initializeStack();//Method to initialize the stack to an empty state.
public boolean isEmptyStack();//Method to determine whether the stack is empty.
public boolean isFullStack(); //Method to determine whether the stack is full.
public T peek(); //Method to return the top element of the stack.
public void push(T newItem); //Method to add newItem to the stack.
public void pop(); //Method to remove the top element of the stack.
}
// LinkedStackClass
//Class: LinkedStackClass implements
//Interface: StackADT
//Linked list implementation
public class LinkedStackClass
//Node Definition: inner class StackNode
private class StackNode
public T info;
public StackNode
//Default constructor
public StackNode() {
info = null;
link = null;
}
//Alternate constructor
public StackNode(T value, StackNode
info = value;
link = ptr;
}
public String toString() {
return info.toString();
}
}
//Instance variable for class LinkedStackClass
private StackNode
//Default constructor
public LinkedStackClass() {
stackTop = null;
}
public void initializeStack() {
stackTop = null;
}
public boolean isEmptyStack() {
return (stackTop == null);
}
public boolean isFullStack() {
return false;
}
public T peek() {
if (stackTop == null){
System.err.println("No top - the stack is empty!");
return stackTop.info;
}
else
return stackTop.info;
}
public void push(T newValue) {
StackNode
newNode = new StackNode
stackTop = newNode;
}
public void pop() {
if (stackTop == null)
System.err.println("Cannot pop from an empty stack!");
else
stackTop = stackTop.link;
}
}
//QueueADT
//Interface: QueueADT
public interface QueueADT
public void initializeQueue(); //Method to initialize the queue to an empty state.
public boolean isEmptyQueue(); //Method to determine whether the queue is empty.
public boolean isFullQueue(); //Method to determine whether the queue is full.
public T front() throws QueueUnderflowException; //Method to return the first element of the queue.
public T back() throws QueueUnderflowException; //Method to return the last element of the queue.
public void enqueue(T newItem); //Method to add newItem to the queue.
public void dequeue() throws QueueUnderflowException; //Method to remove the first element of the queue.
}
//QueueClass
//Class: QueueClass implements
//Interface: QueueADT
//Array-based implementation
public class QueueClass
private int maxQueueSize;
private int count;
private int queueFront;
private int queueRear;
private T[] list; //Array of references to the objects that store queue elements
//Default constructor
public QueueClass() {
maxQueueSize = 100;
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
list = (T[]) new Object[maxQueueSize];
}
//Alternate constructor
public QueueClass(int queueSize) {
if (queueSize <= 0) {
System.err.println("The size must be positive.");
System.err.println("Creating an array with size 100.");
maxQueueSize = 100;
}
else
maxQueueSize = queueSize;
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
list = (T[]) new Object[maxQueueSize];
}
public void initializeQueue() {
for (int i = queueFront; i < queueRear; i = (i + 1) % maxQueueSize)
list[i] = null;
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
}
public boolean isEmptyQueue() {
return (count == 0);
}
public boolean isFullQueue() {
return (count == maxQueueSize);
}
public int getCounter() {
return count;
}
public T front() throws QueueUnderflowException {
if (isEmptyQueue())
throw new QueueUnderflowException();
return (T) list[queueFront];
}
public T back() throws QueueUnderflowException {
if (isEmptyQueue())
throw new QueueUnderflowException();
return (T) list[queueRear];
}
public void enqueue(T newItem) {
if (isFullQueue())
System.err.println("Cannot enqueue in a full queue!");
else {
queueRear = (queueRear + 1) % maxQueueSize; // circular array
count++;
list[queueRear] = newItem;
}
}
public void dequeue() throws QueueUnderflowException {
if (isEmptyQueue())
throw new QueueUnderflowException();
count--;
list[queueFront] = null;
queueFront = (queueFront + 1) % maxQueueSize; // circular array
}
}
// Queue exception
public class QueueException extends RuntimeException {
//public QueueException() {
//}
public QueueException(String msg) {
super(msg);
}
}
//QueueOverflowException
public class QueueOverflowException extends QueueException
{
public QueueOverflowException()
{
super("Queue Overflow");
}
public QueueOverflowException(String msg)
{
super(msg);
}
}
//QueueUnderflowexception
public class QueueUnderflowException extends QueueException
{
public QueueUnderflowException()
{
super("Queue Underflow");
}
public QueueUnderflowException(String msg)
{
super(msg);
}
}
//StackQueueClient
import java.util.*;
public class StackQueueClient {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
LinkedStackClass
QueueClass
int[] a1 = getInt(sc, "Enter digits for stack (999) to stop: ");
for (int i = 0; i < a1.length; i++) {
intStack.push(a1[i]);
}
System.out.println("The original stack printed in direct order (bottom to top) is:");
printStack(intStack);
System.out.println();
System.out.println("The stack printed in reverse order (top to bottom) is: ");
printBackStack(intStack);
System.out.println();
System.out.println("The stack stores " + countItems(intStack) + " items");
System.out.println("The second number is " + getSecond(intStack));
int removeNum = getNum(sc, "Enter a number to remove: ");
removeItem(intStack, removeNum);
System.out.println("The stack after removing every occurrence of " + removeNum + " is: ");
printStack(intStack);
System.out.println();
int[] a2 = getInt(sc, "Enter digits for queue (999) to stop: ");
for (int i = 0; i < a2.length; i++) {
queue.enqueue(a2[i]);
}
System.out.println("The queue is: ");
printQueue(queue);
System.out.println();
System.out.println("The reversed queue is: ");
reverseQueue(queue);
printQueue(queue);
}
public static void printStack(LinkedStackClass
LinkedStackClass
while (!intStack.isEmptyStack()) {
tempStack.push(intStack.peek());
intStack.pop();
}
while (!tempStack.isEmptyStack()) {
System.out.print(tempStack.peek() + " ");
intStack.push(tempStack.peek());
tempStack.pop();
}
}
public static void printBackStack(LinkedStackClass
LinkedStackClass
while (!intStack.isEmptyStack()) {
System.out.print(intStack.peek() + " ");
tempStack.push(intStack.peek());
intStack.pop();
}
while (!tempStack.isEmptyStack()) {
intStack.push(tempStack.peek());
tempStack.pop();
}
}
public static int countItems(LinkedStackClass
LinkedStackClass
int count = 0;
while (!intStack.isEmptyStack()){
tempStack.push(intStack.peek());
intStack.pop();
count++;
}
while (!tempStack.isEmptyStack()) {
intStack.push(tempStack.peek());
tempStack.pop();
}
return count;
}
public static Integer getSecond(LinkedStackClass
int temp, value;
if (intStack.isEmptyStack()) {
return null;
} else {
temp = intStack.peek();
intStack.pop();
value = intStack.peek();
intStack.push(temp);
}
return value;
}
public static void removeItem(LinkedStackClass
LinkedStackClass
while (!intStack.isEmptyStack()) {
if (intStack.peek()!= n) {
tempStack.push(intStack.peek());
intStack.pop();
} else {
intStack.pop();
}
}
while (!tempStack.isEmptyStack()) {
intStack.push(tempStack.peek());
tempStack.pop();
}
}
public static void reverseStack(LinkedStackClass
QueueClass
while (!s.isEmptyStack()) {
q.enqueue(s.peek());
s.pop();
}
while (!q.isEmptyQueue()) {
s.push(q.front());
q.dequeue();
}
}
public static void reverseQueue(QueueClass
LinkedStackClass
while (!q.isEmptyQueue()) {
s.push(q.front());
q.dequeue();
}
while (!s.isEmptyStack()) {
q.enqueue(s.peek());
s.pop();
}
}
public static void printQueue(QueueClass
QueueClass
while (!q.isEmptyQueue()) {
System.out.print( q.front() + " ");
tempQueue.enqueue(q.front());
q.dequeue();
}
while (!tempQueue.isEmptyQueue()) {
q.enqueue(tempQueue.front());
tempQueue.dequeue();
}
}
public static int[] getInt(Scanner sc, String prompt) {
System.out.println(prompt);
int[] intArray = new int[999];
int i = 0;
while (true) {
if(!sc.hasNextInt()) {
sc.next();
System.out.print("Not an integer! try again: ");
} else {
int input = sc.nextInt();
if (input == 999) {
break;
}
intArray[i] = input;
i++;
}
}
int [] trimmedArray = new int[i];
System.arraycopy(intArray, 0, trimmedArray, 0, i);
return trimmedArray;
}
public static int getNum(Scanner sc, String prompt) {
System.out.println(prompt);
while (!sc.hasNextInt()){
sc.next();
System.out.print("Not a valid integer");
}
return sc.nextInt();
}
}