public class LinkedNodesExamQuestion {
/*
>>>>>>>>>>>>>>>>>>
INSTRUCTIONS
<<<<<<<<<<<<<<<<
1. Read through this
entire class to make sure you understand the context.
A class named SinglyLinked is provided for you below. It provides
an
add method for adding new nodes on the front of the chain; this
method
should not be changed. It also provides a max method that you
must
complete. Note that the data type allowed in a Node is constrained
to
be a class that implements the Comparable interface and thus has
a
natural total order defined.
2. A sample main
method is provided for you to illustrate the intended
effect of building a simple chain of nodes and then finding the
largest
element using the max method.
3. Once you have read
through all the provided source code, your task is
to provide a correct implementation of the max method at the very
bottom
of this file.
4. Your grade will be
solely determined by the percentage of test cases that
your submitted code passes.
*/
/** Provides an example. */
public static void main(String[] args) {
SinglyLinked<Integer> iChain = new
SinglyLinked<>();
iChain.add(10);
iChain.add(12);
iChain.add(8);
iChain.add(2);
iChain.add(6);
iChain.add(4);
Integer largestInteger = iChain.max();
// The following statement should print 12.
System.out.println(largestInteger);
SinglyLinked<String> sChain = new
SinglyLinked<>();
sChain.add("W");
sChain.add("A");
sChain.add("R");
sChain.add("E");
sChain.add("A");
sChain.add("G");
sChain.add("L");
sChain.add("E");
String largestString = sChain.max();
// The following statement should print W.
System.out.println(largestString);
}
static class SinglyLinked<T extends Comparable<T>> {
/** A reference to the
first node in the chain. */
Node front;
/** Defines a node
class. */
class Node {
T element;
Node next;
public Node(T elmt,
Node nxt) {
element = elmt;
next = nxt;
}
}
/** Constructs an
empty chain. */
public SinglyLinked() {
front = null;
}
/** Adds a node with
the given value to the front of the chain. */
public void add(T value) {
front = new Node(value, front);
}
/*
>>>>>>>>>>>>>>>>>>
YOUR WORK GOES BELOW
<<<<<<<<<<<<<<<<
*/
///////////////////////////////////////////////////
// YOU MUST COMPLETE THE BODY OF THE MAX METHOD. //
///////////////////////////////////////////////////
/**
* Returns the maximum element in the chain. If the chain is
empty
* (front is null), the max method should return null.
*/
public T max() {
// COMPLETE THE BODY OF THIS METHOD
}
}
}
Code for max function:
public T max() {
Node temp = front.next;
T max = front.element;
while(temp != null) {
T candidateValue = temp.element;
if (candidateValue.compareTo(max) > 0) { // equivalent to
candidate > max
max = candidateValue;
}
temp = temp.next;
}
return max;
}
Full code to execute:
import java.util.*;
class m{
static class SinglyLinked<T extends
Comparable<T>> {
/** A reference to the first node in the chain. */
Node front;
/** Defines a node class. */
class Node {
T element;
Node next;
public Node(T elmt, Node nxt) {
element = elmt;
next = nxt;
}
}
/** Constructs an empty chain. */
public SinglyLinked() {
front = null;
}
/** Adds a node with the given value to the front of the chain.
*/
public void add(T value) {
front = new Node(value, front);
}
public T max() {
Node temp = front.next;
T max = front.element;
while(temp != null) {
T candidateValue = temp.element;
if (candidateValue.compareTo(max) > 0) { // equivalent to
candidate > max
max = candidateValue;
}
temp = temp.next;
}
return max;
}
}
public static void main(String args[]){
SinglyLinked<Integer> iChain
= new SinglyLinked<>();
iChain.add(10);
iChain.add(12);
iChain.add(8);
iChain.add(2);
iChain.add(6);
iChain.add(4);
Integer largestInteger =
iChain.max();
System.out.println(largestInteger);
SinglyLinked<String> sChain =
new SinglyLinked<>();
sChain.add("W");
sChain.add("A");
sChain.add("R");
sChain.add("E");
sChain.add("A");
sChain.add("G");
sChain.add("L");
sChain.add("E");
String largestString =
sChain.max();
// The following statement should
print W.
System.out.println(largestString);
}
}
Code output screenshot:

public class LinkedNodesExamQuestion { /* >>>>>>>>>>>>>>>>>> INSTRUCTIONS <<<<<<<<<<<<<<<< 1. Read through this entire class to...
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...
Here is the IntegerLinkedList_incomplete
class:
public class IntegerLinkedList {
static class Node {
/** The element stored at this node */
private int element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n...
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...
//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...
Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable { // ---------------- nested Node class...
Instructions Create a class BettterTree that extends OurTree, to facilitate the following. Update: if extending the class is too much, you can modify class OurTree. In our discussions about OurTree, we focused on nodes and their left and right children. For each node in the tree, is there another node of interest in addition to its children? If yes, how would you extend OurTree for that? How will the behavior of addNode method change? OurTree Code: public class OurTree {...
Hi,
So I have a finished class for the most part aside of the toFile
method that takes a file absolute path +file name and writes to
that file. I'd like to write everything that is in my run method
also the toFile method. (They are the last two methods in the
class). When I write to the file this is what I get.
Instead of the desired
That I get to my counsel. I am
having trouble writing my...
Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...
// 1. Add methods to get and set, Data and Link. Data should be any Comparable object. class Node { Integer data; // Integer is Comparable Node link; public Node(Integer data, Node link) { this.data = data; this.link = link; } public Integer getData() { return data; } public void setData(Integer data) { this.data = data; } public Node getLink() { return link; } public void setLink(Node link) { this.link = link; } } // b. Create MyLinkedList class and...