This Has To Be Implemented On Java Array Queue Class: Safari File Edit View Hist
ID: 3823218 • Letter: T
Question
This Has To Be Implemented On Java
Array Queue Class:
Safari File Edit View History Bookmarks Window Help (S1% Sat 11:47 AM Q https://mybb.qu.edu.qa/bboswebdav/pid-2371962-dt-content-rid-3145001-2/courses/Spring 2017 CMPS303 21853/HW2.pdf https mybb.qu.edu.qa Reade bbcswebdav/pid-2371962 853/ HNW2.pdf 45001 /courses/Spring 2017 CMPS303 Conten EEE Dropbox C...s Materials Popular Apple Yahoo! Google Maps YouTube Wikipedia News rid-3145001 2/courses/Spring 2017 CM... Assignments algul (Spring 2017 CMPs303 21853 L01 HW 2: Stacks and Queues) Q1) Create class ArrayDegue to implement double ended queue (Deque) Gmodify ueue). De que has the following methods: is Empty0, size0, first0, last0, addFirst0, addLast0, removeFirst0, removeLast0. C10ptsl Test these methods in main method. Q2) Create a java class called DoubleStack to represent a double stack data structure (see figure below). It is like two stacks shared the same array, one from left side and the other one from right side. However, this doesn't mean the array is divided equally between them. It depends on push and pop operations for each one of them. DoubleStack will have SEmptyl, size10, push 1, topl, popl, isEmpty2, size20, push2, top2, and pop2 Write a main0 function to test all the above functionalities. stks: 49 57 3 44 97 23 17 topStk 1 2 topStk2 36 10pts In the main, create an object of DoubleStack, test the methods.Explanation / Answer
import java.util.Scanner;
class ArrayDeque
{
private int[] a;
private int j, n;
public ArrayDeque()
{
j = 0;
n = 0;
resize();
}
public boolean isEmpty()
{
return n == 0;
}
public void clear()
{
j = 0;
n = 0;
resize();
}
public int getSize()
{
return n;
}
private void resize()
{
int[] temp = new int[Math.max(2 * n, 1)];
for (int k = 0; k < n; k++)
temp[k] = a[(j + k) % a.length];
a = temp;
j = 0;
}
public int get(int i)
{
return a[(j + i) % a.length];
}
public int set(int i, int x)
{
int y = a[(j + i) % a.length];
a[(j + i) % a.length] = x;
return y;
}
void add(int i, int x)
{
if (n + 1 > a.length)
resize();
if (i < n/2)
{
j = (j == 0) ? a.length - 1 : j - 1;
for (int k = 0; k <= i - 1; k++)
a[(j + k) % a.length] = a[(j + k + 1)%a.length];
}
else
{
for (int k = n; k > i; k--)
a[(j + k) % a.length] = a[(j + k - 1)%a.length];
}
a[(j + i) % a.length] = x;
n++;
}
public int remove(int i)
{
int x = a[(j + i) % a.length];
if (i < n/2)
{
for (int k = i; k > 0; k--)
a[(j + k) % a.length] = a[(j + k - 1) % a.length];
j = (j + 1) % a.length;
}
else
{
for (int k = i; k < n - 1; k++)
a[(j + k) % a.length] = a[(j + k + 1) % a.length];
}
n--;
if (3 * n < a.length)
resize();
return x;
}
public void display()
{
System.out.print(" Array Deque : ");
int p = j;
for (int i = 0; i < n; i++)
{
System.out.print(a[p % a.length] +" ");
p++;
}
System.out.println();
}
}
public class ArrayDequeTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Array Deque Test ");
ArrayDeque ad = new ArrayDeque();
char ch;
do
{
System.out.println(" Array Deque Operations ");
System.out.println("1. add");
System.out.println("2. get");
System.out.println("3. set");
System.out.println("4. remove");
System.out.println("5. check empty");
System.out.println("6. clear");
System.out.println("7. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter index and element");
ad.add(scan.nextInt(), scan.nextInt() );
break;
case 2 :
System.out.println("Enter index");
System.out.println("Result : "+ ad.get(scan.nextInt() ));
break;
case 3 :
System.out.println("Enter index and element");
ad.set(scan.nextInt(), scan.nextInt() );
break;
case 4 :
System.out.println(" Enter index");
ad.remove(scan.nextInt() );
break;
case 5 :
System.out.println(" Empty Status : "+ ad.isEmpty());
break;
case 6 :
System.out.println(" Array Deque Cleared");
ad.clear();
break;
case 7 :
System.out.println(" Size = "+ ad.getSize() );
break;
default :
System.out.println("Wrong Entry ");
break;
}
ad.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.