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 next; // next node in the list

public Node(E e, Node n) // constructor

{

element = e;

next = n;

}

// Necessary set and get methods

public E getElement()

{

return element;

}

public Node getNext()

{

return next;

}

public void setNext(Node n)

{

next = n;

}

}

//------end of Nested Node class------

private Node head = null;

private Node tail = null;

private int size = 0;

public SinglyLinkedList() //Default Constructor

{

}

public int size() // Accessor methods

{

return size;

}

public boolean isEmpty()

{

return size == 0;

}

public Node getHead() // Sotiris: Added getHead() you should consider whether you need set methods also

{

return head;

}

public Node getTail() // Sotiris: Added getTail()

{

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 newest = new Node<>(e, null);

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 removeNode(Node current)

{

Node temp = current.getNext();

current.setNext(current.getNext().getNext());

size--;

return temp;

}

public void addNode(E e, Node current)

{

Node newest = new Node<>(e,current.getNext());

current.setNext(newest);

size++;

}

}

public class SingleListDriver

{

public static void main(String[] args)

{

SinglyLinkedList highscores = new SinglyLinkedList<>();

SinglyLinkedList.Node entry1 = new GameEntry("Rob",750);

// 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 node = highscores.getHead();

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!