Question: JAVA please! *eclipse Use the DLinkedList provided in class. You must not change any of the existing code in this file! You will add your
JAVA please! *eclipse
Use the DLinkedList provided in class. You must not change any of the existing code in this file! You will add your code to this file.
Add the following methods:
A remove() method that removes a single node containing the value passed in as a parameter from the list. Method signature is provided and may not be changed.
An insertOrder() method that inserts a value into the list in such a way that the list remains sorted in ascending order. Method signature is provided and may not be changed.
An insertOrderUnique() method that does the same as insertOrder() with the additional requirement that the value is only inserted in the list if it is unique. Method signature is provided and may not be changed.
A merge() method that merges the calling list with the parameter list in such a way that the result is a list that is sorted in ascending order and contains no duplicate values. The resulting list should be returned. The two original lists should be empty when the function exits (not destroyed). Method signature is provided and may not be changed.
I am providing a main method. You will add a static method called cleanUp() that takes a string as an argument. The method should return the string after removing any leading or trailing nonalphabetic characters. The resulting string should be in all lowercase. Method signature is provided and may not be changed.
Ideally, merge() should only make one pass through each list. merge() is tricky to get right it may be solved iteratively or recursively. There are many cases to deal with: either initial list may be empty, during processing either list may run out first, and finally there's the problem of starting the result list empty, and building it up while going through the two lists. To get full credit, this method must not use insertOrder or insertOrderUnique or remove.
The main method reads from two files and builds the lists as it goes with cleaned words. It outputs each list, calls merge, and then outputs the two initial lists (which should be empty) and the resulting list.
DLinkedList
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
/** * provided file for DLinkedList Assignment * * @author Mtrro */ public class DLinkedList> {
public static void main(String[] args) throws FileNotFoundException {
DLinkedList lst1 = new DLinkedList<>(); DLinkedList lst2 = new DLinkedList<>();
Scanner fin = new Scanner(new File("text1.in")); String str;
while (fin.hasNext()) { str = fin.next(); str = cleanUp(str); lst1.insertOrderUnique(str); } fin.close();
fin = new Scanner(new File("text2.in")); while (fin.hasNext()) { str = fin.next(); str = cleanUp(str); lst2.insertOrderUnique(str); }
System.out.println("List 1: " + lst1); System.out.println("List 2: " + lst2); DLinkedList combined = lst1.merge(lst2); System.out.println(" AFTER MERGE"); System.out.println("List 1: " + lst1); System.out.println("List 2: " + lst2); System.out.println(" " + combined); }
/** * ASSIGNED * @param str * @return str in all lower case with LEADING and TRAILING non-alpha * chars removed */ public static String cleanUp(String str) { return str; }
//inner DNode class: PROVIDED private class DNode {
private DNode next, prev; private T data;
private DNode(T val) { this.data = val; next = prev = this; } }
//DLinkedList fields: PROVIDED private DNode header;
//create an empty list: PROVIDED public DLinkedList() { header = new DNode(null); }
/** * PROVIDED add * * @param item return ref to newly inserted node */ public DNode add(T item) { //make a new node DNode newNode = new DNode(item); //update newNode newNode.prev = header; newNode.next = header.next; //update surrounding nodes header.next.prev = newNode; header.next = newNode; return newNode; }
//PROVIDED public String toString() { String str = "["; DNode curr = header.next; while (curr != header) { str += curr.data + " "; curr = curr.next; } str = str.substring(0, str.length() - 1); return str + "]"; }
/** * ASSIGNED * remove val from the list * * @param val * @return true if successful, false otherwise */ public boolean remove(T val) { return true; }
/** * ASSIGNED * * @param item */ public void insertOrder(T item) { }
/** * ASSIGNED * * @param item */ public boolean insertOrderUnique(T item) { return true; }
/** * ASSIGNED * PRE: this and rhs are sorted lists * @param rhs * @return list that contains this and rhs merged into a sorted list * POST: returned list will not contain duplicates */ public DLinkedList merge(DLinkedList rhs) { DLinkedList result = new DLinkedList(); return result; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
