Question
LinkedList class // linkList.java // demonstrates linked list // to run this program: C>java LinkListApp //////////////////////////////////////////////////////////////// class Link { public int iData; // data item
LinkedList class
// linkList.java // demonstrates linked list // to run this program: C>java LinkListApp //////////////////////////////////////////////////////////////// class Link { public int iData; // data item (key) public double dData; // data item public Link next; // next link in list // -------------------------------------------------------------
public Link(int id, double dd) // constructor { iData = id; // initialize data dData = dd; // (next is automatically } // set to null) // ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print({ + iData + , + dData + } ); } } // end class Link //////////////////////////////////////////////////////////////// class LinkList { private Link first; // ref to first link on list // ------------------------------------------------------------- public LinkList() // constructor { first = null; // no items on list yet } // ------------------------------------------------------------- public boolean isEmpty() // true if list is empty { return (first==null); } // ------------------------------------------------------------- // insert at start of list public void insertFirst(int id, double dd) { // make new link Link newLink = new Link(id, dd); newLink.next = first; // newLink --> old first first = newLink; // first --> newLink } // ------------------------------------------------------------- public Link deleteFirst() // delete first item { // (assumes list not empty) Link temp = first; // save reference to link first = first.next; // delete it: first-->old next return temp; // return deleted link }
// ------------------------------------------------------------- public void displayList() { System.out.print(List (first-->last): ); Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(); } // ------------------------------------------------------------- } // end class LinkList //////////////////////////////////////////////////////////////// class LinkListApp { public static void main(String[] args) { LinkList theList = new LinkList(); // make new list theList.insertFirst(22, 2.99); // insert four items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99); theList.insertFirst(88, 8.99); theList.displayList(); // display list while( !theList.isEmpty() ) // until its empty, { Link aLink = theList.deleteFirst(); // delete link System.out.print(Deleted ); // display it aLink.displayLink(); System.out.println(); } theList.displayList(); // display list } // end main() } // end class LinkListApp
Convert the class so that the Link objects have a single long instance variable -- call it "data" -- and the Link instance variable..
Step 2
Then add the following capabilities to the class. A constructor that takes an array of longs as an argument. The contents of the array are use to create Link objects that become objects in the LinkedList. A "search" method that takes a long argument and returns a boolean value. The method searches the LinkedList object for Link objects with a data value that matches the long argument. It returns a true or false based on whether or not a matching value is present in the LinkedList. A "numberOfValues" method that takes a long argument and returns an int value. The method searches the LinkedList object for Link objects with a data value that matches the long argument. It returns a numeric value that is the number of times the long argument is present in the LinkedList. A "replace" method that takes two long arguments and returns a void. The method finds all occurrences of the first long value in the Links of the LinkedList and replaces their data value with the method's second long value. A "zero" method that replaces all values in the LinkedList's Link objects with zeros.
A "getArray" method that returns a long array. The method returns long array that contains the long data values in the Link objects of the LinkedList
Step 3 Give the project a LinkedListDriver class that contains your main method.
Instantiate a LinkedList object, using the array constructor.
Demonstrate the isEmpty, insertFirst, deleteFirst, and displayList methods.
Also demonstrate the methods from Step 2. Remember that your output should clearly state what you are doing in your code.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started