8#A splice method combines two queues by adding all elements of a second queue t
ID: 3759311 • Letter: 8
Question
8#A splice method combines two queues by adding all elements of a second queue to the end of a first queue.
Write a splice JAVA method from the implementation perspective. This method will go inside of the LinkedQueue class.
The header should read: public void splice(LinkedQueue secondQueue)
#The secondQueue should not be altered when the method completes.
(For full credit, we must take advantage of being able to directly access the linked node chain of secondQueue. In other words, for an efficient solution, do not destroy and rebuild secondQueue.)
*Note that it is okay to invoke the O(1) methods in the current LinkedQueue class!*
**This is the driver***
public class HomeworkW11Driver {
public static void main(String[] args) {
// Q5 palindromes
System.out.println("Q7");
String p = "anna";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "ana";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "a";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "amanaplanacanalpanama";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "ababb";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "ab";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
System.out.println();
// Q6 splice client
QueueInterface<Integer> q1 = new LinkedQueue<Integer>();
q1.enqueue(4);
q1.enqueue(8);
q1.enqueue(9);
q1.enqueue(2);
QueueInterface<Integer> q2 = new LinkedQueue<Integer>();
q2.enqueue(1);
q2.enqueue(3);
q2.enqueue(7);
q2.enqueue(6);
splice(q1, q2);
System.out.println("q1 contains: 4 8 9 2 1 3 7 6");
((LinkedQueue)q1).display();
System.out.println("q2 still contains: 1 3 7 6");
((LinkedQueue)q2).display();
// Q7 splice ArrayQueue
ArrayQueue<Integer> aq1 = new ArrayQueue<Integer>();
aq1.enqueue(4);
aq1.enqueue(8);
aq1.enqueue(9);
aq1.enqueue(2);
ArrayQueue<Integer> aq2 = new ArrayQueue<Integer>();
aq2.enqueue(1);
aq2.enqueue(3);
aq2.enqueue(7);
aq2.enqueue(6);
aq1.splice(aq2);
System.out.println("q1 contains: 4 8 9 2 1 3 7 6");
aq1.display();
System.out.println("q2 still contains: 1 3 7 6");
aq2.display();
// Q8 splice LinkedQueue
LinkedQueue<Integer> lq1 = new LinkedQueue<Integer>();
lq1.enqueue(4);
lq1.enqueue(8);
lq1.enqueue(9);
lq1.enqueue(2);
LinkedQueue<Integer> lq2 = new LinkedQueue<Integer>();
lq2.enqueue(1);
lq2.enqueue(3);
lq2.enqueue(7);
lq2.enqueue(6);
lq1.splice(lq2);
System.out.println("q1 contains: 4 8 9 2 1 3 7 6");
lq1.display();
System.out.println("q2 still contains: 1 3 7 6");
lq2.display();
}
public static boolean isPalindrome(String s) {
// YOUR CODE HERE!
return false;
}
public static void splice(QueueInterface firstQueue, QueueInterface secondQueue) {
// YOUR CODE HERE!
}
}
Explanation / Answer
CODE :
public static void splice(QueueInterface firstQueue, QueueInterface secondQueue) {
firstQueue = { " Preference to this queue first " };
secondQueue = { "Preference to this to next" };
return firstQueue;
else return secondQueue;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.