Answer To: See zip file for instructions
Abhishek answered on Apr 07 2021
53594 -Queue DS/Screenshots/O3.png
53594 -Queue DS/Screenshots/O1.png
53594 -Queue DS/Screenshots/O4.png
53594 -Queue DS/Screenshots/O2.png
53594 -Queue DS/Screenshots/O5.png
53594 -Queue DS/Screenshots/O6.png
53594 -Queue DS/Solution/Simple Code FIles/RingQueueDriver.java
53594 -Queue DS/Solution/Simple Code FIles/RingQueueDriver.java
import java.util.Arrays;
import java.util.Random;
public class RingQueueDriver {
public static void main(String[] args) {
RingArrayQueue queue1 = new RingArrayQueue();
queue1.add(new Ring("Halo", 2));
queue1.add(new Ring("Vintage", 5));
queue1.add(new Ring("Unique", 3));
queue1.add(new Ring("Braided", 1));
queue1.add(new Ring("Red", 6));
queue1.add(new Ring("Green", 8));
queue1.add(new Ring("Blue", 4));
queue1.add(new Ring("Yellow", 3));
queue1.add(new Ring("White", 9));
queue1.add(new Ring("Red", 10));
System.out.println("Element stored at top of stack is: "+queue1.peek());
System.out.println("\nRemove Method Removed: "+queue1.remove());
System.out.println("Added a new Ring.");
queue1.add(new Ring("Halo", 2));
System.out.println(queue1);
for(int i=0;i<10;i++) {
System.out.println("Element stored at top of stack is: "+queue1.peek());
System.out.println("\nRemove Removed: "+queue1.remove());
System.out.println("Queue Size: "+queue1.size());
System.out.println("Return 'True' or False if Ring Stack is empty: "+queue1.isEmpty());
}
testSwapMethod();
testReplaceMethod();
testSearchQueueMethod();
testEvenElementsMethod();
testSplitMethod();
testMaxValInStackMethod();
}
/**
* Tests the functionality of maxValInStack method
*/
private static void testMaxValInStackMethod() {
System.out.println("\nTesting maxValInStack method==> ");
IntStackInterface stack1 = new IntArrayStack();
IntStackInterface stack2 = new IntLinkedStack();
Random r = new Random();
for(int i= 0;i<5;i++) {
stack1.push(r.nextInt(100)+1);
stack2.push(r.nextInt(100)+1);
}
System.out.print("Stack1 before call to maxValInStack: "+stack1);
System.out.println("maxValInStack returned: "+maxValInStack(stack1));
System.out.println("Stack1 after call to maxValInStack: "+stack1);
System.out.print("\nStack2 before call to maxValInStack: "+stack2);
System.out.println("maxValInStack returned: "+maxValInStack(stack2));
System.out.println("Stack2 after call to maxValInStack: "+stack2);
}
/**
* Tests the functionality of split method
*/
private static void testSplitMethod() {
System.out.println("\nTesting split method==> ");
RingArrayQueue queue = getTempQueue();
queue.add(new Ring("Yellow",0));
System.out.println("Initial Queue: "+queue);
/**
* Queue has both even and odd elements
*/
RingArrayQueue[] result = split(queue);
System.out.println("Call to split Returned: "+Arrays.toString(result));
System.out.println("Queue: "+queue);
/**
* Queue has only even elements
*/
RingArrayQueue[] temp = split(result[0]);
System.out.println("Call to split Returned: "+Arrays.toString(temp));
System.out.println("Queue: "+result[0]);
/**
* Queue has only odd elements
*/
temp = split(result[1]);
System.out.println("Call to split Returned: "+Arrays.toString(temp));
System.out.println("Queue: "+result[1]);
/**
* Queue is empty
*/
temp = split(result[1]);
System.out.println("Call to split Returned: "+Arrays.toString(temp));
System.out.println("Queue: "+result[1]);
}
/**
* Tests the functionality of evenElements method
*/
private static void testEvenElementsMethod() {
System.out.println("\nTesting evenElements method==> ");
RingArrayQueue queue = getTempQueue();
queue.add(new Ring("Yellow",0));
System.out.println("Initial Queue: "+queue);
/**
* Queue has both even and odd elements
*/
RingArrayQueue evenQueue = evenElements(queue);
System.out.print("Call to evenElements Returned: "+evenQueue);
System.out.println("Odd Queue: "+queue);
/**
* Queue has only even elements
*/
RingArrayQueue temp = evenElements(evenQueue);
System.out.print("Call to evenElements Returned: "+temp);
System.out.println("Odd Queue: "+evenQueue);
/**
* Queue has only odd elements
*/
temp = evenElements(queue);
System.out.print("Call to evenElements Returned: "+temp);
System.out.println("Odd Queue: "+queue);
/**
* Queue is empty
*/
queue = evenElements(temp);
System.out.print("Call to evenElements Returned: "+queue);
System.out.println("Odd Queue: "+temp);
}
/**
* Tests the functionality of searchQueue method
*/
private static int testSearchQueueMethod() {
System.out.println("\nTesting searchQueue method==> ");
RingArrayQueue queue = getTempQueue();
System.out.println("Initial Queue: "+queue);
/**
* Item is in middle of queue
*/
System.out.println("searchQueue with 'Blue' returned => "+searchQueue(queue, "Blue"));
/**
* Item is in front of queue
*/
System.out.println("searchQueue with 'Green' returned => "+searchQueue(queue, "Green"));
/**
* Item is in rear of queue
*/
System.out.println("searchQueue with 'Black' returned => "+searchQueue(queue, "Black"));
/**
* Item is in rear of queue
*/
System.out.println("searchQueue with 'Purple' returned => "+searchQueue(queue, "Purple"));
/**
* Empty Queue
*/
queue = new RingArrayQueue();
System.out.println("searchQueue with 'Purple' returned => "+searchQueue(queue, "Purple"));
return 0;
}
/**
* Tests the functionality of swap method
*/
public static void testSwapMethod() {
RingArrayQueue queue = getTempQueue();
RingLinkedStack stack = getTempStack();
System.out.print("Test Stack: "+stack);
System.out.println("Test Queue: "+queue);
swap(stack, queue);
System.out.print("Stack after call to swap: "+stack);
System.out.println("Queue after call to swap: "+queue);
}
/**
* Tests the functionality of replace method
*/
public static void testReplaceMethod() {
System.out.println("Testing Replace method==> ");
RingArrayQueue queue = getTempQueue();
/**
* One old Val where oldVal is the first
*/
replace(queue, new Ring("Green", 5), new Ring("Purple", 5));
System.out.println("Resultant Queue: "+queue);
queue.add(new Ring("Purple", 5));
/**
* Queue has multiple oldVals
*/
replace(queue, new Ring("Purple", 5), new Ring("Orange", 5));
System.out.println("Resultant Queue: "+queue);
/**
* oldVal is the last value
*/
queue.add(new Ring("Silver", 5));
replace(queue, new Ring("Silver", 5), new Ring("Gold", 5));
System.out.println("Resultant Queue: "+queue);
/**
* No oldVal
*/
replace(queue, new Ring("Test", 5), new Ring("Purple", 5));
System.out.println("Resultant Queue: "+queue);
/**
* Empty Queue
*/
queue = new RingArrayQueue();
replace(queue, new Ring("Test", 5), new Ring("Purple", 5));
System.out.println("Final Queue "+queue);
}
/**
* Search the passed queue for the passed searchKey
* @param queue the queue to search
* @param searchKey the search key
* @return the integer index starting from the front
*/
public static Integer searchQueue(RingArrayQueue queue, String searchKey) {
Integer index = null;
int curr=1;
RingArrayQueue tQ = new RingArrayQueue();
/**
* GO hrough each value in the queue
*/
while(!queue.isEmpty()) {
Ring ring = queue.remove();
/**
* This is the required key
* get the index
*/
if(ring.getRing().equals(searchKey) && index == null) {
index = curr;
}
// add to temp queue
tQ.add(ring);
curr++;
}
/**
* Add all elements from temporary queue back to queue
*/
while(!tQ.isEmpty()) {
Ring ring = tQ.remove();
queue.add(ring);
}
return index;
}
/**
* Returns a temporary stack to the calling method
* @return a temporary stack to the calling method
*/
private static RingLinkedStack getTempStack() {
RingLinkedStack linkedStack = new RingLinkedStack();
linkedStack.push(new Ring("White", 4));
linkedStack.push(new Ring("Green", 3));
linkedStack.push(new Ring("Blue", 2));
linkedStack.push(new Ring("Red", 1));
return linkedStack;
}
/**
* Returns a temporary empty stack to the calling method
* @return a temporary empty stack to the calling method
*/
private static RingArrayQueue getTempQueue() {
RingArrayQueue queue = new RingArrayQueue();
queue.add(new Ring("Green", 5));
queue.add(new Ring("Blue", 6));
queue.add(new Ring("Red", 7));
queue.add(new Ring("Black", 8));
return queue;
}
/**
* Swap the elements of the stack and the queue
* @param stack the stack object
* @param queue the queue object
*/
public static void swap(RingLinkedStack stack, RingArrayQueue queue) {
RingArrayQueue tempQueue1 = new RingArrayQueue();
// add all elements to tempQueueq
while(!queue.isEmpty()) {
tempQueue1.add(queue.remove());
}
/**
* Add all stack elements to queue
*/
while(!stack.isEmpty()) {
queue.add(stack.pop());
}
// add all tempQueue1 elements to stack
while(!tempQueue1.isEmpty()) {
stack.push(tempQueue1.remove());
}
}
/**
* Replace the old value from queue with the new value
* @param queue the queue object
* @param oldVal the old value to be replaced
* @param newVal the new value
*/
public static void replace(RingArrayQueue queue, Ring oldVal, Ring newVal) {
RingArrayQueue tQ = new RingArrayQueue();
/**
* Remove all queue elements and add to tQ
*/
while(!queue.isEmpty()) {
Ring ring = queue.remove();
// this is the old value, add new value instead
if(ring.equals(oldVal))
tQ.add(newVal);
else
tQ.add(ring);
}
// add all elements from tQ to queue
while(!tQ.isEmpty()) {
Ring ring = tQ.remove();
queue.add(ring);
}
}
/**
* fetch the even elements from the queue
* @param queue the input queue object
* @return the queue of even elements
*/
public static RingArrayQueue evenElements(RingArrayQueue queue) {
RingArrayQueue tQ = new RingArrayQueue();
RingArrayQueue evenQueue = new RingArrayQueue();
// traverse the whole queue
while(!queue.isEmpty()) {
Ring ring = queue.remove();
//this is an even element
if(ring.getSize() %2 == 0)
evenQueue.add(ring);
else
tQ.add(ring);
}
// add all elements from to to queue
while(!tQ.isEmpty()) {
Ring ring = tQ.remove();
queue.add(ring);
}
return evenQueue;
}
/**
* fetch the queue of even and odd elements from the queue as an array
* where Index 0 will contain a queue of even elements
* and Index 1 will contain a queue of odd elements
* @param queue the input queue object
* @return the queue of even and odd elements from the queue as an array
*/
public static RingArrayQueue[] split(RingArrayQueue queue) {
RingArrayQueue[] array = new RingArrayQueue[2];
RingArrayQueue oddQueue = new RingArrayQueue();
RingArrayQueue evenQueue = new RingArrayQueue();
// go through all elements
while(!queue.isEmpty()) {
Ring ring = queue.remove();
// this is an even element
if(ring.getSize() %2 == 0)
evenQueue.add(ring);
else
oddQueue.add(ring);
}
// add the two queues to the array
array[0] = evenQueue;
array[1] = oddQueue;
return array;
}
/**
* Returns as output the maximum value that is stored in the stack
* @param stack the input stack
* @return max value in stack
*/
public static Integer maxValInStack(IntStackInterface stack) {
Integer max = null;
IntArrayStack st = new IntArrayStack();
while(!stack.isEmpty()) {
int val = stack.pop();
if(max == null || max.intValue() < val)
max= val;
st.push(val);
}
while(!st.isEmpty()) {
int val = st.pop();
stack.push(val);
}
return max;
}
}
53594 -Queue DS/Solution/Simple Code FIles/RingNode.java
53594 -Queue DS/Solution/Simple Code FIles/RingNode.java
public class RingNode {
private Ring data;
private RingNode link ;
public RingNode(Ring data, RingNode link) {
super();
this.data = data;
this.link = link;
}
public Ring getData() {
return data;
}
public void setData(Ring data) {
this.data = data;
}
public RingNode getLink() {
return link;
}
public void setLink(RingNode link) {
this.link = link;
}
public void addNodeAfter(Ring element){
this.link = new RingNode(element,this.link);
}
public void removeNodeAfter(){
this.link = this.link.link;
}
public static int listLength(RingNode head){
RingNode cursor = head;
int answer = 0;
while (cursor != null){
answer++;
cursor = cursor.link;
}
return answer;
}
public static RingNode listSearch(RingNode head, Ring target){
RingNode cursor = head;
while (cursor != null){
if (cursor.getData().equals(target))
return cursor;
cursor = cursor.getLink();
}
return null;
}
public static RingNode listPosition(RingNode head, int position){
RingNode cursor = head;
int index = 1;
while (cursor != null && index < position){
index++;
cursor = cursor.getLink();
}
return cursor;
}
public static void display(RingNode list){
RingNode cursor = list;
while (cursor != null){
System.out.println(cursor.getData());
cursor = cursor.getLink();
}
}
}
53594 -Queue DS/Solution/Simple Code FIles/RingLinkedStack.java
53594 -Queue DS/Solution/Simple Code FIles/RingLinkedStack.java
import java.util.EmptyStackException;
public class RingLinkedStack
{
private RingNode top;
private int numItems;
/**
* Initialize an empty stack.
**/
public RingLinkedStack( ){
top = null;
numItems = 0;
}
/**
* Determine whether this stack is empty.
* @return true if this stack is empty and false otherwise.
**/
public boolean isEmpty( ){
return (top == null);
}
/**
* @return the top item of the stack without removing it
* @exception EmptyStackException Indicates that this stack is empty.
**/
public Ring peek( ) {
if (top == null)
// EmptyStackException is from java.util and its constructor has no argument.
throw new EmptyStackException( );
return top.getData( );
}
/**
* Get the top item, removing it from this stack.
* @return The return value is the top item of this stack, and the item is removed.
* @exception EmptyStackException Indicates that this stack is empty.
**/
public Ring pop( ) {
Ring answer;
if (this.top == null)
// EmptyStackException is from java.util and its constructor has no argument.
throw new EmptyStackException( );
answer = this.top.getData( );
this.top = this.top.getLink( );
numItems --;
return answer;
}
/**
* Push a new item onto this stack.
* @param item - the item to be pushed onto this stack
**/
public void push(Ring item){
this.top = new RingNode(item, this.top);
numItems++;
}
/**
* Accessor method to determine the number of items in this stack.
* @return the number of items in this stack
**/
public int size( ) {
return numItems;
}
/** convert the stack to a printable string
* @return a string representing the stack
*/
public String toString() {
String output = "[";
RingNode cursor = top;
while (cursor != null){
output += cursor.getData()+"\t";
cursor = cursor.getLink();
}
output += "] \n";
return output;
}
}
53594 -Queue DS/Solution/Simple Code FIles/RingArrayQueue.java
53594 -Queue DS/Solution/Simple Code FIles/RingArrayQueue.java
import java.util.NoSuchElementException;
public class RingArrayQueue {
private Ring[] rings;
private int front;
private int rear;
private int elements;
public RingArrayQueue() {
this.rings = new Ring[10];
this.front = 0;
this.rear = 0;
this.elements = 0;
}
public RingArrayQueue(int capacity) {
this.rings = new Ring[capacity];
this.front = 0;
this.rear = 0;
this.elements = 0;
}
public boolean isEmpty() {
return this.elements == 0;
}
public Ring peek() {
if(isEmpty())
throw new NoSuchElementException();
return rings[front];
}
public Ring remove() {
if(isEmpty())
throw new NoSuchElementException();
Ring ring = rings[front];
front = (front + 1) % rings.length;
elements--;
return ring;
}
public void add(Ring ring) {
if(elements == rings.length)
return;
rings[rear] = ring;
rear = (rear + 1) % rings.length;
elements++;
}
@Override
public String toString() {
String output = "[";
for(int i = 0;i< elements;i++) {
int index = (front + i)% rings.length;
output+= rings[index]+"\t";
}
output += "]\n";
return output;
}
public int size() {
return this.elements;
}
}
53594 -Queue DS/Solution/Simple Code FIles/Ring.java
53594 -Queue DS/Solution/Simple Code FIles/Ring.java
public class Ring implements Comparable {
/**
* instance variables
*/
private String ring;
private int size;
/**
* Contrustor
* @param ring
* @param size
*/
public Ring(String ring, int size) {
this.ring = ring;
this.size = size;
}
/**
* Getters and Setters
* @return
*/
public String getRing() {
return this.ring;
}
public void setRing(String ring) {
this.ring = ring;
}
public int getSize() {
return this.size;
}
public void setSize(int size) {
this.size = size;
}
/**
* returns a string represent a ring
*/
@Override
public String toString() {
String output = "";
output += "Ring type-" + "["+this.ring +": ";
output += this.size +"]";
return output;
}
/**
* equals method to
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ring other = (Ring) obj;
if (ring == null) {
if (other.ring != null)
return false;
} else if (!ring.equalsIgnoreCase(other.ring))
return false;
if (size != other.size)
return false;
return true;
}
/**
*
*/
@Override
public int compareTo(Ring otherRing) {
int output = 0;
if (this.ring.equalsIgnoreCase(otherRing.ring)) {
if (this.size > otherRing.size)
output = 1;
else if(this.size < otherRing.size) {
output = -1;
}
else {
output = 0;
}
}
else if(this.ring.toLowerCase().compareTo(otherRing.ring.toLowerCase())<0){
output = -1;
}
else {
output = 1;
}
return output;
}
}
53594 -Queue DS/Solution/Simple Code FIles/IntStackInterface.java
53594 -Queue DS/Solution/Simple Code FIles/IntStackInterface.java
import java.util.EmptyStackException;
public interface IntStackInterface {
/**Determine whether this stack is empty.
* @return true if this stack is empty and false otherwise.
*/
public boolean isEmpty( );
/**@return the top item of the stack without removing it
* @exception EmptyStackException Indicates that this stack is empty.
**/
public int peek( );
/** Get the top item, removing it from this stack.
* @return The return value is the top item of this stack, and the item is removed.
* @exception EmptyStackException Indicates that this stack is empty.
**/
public int pop( );
/**Push a new item onto this stack.
* @param item - the item to be pushed onto this stack
**/
public void push(int item);
/**
* Accessor method to determine the number of items in this stack.
* @return the number of items in this stack
* **/
public int size( );
/** convert the stack to a printable string
* @return a string representing the stack
**/
public String toString();
}
53594 -Queue DS/Solution/Simple Code FIles/IntNode.java
53594 -Queue DS/Solution/Simple Code FIles/IntNode.java
public class IntNode {
private int data;
private IntNode link;
public IntNode(int initialData, IntNode initialLink){
this.data = initialData;
this.link = initialLink;
}
public int getData() { return this.data;}
public void setData(int data) { this.data = data;}
public IntNode getLink() {return this.link; }
public void setLink(IntNode link) { this.link = link; }
public void display(){
IntNode cursor = this;
while (cursor != null){
System.out.println(cursor.getData());
cursor = cursor.getLink();
}
}
public static void display(IntNode list){
IntNode cursor = list;
while (cursor != null){
System.out.println(cursor.getData());
cursor = cursor.getLink();
}
}
public static void displayReversed(IntNode head){
int size = IntNode.listLength(head);
int[] temp = new int[size];
int pos = 0;
IntNode cursor = head;
while (cursor != null){
temp[pos]= cursor.getData();
pos++;
cursor = cursor.getLink();
}
for (int i=size-1; i>=0; i--)
System.out.println(temp[i]);
}
public static IntNode getReversedList(IntNode head){
int size = IntNode.listLength(head);
int[] temp = new int[size];
int pos = 0;
IntNode cursor = head;
while (cursor != null){
temp[pos]= cursor.getData();
pos++;
cursor = cursor.getLink();
}
IntNode output = new IntNode(temp[0],null);
for (int i=1; i < size; i++)
output = new IntNode(temp[i],output);
return output;
}
public static int countOccurances(IntNode list, int target){
int count = 0;
IntNode cursor = list;
while (cursor != null){
if (cursor.getData() == target)
count++;
cursor = cursor.getLink();
}
return count;
}
public void addNodeAfter(int element){
this.link = new IntNode(element,this.link);
}
public void removeNodeAfter(){
this.link = this.link.link;
}
public static int listLength(IntNode head){
IntNode cursor = head;
int answer = 0;
while (cursor != null){
answer++;
cursor = cursor.link;
}
return answer;
}
public static IntNode listSearch(IntNode head, int target){
IntNode cursor = head;
while (cursor != null){
if (cursor.getData() == target)
return cursor;
cursor = cursor.getLink();
}
return null;
}
public static IntNode listPosition(IntNode head, int position){
IntNode cursor = head;
int index = 1;
while (cursor != null && index < position){
index++;
cursor = cursor.getLink();
}
return cursor;
}
public static IntNode listCopy(IntNode source){
IntNode copyHead;
IntNode copyTail;
IntNode cursor = source;
if (source == null)
return null;
//make the first node in the new list
copyHead = new IntNode(source.getData(),null);
copyTail = copyHead;
while (cursor !=null){
cursor = cursor.getLink();
copyTail.addNodeAfter(cursor.getData());
copyTail = copyTail.link;
}
return copyHead;
}
public static void addTail(IntNode head, int val){
IntNode cursor = head;
while (cursor.getLink() != null)
cursor = cursor.getLink();
cursor.addNodeAfter(val);
}
/*
public static void insertkth(IntNode head, int k, int val){
IntNode cursor = head;
for (int i= 1; i < k-1; i++)
cursor = cursor.getLink();
cursor.addNodeAfter(val);
}
*/
public static void insertkth(IntNode head, int k, int val){
IntNode cursor = IntNode.listPosition(head, k-1);
cursor.addNodeAfter(val);
}
}
53594 -Queue DS/Solution/Simple Code FIles/IntLinkedStack.java
53594 -Queue DS/Solution/Simple Code...