Assume the \"ferrets\" variable points to a linked list of \"LLNode<Ferret> .\"
ID: 3756740 • Letter: A
Question
Assume the "ferrets" variable points to a linked list of "LLNode<Ferret> ." Write code that traverses the list and prints the following. Do not forget to consider the case where the list is empty.* The "LLNode<T>" class is in the "2.7 Introduction to Liked Lists"
* The "Ferret" class is to be implemented by you, and it holds "name" and "weight."
* 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
[in-class-only extra credits]
* each applied toward the lowest of your single lab/test in the past (not future).
e. Remove a ferret in the middle of the list
f. Remove a ferret in the middle of the list Assume the "ferrets" variable points to a linked list of "LLNode<Ferret> ." Write code that traverses the list and prints the following. Do not forget to consider the case where the list is empty.
* The "LLNode<T>" class is in the "2.7 Introduction to Liked Lists"
* The "Ferret" class is to be implemented by you, and it holds "name" and "weight."
* 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
[in-class-only extra credits]
* each applied toward the lowest of your single lab/test in the past (not future).
e. Remove a ferret in the middle of the list
f. Remove a ferret in the middle of the list
* The "LLNode<T>" class is in the "2.7 Introduction to Liked Lists"
* The "Ferret" class is to be implemented by you, and it holds "name" and "weight."
* 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
[in-class-only extra credits]
* each applied toward the lowest of your single lab/test in the past (not future).
e. Remove a ferret in the middle of the list
f. Remove a ferret in the middle of the list * The "LLNode<T>" class is in the "2.7 Introduction to Liked Lists"
* The "Ferret" class is to be implemented by you, and it holds "name" and "weight."
* 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
[in-class-only extra credits]
* each applied toward the lowest of your single lab/test in the past (not future).
e. Remove a ferret in the middle of the list
f. Remove a ferret in the middle of the list
Explanation / Answer
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 + ". " + temp.getInfo());
// getting next link
temp = temp.getLink();
}
e) Code to display the enumerated contents of list in reverse order, using Stack
//getting a reference to the head node
LLNode<Integer> temp = numbers;
// creating an empty stack
Stack<Integer> stack = new Stack<Integer>();
// looping until temp is null
while (temp != null) {
// pushing the number to stack
stack.push(temp.getInfo());
// getting next link
temp = temp.getLink();
}
// initializing index to 0
int index = 0;
// looping until stack is empty
while (!stack.isEmpty()) {
index++;
//removing and storing the top element of stack
int number=stack.pop();
// displaying index number and the removed top element of stack
System.out.println(index + ". " + number);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.