Instructions
Objective
Write a program to work with queues stacks and ADTs in java language.
Requirements and Specifications
Source Code
QUEUE
//ENGR3440 - Data Structures and Algorithms for Engineers
//Spring 2022
//Assignment #3, Queue and Stack ADTs.
//Dominic Reagle
import java.util.NoSuchElementException;
public class myQueueADT<E> {
private static class Node<E> {
private E data;
private Node<E> next;
public Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
}
private Node<E> front;
private Node<E> rear;
private int size;
public myQueueADT() {
front = null;
rear = null;
size = 0;
}
public int Size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void enqueue(E e) {
Node<E> newNode = new Node<E>(e, null);
if (isEmpty()) {
front = newNode;
rear = newNode;
}
else {
rear.next = newNode;
rear = newNode;
}
size++;
}
public E dequeue() {
if (isEmpty()) {
throw new NoSuchElementException();
}
E result = front.data;
front = front.next;
if (front == null) {
rear = null;
}
size--;
return result;
}
}
STACK
//ENGR3440 - Data Structures and Algorithms for Engineers
//Spring 2022
//Assignment #3, Queue and Stack ADTs.
//Dominic Reagle
import java.util.NoSuchElementException;
public class myStackADT<E> {
private static class Node<E> {
private E data;
private Node<E> next;
public Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
}
private Node<E> top;
private int size;
public myStackADT() {
top = null;
size = 0;
}
public int Size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void push(E e) {
Node<E> newNode = new Node<E>(e, top);
top = newNode;
size++;
}
public E pop() {
if (isEmpty()) {
throw new NoSuchElementException();
}
E result = top.data;
top = top.next;
size--;
return result;
}
}