Question: Please help me with the assignment below. Really appreciate !!! you will implement find() and duplicate() methods for a linked list class. To keep things

Please help me with the assignment below. Really appreciate !!!

you will implement find() and duplicate() methods for a linked list class. To keep things simple, the list class and its node class arent generic. The data of each node is one char, stored in a CharNode class that is provided. In the Eclipse workspace of your choice, create a new Java project containing package linked and add to it the 3 provided source files. The method requirements are: public CharNode find(char ch) Returns the first (i.e. closest to the head) node in the list whose data is equal to ch. If there is no such node, returns null. public void duplicate(char ch) Finds the first node in the list whose data is equal to ch. Returns if there is no such node. If the node is found, creates a new node containing the same data, and insert that node into the list either immediately before or immediately after the found node. The CharNode starter file contains a hasIntegrity() method that checks some (but not all) aspects of list integrity. Use it (maybe in assert statements) in your find() and duplicate() methods if it will help. This is a win-or-lose assignment. If your code passes the graderbot (class LinkedListTester), you get 100 points. If your code fails any part of the graderbot, you get zero points.

--- CharLinkedList.java:

package linked;

import java.util.*;

public class CharLinkedList

{

private CharNode head; // Empty if head and

private CharNode tail; // tail are null

public CharLinkedList() { }

public CharLinkedList(String s)

{

for (int i=s.length()-1; i>=0; i--)

insertAtHead(s.charAt(i));

}

public void insertAtHead(char ch)

{

assert hasIntegrity(); // Precondition

CharNode node = new CharNode(ch);

node.setNext(head);

head = node;

if (tail == null)

tail = node; // Corner case: inserting into empty node

assert hasIntegrity(); // Postcondition

}

public String toString()

{

String s = "";

CharNode node = head;

while (node != null)

{

s += node.getData();

node = node.getNext();

}

return s;

}

//

// Returns true if this list has emptiness integrity, has tail integrity, has no loops,

// and tail is reachable from head.

//

// Caution: this checks for most but not all common integrity problems.

//

boolean hasIntegrity()

{

// Check emptiness. If either head or tail is null, the other must

// also be null. Different logic from what you saw in lecture. Returns

// immediately if this list is empty.

if (head == null || tail == null)

return head == null && tail == null;

// Check tail integrity (tail.next must be null).

if (tail.getNext() != null)

return false;

// Check for loops.

Set visitedNodes = new HashSet<>();

CharNode node = head;

while (node != null)

{

if (visitedNodes.contains(node))

return false; // Current node has been visited before, we must have a loop

visitedNodes.add(node); // First visit to this node

node = node.getNext();

}

// Make sure tail is reachable from head.

node = head;

while (node != null && node != tail)

node = node.getNext();

return node == tail;

}

static void sop(Object x) { System.out.println(x); }

}

------- CharNode.java:

package linked;

class CharNode

{

private char ch;

private CharNode next;

CharNode(char ch)

{

this.ch = ch;

}

CharNode getNext()

{

return next;

}

void setNext(CharNode next)

{

this.next = next;

}

char getData()

{

return ch;

}

}

------ LinkedListTester.java

package linked;

public class LinkedListTester

{

private static void fail(String message)

{

if (!message.endsWith("."))

message += ".";

System.out.println("... FAIL: " + message);

System.exit(0);

}

public static void main(String[] args)

{

System.out.println("Searching for A in empty list ...");

CharLinkedList list = new CharLinkedList();

if (list.find('A') != null)

fail("Found a node in an empty list.");

String original = "ABCDE";

for (int i=0; i

{

// Find target.

list = new CharLinkedList(original);

char target = original.charAt(i);

System.out.println("Searching for " + target + " in " + original + " ...");

CharNode node = list.find(target);

if (node == null)

fail("Couldn't find it.");

if (node.getData() != target)

fail("Found node containing " + node.getData());

if (!list.hasIntegrity())

fail("Found " + target + " but list has broken integrity");

if (!list.toString().equals(original))

fail("Found " + target + " but list has changed to " + list);

// Duplicate target.

list = new CharLinkedList(original);

System.out.println("Wil duplicate " + target + " in " + original + " ...");

list.duplicate(target);

if (!list.hasIntegrity())

fail("After duplicating, list doesn't have integrity");

String expected = original.substring(0, i+1) + original.substring(i);

if (!list.toString().equals(expected))

fail("After duplicating, list is " + list + ", expected " + expected);

}

// Duplicate non-existent char.

list = new CharLinkedList(original);

System.out.println("Will duplicate X in " + original + " ...");

list.duplicate('X');

if (!list.hasIntegrity())

fail("After duplicating, list doesn't have integrity");

if (!list.toString().equals(original))

fail("List should not have changed, became " + list);

System.out.println(" PASS");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!