Question

In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that...

In Java:

Assume the "ferrets" variable points to a linked list of "LLNode." Write code that traverses the list and prints the following. Do not forget to consider the case where the list is empty.

\bulletThe "LLNode" class is given:

public class LLNode
{
protected T info;
protected LLNode link;

public LLNode(T info)
{
this.info = info;
link = null;
}

public void setInfo(T info)
{
this.info = info;
}
public T getInfo()
{
return info;
}

public void setLink(LLNode link)
{
this.link = link;
}
public LLNode getLink()
{
return link;
}
}  

\bullet The "Ferret" class is to be implemented by you, and it holds "name (String)" and "weight (int)."
\bullet You must use your own implementation of the linked list data structure.
a. The sum of the weight of the ferrets on the list
b. The count of how many ferrets are on the list
c. The names of the ferrets on the list in an alphabetical order
d. The names of the ferrets on the list in a reverse alphabetical order

Hints:
\bullet You need only 3 files: (1) LLNode.java (exactly the same as the given code), (2) Ferret.java (keep it minimal), and (3) TestDriver.java (any name) with main() method
\bullet Example of code to add a node to a linked list is:

LLNode sNode1 = new LLNode("basketball");

LLNode sNode2 = new LLNode("baseball");

sNode1.setLink(sNode2);

- Example of code to traverse a linked list is:

LLNode currNode = letters;

while(currNode != null)

{

System.out.prinln(currNode.getInfo());

currNode = currNode.getLink();

}

\bullet For parts c and d, you need to use another temporary placeholder before the while loop, then output the sorted result after the while loop.

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

Please find the answers to your questions below. Let me know if you have any doubts. If you are satisfied with the solution, please upvote the answer. Thanks

a) Code to find and display the sum of all numbers on the list.

//assuming numbers point to linked list of Integers

//getting a reference to the head node

LLNode<Integer> temp=numbers;

//initializing sum to 0

int sum=0;

//looping until temp is null

while(temp!=null){

      //adding the info to the sum

      sum+=temp.getInfo();

      //getting next link

      temp=temp.getLink();

}

//displaying the sum

System.out.println(sum);

b) Code to find the count of all elements on the list.

//getting a reference to the head node

LLNode<Integer> temp=numbers;

//initializing count to 0

int count=0;

//looping until temp is null

while(temp!=null){

      //incrementing the count

      count++;

      //getting next link

      temp=temp.getLink();

}

//displaying the count

System.out.println(count);

c) Code to find the count of all positive numbers on the list.

//getting a reference to the head node

LLNode<Integer> temp=numbers;

//initializing count to 0

int count=0;

//looping until temp is null

while(temp!=null){

      //incrementing the count if the number is positive

      if(temp.getInfo()>=0){

            count++;

      }          

      //getting next link

      temp=temp.getLink();

}

//displaying the count

System.out.println(count);

d) Code to display the enumerated contents of list

//getting a reference to the head node

LLNode<Integer> temp = numbers;

// initializing index to 0

int index = 0;

// looping until temp is null

while (temp != null) {

      index++;

      // displaying index number and the number in the list

      System.out.println(index + ".\t" + temp.getInfo());

      // getting next link

      temp = temp.getLink();

}

please upvote

Add a comment
Know the answer?
Add Answer to:
In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that...
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
  • Write a recursive method remove(int target, LLNode list) that removes all occurrences of target from list...

    Write a recursive method remove(int target, LLNode list) that removes all occurrences of target from list and returns a reference to the new list. For our example list the statement values = remove(6, values); would result in values referencing the list containing 3 9 12 15 18 19 19 and 20. If target is not contained in list then the list remains unchanged. Please also add a driver class to demonstrate that the method is working correctly. LLNode.java //---------------------------------------------------------------------------- //...

  • Use the following implementation of LinkedListNode to write the Stack4T> classi public class LinkListNodecT>T private T...

    Use the following implementation of LinkedListNode to write the Stack4T> classi public class LinkListNodecT>T private T info; private LinkListNode link; public LinkListNode ( T info) this, info = info; link = null; public T getInfo() return info; public LinkListNode getlink() return link; public void setInfo( T info) this.infoinfo; public void setLink (LinklistNode link) this.link = link; public interface StackInterface <T T top() throws StackUnderflowException; void pop () throws StackUnderflowException; void push (T element);

  • Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info...

    Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data type: QueueNode<T> Constructors: Name: QueueNode Access modifier: public Parameters: info (data type T) Task: sets the value of this.info to the value of the info parameter sets the value of link to null Methods Name: setInfo Access modifier: public Parameters: info (data type T) Return type: void Task: sets the...

  • use intellij idea main java Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier:...

    use intellij idea main java Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data type: Queue Node<T> Constructors: Name: QueueNode Access modifier: public Parameters: info (data type T Task: sets the value of this.info to the value of the info parameter sets the value of link to null Methods Name: link Access modifier: private Data type: QueueNode<T> Constructors: Name:...

  • 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. 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...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • Grocery shopping list (linked list: inserting at the end of a list)

    import java.util.Scanner;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      ItemNode headNode;  // Create intNode objects                                                         ItemNode currNode;      ItemNode lastNode;      String item;      int i;      // Front of nodes list           ...

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

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