×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create a Program to Implement Stacks and Queues in Java Assignment Solution

June 20, 2024
Jessica Miller
Jessica Miller
🇺🇸 United States
Java
Jessica Miller is a seasoned programmer with a master's degree in software engineering from Stanford University. Having completed over 700 Java assignments, Jessica excels in implementing sorting algorithms, multi-dimensional arrays, and efficient array operations. Her deep understanding of complex programming concepts and hands-on experience make her a valuable resource for students seeking high-quality homework help.
Key Topics
  • Instructions
    • Objective
  • Requirements and Specifications
Tip of the day
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

Instructions

Objective

Write a java assignment program to implement stacks and queues.

Requirements and Specifications

program-to-implement-stacks-and-queues-in-java 1 (1)
program-to-implement-stacks-and-queues-in-java 2 (1)

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.