Question

In Java You may add any classes or methods to the following as you see fit in order to complete t...

In Java

You may add any classes or methods to the following as you see fit in order to complete the given tasks.

  1. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at a time.

This is the LinkedList class

import java.util.NoSuchElementException;
import java.util.Iterator;

/**
* A class to represent a linked list of nodes.
*/
public class LinkedList<T> implements Iterable<T> {
/** the first node of the list, or null if the list is empty */
private LLNode<T> firstNode;
  
/**
* Creates an initially empty linked list
*/
public LinkedList() {
firstNode = null;
}
  
/**
* Returns the first node.
*/
protected LLNode<T> getFirst() {
return firstNode;
}

/**
* Changes the first node.
* @param node the first node of the new linked list
*/
protected void setFirst(LLNode<T> node) {
this.firstNode = node;
}

/**
* Add an element to the front of the linked list
* @param element the element to add
*/
public void addToFront(T element) {
setFirst(new LLNode<T>(element, getFirst()));
}
  
/**
* Return whether the list is empty
* @return true if the list is empty
*/
public boolean isEmpty() {
return (getFirst() == null);
}
  
/**
* Returns the length of the linked list
* @return the number of nodes in the list
*/
public int length() {
int count = 0; // counts number of nodes seen
LLNode<T> nodeptr = getFirst();
while (nodeptr != null) {
count++;
nodeptr = nodeptr.getNext();
}
return count;
}
  
/**
* Remove and return the element at the front of the list
* @return the first element of the list
* @throws NoSuchElementException if there is no such element
*/
public T removeFromFront() {
if (isEmpty())
throw new NoSuchElementException();
else {
T save = getFirst().getElement();
setFirst(getFirst().getNext());
return save;
}
}

/**
* Add an element to the very end of the list
* @param element the element to add to the end of the list
*/
public void addToEnd(T element) {
if (isEmpty())
addToFront(element);
else {
LLNode<T> nodeptr = getFirst();
// the loop will end with nodeptr looking at the last node in list
while (nodeptr.getNext() != null)
nodeptr = nodeptr.getNext();
nodeptr.setNext(new LLNode<T>(element, null));
}
}
  
/**
* Print the contents of the list to the console.
* @param list the list to print
*/
public static <T> void printList1(LinkedList<T> list) {
LLNode<T> nodeptr = list.getFirst();
while (nodeptr != null) {
System.out.print(nodeptr.getElement().toString() + " ");
nodeptr = nodeptr.getNext();
}
System.out.println();
}
  
/**
* Print the contents of the list to the console.
* @param list the list to print
*/
public static <T> void printList2(LinkedList<T> list) {
// this loop is using the iterator instead of the nodes directly
Iterator<T> it = list.iterator();
while (it.hasNext())
System.out.print(it.next().toString() + " ");
System.out.println();
}
  
/**
* Returns an iterator that will run through the linked list contents in order
* @return the iterator for this linked list
*/
public Iterator<T> iterator() {
return new LinkedListIterator<T>(this.getFirst());
}
}

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

Added the method

public void append(LinkedList<T> list) {

if (list.isEmpty()) {

return;

} else if (isEmpty()) {

this.setFirst(list.getFirst());

}else {

LLNode<T> nodeptr = getFirst();

// the loop will end with nodeptr looking at the last node in list

while (nodeptr.getNext() != null)

nodeptr = nodeptr.getNext();

nodeptr.setNext(list.getFirst());

}

}


======================================================================
SEE CODE, Dont WORRY About compiler warnings


public void append(LinkedList<T> list) 940 95 96 97 if (list.isEmptyO) t else if (isEmptyO) f else ł return; 98 this.setFirst


Thanks, PLEASE COMMENT if there is any concern,

Add a comment
Know the answer?
Add Answer to:
In Java You may add any classes or methods to the following as you see fit in order to complete t...
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
  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • Using Java coding, complete the following: This program should implement a LinkedList data structure to handle...

    Using Java coding, complete the following: This program should implement a LinkedList data structure to handle the management of student records. It will need to be able to store any number of students, using a Linked List. LinearNode.java can be used to aid creating this program Student should be a class defined with the following:    • String name    • int year    • A linked list of college classes (college classes are represented using strings)    • An...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...

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