Question: How do I create a node for this code? public class SinglyLinkedList { //---------Nested Node class----------- public static class Node { private E element; //
How do I create a node for this code?
public class SinglyLinkedList
{
//---------Nested Node class-----------
public static class Node
{
private E element; // content, this could be set to final
private Node
public Node(E e, Node
{
element = e;
next = n;
}
// Necessary set and get methods
public E getElement()
{
return element;
}
public Node
{
return next;
}
public void setNext(Node
{
next = n;
}
}
//------end of Nested Node class------
private Node
private Node
private int size = 0;
public SinglyLinkedList() //Default Constructor
{
}
public int size() // Accessor methods
{
return size;
}
public boolean isEmpty()
{
return size == 0;
}
public Node
{
return head;
}
public Node
{
return tail;
}
public E first() // Get the value of the element in the Head
{
if (isEmpty())
return null;
return head.getElement();
}
public E last() // Get the value of the element in the Tail
{
if (isEmpty())
return null;
return tail.getElement();
}
public void addFirst(E e) // Create new node and added to the head
{
head = new Node<>(e, head);
if (size == 0)
tail = head;
size++ ;
}
public void addLast(E e) // Create new node and added to the tail
{
Node
if (isEmpty())
head = newest;
else
tail.setNext(newest);
tail = newest;
size++;
}
public E removeFirst() // Remove the head node, set new head
{
if (isEmpty())
return null;
E answer = head.getElement();
head = head.getNext();
size--;
if (size == 0)
tail = null;
return answer;
}
// Methods that I created
public Node
{
Node
current.setNext(current.getNext().getNext());
size--;
return temp;
}
public void addNode(E e, Node
{
Node
current.setNext(newest);
size++;
}
}
public class SingleListDriver
{
public static void main(String[] args)
{
SinglyLinkedList
SinglyLinkedList.Node
// GameEntry entry1 = new GameEntry("Rob",750);
GameEntry entry2 = new GameEntry("Mike",1105);
GameEntry entry3 = new GameEntry("Rose",590);
GameEntry entry4 = new GameEntry("Jill",740);
GameEntry entry5 = new GameEntry("Jack",510);
GameEntry entry6 = new GameEntry("Anna",660);
GameEntry entry7 = new GameEntry("Paul",720);
GameEntry entry8 = new GameEntry("Bob",400);
System.out.println("Adding " + entry1);
highscores.addNode(entry1);
System.out.println("Adding " + entry2);
highscores.addFirst(entry2);
System.out.println("Adding " + entry3);
highscores.addFirst(entry3);
System.out.println("Adding " + entry4);
highscores.addFirst(entry4);
System.out.println("Adding " + entry5);
highscores.addFirst(entry5);
System.out.println("Adding " + entry6);
highscores.addFirst(entry6);
System.out.println("Adding " + entry7);
highscores.addFirst(entry7);
System.out.println("Adding " + entry8);
highscores.addFirst(entry8);
// Print all elements of the array
System.out.println("Traversing the linked list:");
SinglyLinkedList.Node
while (node != null)
{
GameEntry gE = node.getElement();
System.out.printf("%s -> ", node.getElement());
node = node.getNext();
}
System.out.printf("null");
System.out.println("Removing Node 2");
highscores.removeNode(entry4);
}
}
public class GameEntry
{
private String name; // name of the person earning this score
private int score; // the score value
/** Constructs a game entry with given parameters.. */
public GameEntry(String n, int s)
{
name = n;
score = s;
}
/** Returns the name field. */
public String getName()
{
return name;
}
/** Returns the score field. */
public int getScore()
{
return score;
}
/** Returns a string representation of this entry. */
public String toString()
{
return "(" + name + ", " + score + ")";
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
