How do I create a node for this code? public class SinglyLinkedList<E> { //-----
ID: 3597192 • Letter: H
Question
How do I create a node for this code?
public class SinglyLinkedList<E>
{
//---------Nested Node class-----------
public static class Node<E>
{
private E element; // content, this could be set to final
private Node<E> next; // next node in the list
public Node(E e, Node<E> n) // constructor
{
element = e;
next = n;
}
// Necessary set and get methods
public E getElement()
{
return element;
}
public Node<E> getNext()
{
return next;
}
public void setNext(Node<E> n)
{
next = n;
}
}
//------end of Nested Node class------
private Node<E> head = null;
private Node<E> 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<E> getHead() // Sotiris: Added getHead() you should consider whether you need set methods also
{
return head;
}
public Node<E> 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<E> 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<E> removeNode(Node<E> current)
{
Node<E> temp = current.getNext();
current.setNext(current.getNext().getNext());
size--;
return temp;
}
public void addNode(E e, Node<E> current)
{
Node<E> newest = new Node<>(e,current.getNext());
current.setNext(newest);
size++;
}
}
public class SingleListDriver
{
public static void main(String[] args)
{
SinglyLinkedList<GameEntry> highscores = new SinglyLinkedList<>();
SinglyLinkedList.Node<GameEntry> 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<GameEntry> 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 + ")";
}
}
Explanation / Answer
Please find my implementation:
public class SinglyLinkedList<E>
{
//---------Nested Node class-----------
public static class Node<E>
{
private E element; // content, this could be set to final
private Node<E> next; // next node in the list
public Node(E e, Node<E> n) // constructor
{
element = e;
next = n;
}
// Necessary set and get methods
public E getElement()
{
return element;
}
public Node<E> getNext()
{
return next;
}
public void setNext(Node<E> n)
{
next = n;
}
}
//------end of Nested Node class------
private Node<E> head = null;
private Node<E> 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<E> getHead() // Sotiris: Added getHead() you should consider whether you need set methods also
{
return head;
}
public Node<E> 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<E> 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<E> removeNode(Node<E> current)
{
Node<E> temp = current.getNext();
current.setNext(current.getNext().getNext());
size--;
return temp;
}
public void addNode(E e, Node<E> current)
{
Node<E> newest = new Node<>(e,current);
newest.setNext(newest);
size++;
}
}
#############
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 + ")";
}
}
#############
public class SingleListDriver
{
public static void main(String[] args)
{
SinglyLinkedList<GameEntry> highscores = new SinglyLinkedList<>();
GameEntry 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, null);
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<GameEntry> 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");
node = highscores.getHead();
highscores.removeNode(node);
}
}
/*
Sample run:
Adding (Rob, 750)
Adding (Mike, 1105)
Adding (Rose, 590)
Adding (Jill, 740)
Adding (Jack, 510)
Adding (Anna, 660)
Adding (Paul, 720)
Adding (Bob, 400)
Traversing the linked list:
(Bob, 400) -> (Paul, 720) -> (Anna, 660) -> (Jack, 510) -> (Jill, 740) -> (Rose, 590) -> (Mike, 1105) -> nullRemoving Node 2
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.