Question

Your text has an example of LinkedList class. Convert the class so that the Link objects have a s...

Your text has an example of LinkedList class. 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// --------------------- LinkedListDriver.java ---------------------------

public class LinkedListDriver {

public static void main(String[] args) {

Long[] values = {9l, 2l, 6l, 9l, 3l};

Long newElement = 5l;

LinkedListModified linkedList = new LinkedListModified(values);

System.out.print("Initial List : ");

linkedList.displayList();

System.out.println();

System.out.println("Linked List : is emplty : "+linkedList.isEmpty() + "\n");

linkedList.insertFirst(newElement);

System.out.println("Linked List after adding new element ["+newElement+"] at first");

linkedList.displayList();

System.out.println();

linkedList.deleteFirst();

System.out.println("Linked List after deleting first element : ");

linkedList.displayList();

System.out.println();

System.out.println("Searching element : Is element [3] exists ? : "+linkedList.search(3l) + "\n");

System.out.println("Counting existance of element : How many times element [9] exists ? : "+linkedList.numberOfValues(9l) + "\n");

linkedList.replace(9l, 8l);

System.out.println("Linked list after replacing element [9] with element [8]:");

linkedList.displayList();

System.out.println();

linkedList.zero();

System.out.println("Linked list after setting all to zero:");

linkedList.displayList();

System.out.println();

}

}

// --------------- LinkedListModified.java -----------------------

import collection.linkedlist.LLNode;

public class LinkedListModified {

protected LLNode<Long> head; // First node

protected LLNode<Long> tail; // First node

protected int listSize; // Size of LinkedList

public LinkedListModified(Long[] values) {

this.head = null;

this.listSize = 0;

for(Long value : values) {

LLNode<Long> newNode = new LLNode<Long>(value);

if (head == null)

{

this.head = newNode;

this.tail = head;

} else {

this.tail.setLink(newNode);

this.tail = newNode;

}

this.listSize++;

}

}

public boolean search(Long element) {

LLNode<Long> location = head;

boolean found = false;

while (location != null) {

// if they match

if (location.getData().equals(element)) {

found = true;

break;

}

location = location.getLink();

}

return found;

}

public int numberOfValues(Long element) {

LLNode<Long> location = head;

int count = 0;

while (location != null) {

if (location.getData().equals(element)) {

count++;

}

location = location.getLink();

}

return count;

}

public void replace(Long source, Long target) {

LLNode<Long> location = head;

while (location != null) {

// if they match

if (location.getData().equals(source)) {

location.setData(target);

} else {

location = location.getLink();

}

}

}

public void zero() {

LLNode<Long> location = head;

while (location != null) {

location.setData(0l);

location = location.getLink();

}

}

public Long[] getArray() {

Long[] elements = new Long[listSize];

LLNode<Long> location = head;

int index = 0;

while (location != null) {

elements[index]=location.getData();

location = location.getLink();

index++;

}

return elements;

}

public boolean isEmpty() {

return listSize == 0;

}

public void insertFirst(Long element) {

LLNode<Long> newNode = new LLNode<Long>(element);

if(head == null) {

head = newNode;

} else {

newNode.setLink(head);

head = newNode;

}

}

public void deleteFirst() {

if(head == null) {

return;

} else {

head = head.getLink();

}

}

public void displayList() {

LLNode<Long> location = head;

while (location != null) {

System.out.print("["+location.getData() + "]" + " --> ");

location = location.getLink();

}

System.out.println("NULL");

}

}

// Sample output below

Initial List : [9]-2] --> [6] --> [9] --> [3] --> NULL Linked List : is emplty : false Linked List after adding new element [

Add a comment
Know the answer?
Add Answer to:
Your text has an example of LinkedList class. Convert the class so that the Link objects have a s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • information and technology

    Create an EmployeeLinkedList class.  However, instead of generic Link class for the link objects, you should use objects created from an Employee class as the objects stored in the list.The Employee class represents an employee at a company.  It should have instance variables for the employee's first name, last name, id number (use an integer), date of birth (us a String), and rate of pay.  Also, any other variable(s) needed to make an Employee object work in a linked list should...

  • Define a class DoubleStack which implements two stacks of objects of type Object using a single...

    Define a class DoubleStack which implements two stacks of objects of type Object using a single shared array, so that the push and pop operations specify which of the two stacks is involved in the operation (as described below). (a) Specify necessary instance variables and write a constructor “DoubleStack(int n)” that takes integer n and creates an empty DoubleStack object with the shared array with room for n elements (2 pt); b) Write methods "boolean push(int i, Object o)" and...

  • put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following:...

    put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following: A private double 1D array instance variable named numbers. A constructor that takes one parameter, a 1D double array. The constructor should throw an IllegalArgumentException with the message "Array length of zero" if the parameter has a length of 0. If the exception is not thrown, the constructor should create the numbers array and copy every element from the parameter into the numbers instance...

  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

  • Write a class that analyzes data you have stored in a text file. Your data represents...

    Write a class that analyzes data you have stored in a text file. Your data represents the ages of the individuals in your fictitious community. The data is stored, one per line, in a text file. For example, File name: myData.txt Contents: 45 18 6.5 3 5 sdtypo# 18 10 The number of lines in your text file must fall somewhere between 20 and 100 lines. Some of the lines must hold character strings that are not convertible to integers,...

  • fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life...

    fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life Before you begin, DOWNLOAD the files “Lab8_Bicycle.java” and “Lab8_BicycleApp.java” from Canvas to your network drive (not the local hard drive or desktop). Once you have the files saved, RENAME THE FILES Bicycle.java and BicycleApp.java then open each file. 1) Look at the Bicycle class. Two of the many data properties we could use to define a bike are its color and the gear it...

  • An array of class objects is similar to an array of some other data type. To...

    An array of class objects is similar to an array of some other data type. To create an array of Points, we write Point parray [4]; To access the object at position i of the array, we write parray [i] and to call a method on that object method, we write parray [i]. methodName (arg1 , arg2 , ...) ; To initialize an array of objects whose values are known at compile time, we can write Point parray [4] =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT