# CS112-Problem Set 3 Solved

30.00 \$ 15.00 \$

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . ` zip` solution files instantly, after Payment

## Description

5/5 - (1 vote)

Problem Set 3

1. *Given the following definition of a circular linked list (CLL) class:

public class Node {Â Â Â Â Â Â Â Â Â  public String data;Â Â Â Â Â Â Â Â Â  public Node next;

public Node(String data, Node next) { Â Â Â Â Â Â Â Â Â Â Â Â Â this.data = data; this.next = next;

}

}

private Node rear;Â  // pointer to last node of CLL

…Â Â Â Â Â Â  }

The class keeps a circular linked list, with a rear pointer to the last node.

Implement the following method in the LinkedList class, to delete the first occurrence of a given item from the linked list. The method returns true if the item is deleted, or false if the item is not found.

public boolean delete(String target) {

/* COMPLETE THIS METHOD */

}

1. * Implement a method in the circular linked list class of problem 1, to add a new item after the first occurrence (from the front) of a specified item. If the item does not exist in the list, the method should return false, otherwise true

public boolean addAfter(String newItem, String afterItem) {

/* COMPLETE THIS METHOD */

}

1. A doubly linked list (DLL) is a linked list with nodes that point both forward and backward. Here’s an example: 3 <—> 5 <—> 7 <—> 1

Here’s a DLL node definition:

public class DLLNode {Â Â Â Â Â Â Â Â Â  public String data;Â Â Â Â Â Â Â Â Â  public DLLNode prev, next;

public DLLNode(String data, DLLNode next, DLLNode prev) {Â Â Â Â Â Â Â Â Â Â Â Â Â  this.data = data; this.next = next; this.prev = prev;

}Â Â Â Â Â Â  }

The next of the last node will be null, and the prev of the first node will be null.

Implement a method to move a node (given a pointer to it) to the front of a DLL.

// moves target to front of DLL

public static DLLNode moveToFront(DLLNode front, DLLNode target) {

/** COMPLETE THIS METHOD **/Â Â Â Â Â Â  }

1. With the same DLLNode definition as in the previous problem, implement a method to reverse the sequence of items in a DLL. Your code should NOT create any new nodes – it should simply resequence the original nodes. The method should return the front of the resulting list.

public static DLLNode reverse(DLLNode front) {

/** COMPLETE THIS METHOD **/Â Â Â Â Â Â  }

1. Implement a RECURSIVE method to delete all occurrences of an item from a (non-circular) linked list. Use the Node class definition of problem 1. Return a pointer to the first node in the updated list.

public static Node deleteAll(Node front, String target) {

/* COMPLETE THIS METHOD */

}

1. * Implement a RECURSIVE method to merge two sorted linked lists into a single sorted linked list WITHOUT duplicates. No new nodes must be created: the nodes in the result list are a subset of the nodes in the original lists, rearranged appropriately.

You may assume that the original lists do not have any duplicate items.

For instance:

l1 = 3->9->12->15Â Â Â  l2 = 2->3->6->12

should result in the following:

2->3->6->9->12->15

Assuming a Node class defined like this:

public class Node {Â Â Â Â Â Â Â Â Â  public int data;Â Â Â Â Â Â Â Â Â  public Node next;

}

Complete the following method:

public static Node merge(Node frontL1, Node frontL2) {

}

• Problem-Set-3-tpacrm.zip