Instructions
Objective
Write a java assignment program to implement stacks and queues.
Requirements and Specifications
Source Code
QUEUE
public class ArrayQueue implements QueueInterface {
private int[] elements;
private int numElements;
/**
* Initialize the queue with an initial capacity
*/
public ArrayQueue() {
elements = new int[10];
numElements = 0;
}
/**
* Copy constructor.
*/
public ArrayQueue(ArrayQueue other) {
for (int i = 0; i < other.numElements; i++) {
enqueue(other.elements[i]);
}
}
/**
* Add an element to the back of the queue
*/
@Override
public void enqueue(int value) {
if (numElements >= elements.length) {
// Expand the array if out of space
int[] temp = new int[elements.length * 2];
for (int i = 0; i < numElements; i++) {
temp[i] = elements[i];
}
elements = temp;
}
elements[numElements++] = value;
}
/**
* Remove and return the first element of the queue.
*/
@Override
public int dequeue() {
if (numElements == 0) {
return -1;
}
int element = elements[0];
for (int i = 0; i < numElements - 1; i++) {
elements[i] = elements[i + 1];
}
numElements--;
return element;
}
/**
* Return the first element of the queue.
*/
@Override
public int peek() {
if (numElements == 0) {
return -1;
}
return elements[0];
}
/**
* Check if queue is empty.
*/
@Override
public boolean isEmpty() {
return numElements == 0;
}
/**
* Return the number of elements.
*/
@Override
public int size() {
return numElements;
}
/**
* Remove all elements.
*/
@Override
public void clear() {
numElements = 0;
}
/**
* Return a string representation.
*/
@Override
public String toString() {
String str = "{ ";
for (int i = 0; i < numElements; i++) {
str += elements[i] + ", ";
}
return str + "}";
}
/**
* Check if this queue is the same as the other.
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof QueueInterface)) {
return false;
}
QueueInterface otherQueue = (QueueInterface) other;
return toString().equals(otherQueue.toString());
}
}
STACK
public class ArrayStack implements StackInterface {
private int[] elements;
private int numElements;
/**
* Initialize an array stack.
*/
public ArrayStack() {
elements = new int[10];
numElements = 0;
}
/**
* Copy constructor.
*/
public ArrayStack(ArrayStack other) {
for (int i = 0; i < other.numElements; i++) {
push(other.elements[i]);
}
}
/**
* Add an element to the top of the stack.
*/
@Override
public void push(int value) {
if (numElements >= elements.length) {
int[] temp = new int[elements.length];
for (int i = 0; i < numElements; i++) {
temp[i] = elements[i];
}
elements = temp;
}
elements[numElements++] = value;
}
/**
* Remove and return the top element in the stack.
*/
@Override
public int pop() {
if (numElements == 0) {
return -1;
}
int element = elements[numElements - 1];
numElements--;
return element;
}
/**
* Return the top element in the stack.
*/
@Override
public int peek() {
if (numElements == 0) {
return -1;
}
return elements[numElements - 1];
}
/**
* Return true if the stack has no element.
*/
@Override
public boolean isEmpty() {
return numElements == 0;
}
/**
* Returns the number of elements in the stack.
*/
@Override
public int size() {
return numElements;
}
/**
* Remove all elements in the stack.
*/
@Override
public void clear() {
numElements = 0;
}
/**
* Return a string representation.
*/
@Override
public String toString() {
String str = "{ ";
for (int i = 0; i < numElements; i++) {
str += elements[i] + ", ";
}
return str + "}";
}
/**
* Check if this queue is the same as the other.
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof StackInterface)) {
return false;
}
StackInterface otherStack = (StackInterface) other;
return toString().equals(otherStack.toString());
}
}
Similar Samples
Explore our sample programming homework solutions to get a glimpse of our expertise. At ProgrammingHomeworkHelp.com, we deliver high-quality, tailored solutions for a variety of programming assignments. Our samples showcase our commitment to accuracy, efficiency, and clear documentation, ensuring you receive top-notch assistance for your academic success.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java