Writing a method retainAll for Circular Doubly-Linked List:
I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt.
Initialization:
public class CDoublyLinkedList {
private class Node {
private Object data; //Assume data
implemented Comparable
private Node next, prev;
private Node(Object data, Node
pref, Node next)
{
this.data =
data;
this.prev =
pref;
this.next =
next;
}
}
private Node head;
private int size;
public CDoublyLinkedList() {
this.head = new Node(null, null,
null );
this.head.next = this.head;
this.head.prev=this.head;
this.size = 0;
}
public boolean isEmpty() {
return this.head ==
this.head.next;
}
Method instructions and attempted method:
// Removes from this list all of its elements that
// are NOT contained in the specified linkedlist
other.
// If any element has been removed from this
list,
// returns true. Otherwise returns false.
// If other list is null, throws
NullPointerException.
// Helper methods are allowed.
public boolean retainAll(CDoublyLinkedList other)
throws NullPointerException {
Node cur = this.head.next;
Node cur1 = other.head.next;
if (other == null) {
throw new
NullPointerException("NullPointerException");
}
while (cur!= this.head) {
if (cur.data ==
null) {
cur = cur.next;
cur1 = cur1.next;
}
}
if
(!cur.data.equals(cur1.data)) {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
this.size--;
return true;
}
return
false;
}
Here is the tester class, and the proper output.
public class Tester {
private static CDoublyLinkedList list0;
private static CDoublyLinkedList list2;
private static CDoublyLinkedList list3;
private static void init() {
list0 = new
CDoublyLinkedList();
list2 = new
CDoublyLinkedList();
list3 = new
CDoublyLinkedList();
list2.addFirst("6:Tony");
list2.addFirst("5:Tom");
list2.addFirst("4:Tim");
list2.addFirst("3:Abby");
list2.addFirst("2:Morning");
list2.addFirst("1:Good");
list0.addFirst(null);
list0.addFirst("bad");
list0.addFirst("apple");
list0.addFirst(null);
}
public static void drawLine() {
System.out.println("-----------------------------");
}
public static void testRetainAll() {
init();
System.out.println("---------testRetainAll()---------");
list3.retainAll(list0);
System.out.println(list3);
list3.addFirst("C");
list3.addFirst("C");
list3.addFirst("6:Tony");
list3.addFirst("6:Tony");
list3.retainAll(list2);
System.out.println(list3);
init();
list3.addFirst(null);
list3.addFirst("bad");
list3.addFirst(null);
list3.addFirst("B");
list3.addFirst("A");
list3.retainAll(list0);
System.out.println(list3);
drawLine();
}
---------testRetainAll()---------
{}
{6:Tony->6:Tony}
{null->bad->null}
-----------------------------
Please help, I have been scratching my head about this for several hours now.
public class CDoublyLinkedList { private class Node { private Object data; // Assume data implemented Comparable private Node next, prev; private Node(Object data, Node pref, Node next) { this.data = data; this.prev = pref; this.next = next; } } private Node head; private int size; public CDoublyLinkedList() { this.head = new Node(null, null, null); this.head.next = this.head; this.head.prev = this.head; this.size = 0; } public boolean isEmpty() { return this.head == this.head.next; } public boolean contains(Object data) { Node start = head.next; int len = size; while(len != 0) { if(start.data != null && start.data.equals(data)) { return true; } start = start.next; } return false; } private void removeNode(Node x) { Node prevNode = x.prev; Node nextNode = x.next; prevNode.next = nextNode; nextNode.prev = prevNode; size--; } // Removes from this list all of its elements that // are NOT contained in the specified linkedlist other. // If any element has been removed from this list, // returns true. Otherwise returns false. // If other list is null, throws NullPointerException. // Helper methods are allowed. public boolean retainAll(CDoublyLinkedList other) throws NullPointerException { Node cur = this.head.next; boolean status = false; while(cur != this.head) { Node nextNode = cur.next; if(!other.contains(cur.data)) { removeNode(cur); status = true; } cur = nextNode; } return status; } }
please use above code.. make very simply. Hope it helps.
Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a...
Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...
Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...
I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...
Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...
Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...
Java Given the array, which has been converted to a linked list, I need to invoke a remove method to remove a number from the list. The output for the first array should come out like this Now list1 = 24->18->3-7->null Now list2 = 31->-9->5->21->4->0->8->null Now lsit3 = 5->0->1->null but i keep getting this output Now list1= 12->3->7->null Now list2= -7->null Now list3= 5->0->1->null int arr1[] = {24, 18, 12, 3, 7}; int arr2[] = {31, -9, 5, 21, 4,...
CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three files that implement and use a simple linked list. The code was written in early Java-style using the Object class in order to allow the linked list to be a list of objects of any type. While the code works, it is not type-safe. Refactor the code to use Java Generics. You will need to change the Main class to create a linked list...
Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...
iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...