1. Engineering
  2. Computer Science
  3. a1simpledriverjava file a1simpledriverjava author your name here...

Question: a1simpledriverjava file a1simpledriverjava author your name here...

Question details

CSCI 225 Assignment 1 A Doubly-Linked List Implementation with Generics Due: Monday, January 28, 2019, 10pm In this assignment, you will implement a generic doubly-linked list class. This class can be used in several ways, e.g. to support a generic queue or a generic stack class What is a Doubly-Linked List? Doubly-linked lists are like the singly-linked lists in your lecture notes. Individual data elements are stored within a node structure. Nodes in doubly-linked lists, however, contain both, a reference to the next list element, as well as a reference to the previous list element. The previous reference at the front of the list and the next reference at the back of the list are null. With such a doubly-linked structure, the list can easily be traversed from the front to the back by following the next references, and from the back to the front by following the previous references One node with prev(ious) reference(a), data(b) and next reference (c) A doubly linked list: front back O-indexed access: Consider the following linked list elementAt(1) returns 76 insertAt(81, 2) will result in the following list (81 at index 2) front - 16-76-21-53back front- 16-76-81-21-53 back removeAt(0) returns 16 and results in the list: front-> 76-81-21-53 <-back Your Assignment: Implement the DLinkedList generic class to store data of any reference type (see chapter 18 of your textbook); this class includes a generic DListNode inner class implemented for you in the DLinkedList.java file Complete the code in the DLinkedList.java file. Refer to the comments for the class definition and functional requirements. Special attention must be taken in any case where the front or the back of a list is changed by an insertion or a removal.

CSCI 225 Assignment 1 Exception Handling Your DLinkedList class must throw exceptions on invalid inputs such as list indices out of bounds or empty list. See the comments in DLinkedList.java for details on any exceptions that will be thrown by these class methods. As example, check out the elementAt () method in the code of CorrectListSolution.java of lab 3. Testing .A simple test driver (AlSimpleDriver.java) is provided. This driver will call most methods, you have been asked to implement, but it is not a thorough test of each methods special and general cases. Extend your testing to ensure that your methods work correctly for all general and special situations Your test driver (AlSimpleDriver java) should also test and handle all possible exceptions using try... catch blocks. Deliverables Upload the following two files to C4 before the due date: DLinkedList.java A1SimpleDriver.java

-------------------------------------------------------------------------------

A1SimpleDriver.java

// File: A1SimpleDriver.java
// Author: (your name here)
// Rita Ester
// Date: January 2019
// Description: Testing for the doubly-linked list class of Ass. 1
//

import java.util.ArrayList;

public class A1SimpleDriver
{
public static void main(String[] args)
{
System.out.println("Entering DLinkedList test ...");
listTest();
System.out.println("DLinkedList test (in-complete) ended!!!");

}

public static void listTest()
{
DLinkedList<Integer> listA = new DLinkedList<Integer>();
listA.insertFront(5);
listA.insertBack(10);
System.out.print("listA: ");
printList(listA);
  
DLinkedList<Integer> listB = new DLinkedList<Integer>(listA);
System.out.print("listB: ");
printList(listB);
if (listA.equals(listB))
   System.out.println("Both lists are equal");
else
   System.out.println("Both lists are different");
  
  
listB.insertAt(7, 1);
System.out.print("listB: ");
printList(listB);
if (listA.equals(listB))
   System.out.println("Both lists are equal");
else
   System.out.println("Both lists are different");
  
System.out.println("listA contains 7: " + listA.contains(7));
System.out.println("listB contains 7: " + listB.contains(7));
  
System.out.println("listB element at index 1: " + listB.elementAt(1));
  
boolean done = listA.removeAll(5);
if (done)
System.out.print("listA, " + 5 + " removed:");
printList(listA);
  
done = listA.removeAll(10);
if (done)
System.out.print("listA, " + 10 + " removed: ");
printList(listA);
  

listB.insertBack(7);
listB.insertBack(12);
listB.insertBack(7);
listA = new DLinkedList<Integer>(listB);
  
System.out.print("listB: ");
printList(listB);
  
listB.removeAll(7);
System.out.print(" listB 7 removed: ");
printList(listB);
  
System.out.print(" listA: ");
printList(listA);
}
  
public static void printList(DLinkedList<Integer> ls){
   ArrayList<Integer> arrayList = ls.insertForward();
   for (int i = 0; i < arrayList.size(); i++)
   {
   System.out.print(arrayList.get(i) + " ");
   }
   System.out.println(". Size of this list: "+ ls.size());   
}
}

-----------------------------------------------------------------------------------------------

DLinkedList.java

// File: DLinkedList.java
// Author: (your name here)
// Rita Ester
// Date: January 2019
// Description: Implementation of a generic 0-indexed doubly-linked list class
// and list node

import java.util.ArrayList;

public class DLinkedList<T> {
  
private class DListNode<PT> {
   // one node for a doubly linked list
public PT data;
public DListNode<PT> prev;
public DListNode<PT> next;
  
public DListNode(PT value){
data = value;
prev = null;
next = null;
}
}
  
private DListNode<T> front; // reference to the beginning of the list
private DListNode<T> back; // reference to the end of the list
  
// public methods
  
// default constructor
public DLinkedList(){
front = null;
back = null;
}
  
// copy constructor
public DLinkedList(DLinkedList<T> other) {
// to be completed
   // copies all data, in the same sequence as in the other list
}

  
// returns true if item exists
// PARAM: item to be searched for.
public boolean contains(T item) {
DListNode<T> nd = front;
while (nd != null) {
if (nd.data.equals(item))
return true;
nd = nd.next;
}
// exited while loop, nd == null, item not found
return false;
}
  
  
// returns the size of the list.
// this list implementation does not store the size of the list as a private member field.
public int size() {
   // to be completed
return -1;
}
  
  
// returns whether the list is empty
public boolean isEmpty() {
return (front == null);
}
  
  
// Inserts an item at the front of the list
// POST: List contains item at front
// PARAM: item = item to be inserted
public void insertFront(T item) {
// to be completed
   // special case: the list is empty
}
  
  
// Inserts an item at the back of the list
// POST: List contains item at back
// PARAM: item = item to be inserted
public void insertBack(T item) {
// to be completed
   // special case: the list is empty
}
  
  
// Inserts an item in position pos (0-indexed)
// Throws exception for invalid index (IndexOutOfBoundsException)
// PRE: 0 <= pos <= size of the list
// POST: List contains item at position pos
// PARAM: item = item to be inserted, pos = position where item will be inserted
public void insertAt(T item, int pos) {
// to be completed
// general case: insert in the middle.
   // special cases: insert at the front, insert at the back
}

// tests if two lists have the same size and if they contain the same elements
// returns false if the otherList is different from this list.
public boolean equals(DLinkedList<T> other) {
   // to be completed
   // compare the size and the data. Use the equals method to compare data.
   return true;
}


// Returns the item at pos from the list.
// Throws an appropriate exception if the index is out of bounds or if the list is empty
// PRE: 0 <= pos <= size of the list
// PARAM: pos of item to be returned
public T elementAt(int pos){
// to be completed
return front.data;
}
  
// Removes all occurrences of item from the list (not just the first one!)
// POST: all items are removed from list,
// PARAM: item to be removed
// returns true if successfully removed all items.
public boolean removeAll(T item) {
// to be completed
// repeat for all nodes in the list:
// if this is a targetNode (where the item is found as data): remove it
   // special cases: remove from a list with only one node, remove at the front, remove at the back
   // general case: remove in the middle.      
   return true;
}

// For testing purposes
// inserts list contents to an ArrayList, iterates from front
public ArrayList<T> insertForward() {
ArrayList<T> al = new ArrayList<T>();
DListNode<T> nd = front;
  
while (nd != null ) {
al.add(nd.data);
nd = nd.next;
}
return al;
}
  
// For testing purposes
// inserts list contents to an ArrayList, iterates from back
public ArrayList<T> insertBackward()
{
ArrayList<T> al = new ArrayList<T>();
DListNode<T> nd = back;
while (nd != null)
{
al.add(nd.data);
nd = nd.prev;
}
return al;
}
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution