Write the method checkListEndsSymmetry that receives a double linked list and an
ID: 3861682 • Letter: W
Question
Write the method checkListEndsSymmetry that receives a double linked list and an integer number k. The method checks if the double linked list has identical k elements going forward from the first element and backwards from the last one. The method returns true if they are identical, and false otherwise. The method signature is: public boolean checkListEndsSymmetry (DoubleLinkedListdl, int k) If dl = A doubleheadarrow B doubleheadarrow C doubleheadarrow D doubleheadarrow B doubleheadarrow A and k = 2, then the method should return true. If k = 3, it should return false, since C does not equal D. Write the method bubbleSort that sorts a double linked list of integers given as input using bubble sort. The method signature is: public void bubbleSort(DoubleLinkedList l). Write the recursive method reverse, member of the class LinkedList that reverses the content of the list. Write the recursive method reverse, member of the class DoubleLinkedList that reverses the content of the list.Explanation / Answer
//reversing the content of sinle linked list
void reverse(node l) {
/* empty list */
if (l == null)
return;
/* Last node (tail node)*/
if(l.next == null){
head = l;
return;
}
/* reverse the rest of list and put the first element at the end */
reverse(l.next);
l.next.next = l;
l.next = null;
}
//doubly linked list
void reverse(node l) {
node temp;
/* empty list */
if (l == null)
return;
/* Last node (tail node)*/
if(l.next == null){
l.next=l->prev;
l.prev = null;
head = l;
return;
}
/* reverse the rest of list and put the first element at the end */
reverse(l->next);
temp =l.next;//reversing the list
l.next=l.prev;
l.prev =temp;
}
public void bubbleSort(DoubleLinkedList <Integer>l)
{
DoubleLinkedList<Integer> t1=l,t2=l;
int temp;
//sorting
while(t1!=null)
{
while(t2!=null)
{
if(t1.data>t2.data)
{
temp=t1.data;//swapping
t1.data=t2.data;
t2.data =temp;
}
t2=t2.next;
}
t1=t1.next
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.