Create a CircularArrayQueue<E> implementation of the Queue<E> interface defined in class.
Hints:
Note: one new method appears below which was not discussed in class. The clear method should clear the queue and return nothing.
public interface Queue<E> {
/**
* Adds an element to the queue. Return false
* if the element cannot be added.
* @param value - the value to add
* @return - true if the element was added, false otherwise
*/
public boolean add(E value);
/**
* Removes a value from the front of the queue
* @return - the removed value
*/
public E remove();
/**
* Returns the value at the front of the queue
* without removing it
* @return - the value at the front of the queue
*/
public E peek();
/**
* Returns true if this list contains no elements.
* @return true if this list contains no elements
*/
public boolean isEmpty();
/**
* Clear the queue
*/
public void clear();
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// CircularArrayQueue.java
public class CircularArrayQueue<E> implements Queue<E> {
// underlying array
private E[] queue;
// index of front and back elements
private int front, back;
// for avoiding type cast warnings, we use @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked")
public CircularArrayQueue() {
// creating an array with default capacity 10
queue = (E[]) new Object[10];
// setting 0 as front and rear index
front = back = 0;
}
@Override
public boolean add(E value) {
// queue is full when (back + 1) % queue.length returns front index
if ((back + 1) % queue.length == front) {
// in that case, doubling queue size
resize(queue.length * 2);
}
// adding value to index back
queue[back] = value;
// updating back index, wrapping around from the beginning if necessary
back = (back + 1) % queue.length;
return true;
}
@Override
public E remove() {
if (isEmpty()) {
// empty
return null;
}
// taking element at front
E elem = queue[front];
// updating front index, wrapping around from the beginning if necessary
front = (front + 1) % queue.length;
return elem; // returning removed element
}
@Override
public E peek() {
if (isEmpty()) {
// empty
return null;
}
// element at front index
return queue[front];
}
@Override
public boolean isEmpty() {
// returns true if front and back are same
return front == back;
}
@Override
public void clear() {
// simply setting front and back to 0
front = back = 0;
}
// private helper method to resize the array when full
@SuppressWarnings("unchecked")
private void resize(int newCap) {
// creating a new array
E[] newArr = (E[]) new Object[newCap];
int i = front;
int index = 0;
// copying elements from old array to new (loops until i and back are
// same)
while (i != back) {
newArr[index] = queue[i];
i = (i + 1) % queue.length;
index++;
}
// replacing old array with new, updating front and back indices
queue = newArr;
front = 0;
back = index;
}
}
Create a CircularArrayQueue<E> implementation of the Queue<E> interface defined in class. Hints: You will need two...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...
Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return an iterator over...
Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...
Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...
Create a Java code that includes all the methods from the
Lecture slides following the ADTs
LECTURE SLIDES
Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....
QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...
Develop a generic stack ADT class by once again implementing the StackInterface.java and use Queue object as underlying ADT(implement stack using queue) Include two constructors: one that sets the capacity at 100 elements, and the other that allows the application level to provide a capacity value as an actual parameter with the constructor call. Also include a toString method. StackInterface : package interfaces; public interface StackInterface<E> { void push(E element); // add an element to the stack - always at...