Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the main method to the driver class that tests the methods from the Rlist class package list; public class RList { private Node

Implement the main method to the driver class that tests the methods from the Rlist class

 

package list;

public class RList {

private Node head; // points to first item private int k; // number of items in list

public RList() { k = 0; head = null; }

public boolean isEmpty() { return (k == 0); }

public int size() { return k; }

public void add(int index, Object o) throws ListIndexOutOfBoundsException { if(index >= 0 && index < k+1) { if(index == 0) { Node newNode = new Node(o, head); head = newNode; } else { Node prev = find(index-1); Node newNode = new Node(o, prev.next); prev.next = newNode; } k++; } else { throw new ListIndexOutOfBoundsException("List index out of bounds on add"); } }

private Node find(int index) { Node curr = head; for(int skip = 0; skip < index; skip++) { curr = curr.next; } return curr; }

public Object get(int index) throws ListIndexOutOfBoundsException { if(index >= 0 && index < k) { Node curr = find(index); Object item = curr.item; return item; } else { throw new ListIndexOutOfBoundsException("List index out of bounds on get"); } }

public void remove(int index) throws ListIndexOutOfBoundsException { if(index >= 0 && index < k) { if(index == 0) { head = head.next; } else { Node prev = find(index-1); Node curr = prev.next; prev.next = curr.next; } k--; } else { throw new ListIndexOutOfBoundsException("List index out of bounds on remove"); } }

public void clear() { head = null; k = 0; }

public void display() { Node curr = head; while(curr != null) { System.out.printf("%-18s %-22s %s ", curr, curr.item, curr.next); curr = curr.next; } }

public void testComparison(Object o) { Node curr = head; if(curr.item.equals(o)) { System.out.println("Matches"); } else { System.out.println("Does not match"); }

}

public void add(Object o) { Node newNode = new Node(o, null); if(head == null) { head = newNode; } else { Node curr = head; while(curr.next != null) { curr = curr.next; } curr.next = newNode; } k++; }

public void addFirst(Object o) { Node newNode = new Node(o, head); head = newNode; k++; }

public int indexOf(Object o) { int index = 0; Node curr = head; while(curr.next != null) { if(curr.item.equals(o)) { return index; } curr = curr.next; index++; } return -1; }

public int lastIndexOf(Object o) { int index = 0; int lastIndex = -1; Node curr = head; while(curr.next != null) { if(curr.item.equals(o)) { lastIndex = index; } curr = curr.next; index++; } return lastIndex; }

public boolean remove(Object o) { if(indexOf(o) == -1) { return false; } else { remove(indexOf(o)); return true; } }

public boolean removeLast() { if(head == null) { return false; } Node curr = head; while(curr.next.next != null) { curr = curr.next; } curr.next = null; k--; return true; }

public boolean removeLast(Object o) { int lastIndex = lastIndexOf(o); if(lastIndex == -1) { return false; } else { remove(lastIndex); return true; } }

public int removeAll(Object o) { int count = 0; while(remove(o)) { count++; } return count; } public void set(int index, Object o) { Node curr = find(index); curr.item = o; }

public void setAll(Object find, Object replacement) { int index = indexOf(find); Node curr = find(index); while(index != -1) { set(index, replacement); curr = curr.next; index = indexOf(find); } }

public Object[] toArray() { Object[] array = new Object[k]; Node curr = head; for(int i = 0; i < k; i++) { array[i] = curr.item; curr = curr.next; } return array; }

private class Node {

private Object item; private Node next;

private Node(Object o, Node n) { item = o; next = n; } } }

driver class code

package driver;

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;

import Person.Person; import list.RList;

public class Driver {

public static void main(String[] args) throws FileNotFoundException { File file = new File("files/people.txt"); Scanner scnr = new Scanner(file); RList people = new RList(); while(scnr.hasNext()) { int ID = scnr.nextInt(); String name = scnr.next(); Person p = new Person(ID, name); people.add(people.size(), p); } scnr.close(); people.display(); Person p = new Person(46952, "Gilberto"); people.testComparison(p); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

It looks like youve shared the code for a linked list implementation RList class along with a simple driver class Driver class that reads data from a ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions