
Concept explainers
Hello. Please add a toString() function to the Java class implementation of the singly-linked list which returns the contents of the singly-linked list. Also, does Java have a built-in class that implements singly-linked lists? Thank you.
public class SinglyLinkedList<E>
{
private static class Node<E>
{
private E element;
private Node<E> next;
public Node(E e, Node<E> n)
{
element = e;
next = n;
}
public E getElement() { return element;}
public Node<E> getNext() { return next;}
public void setNext(Node<E> n) { next = n;}
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
public SinglyLinkedList() {}
public int size() { return size;}
public boolean isEmpty() { return size == 0;}
public E first()
{
if(isEmpty()) return null;
return head.getElement();
}
public E last()
{
if(isEmpty()) return null;
return tail.getElement();
}
public void addFirst(E e)
{
head = new Node<>(e, head);
if(size == 0)
tail = head;
size++;
}
public void addLast(E e)
{
Node<E> newest = new Node<>(e, null);
if(isEmpty())
head = newest;
else
tail.setNext(newest);
tail = newest;
size++;
}
public E removeFirst()
{
if(isEmpty()) return null;
E answer = head.getElement();
head = head.getNext();
size--;
if(size == 0)
tail = null;
return answer;
}
}

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

- JAVA please Given main() in the ShoppingList class, define an insertAtEnd() method in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list. The output is: Kale Lettuce Carrots Peanuts Code provided in the assignment ItemNode.java:arrow_forwardPlease fill in the code gaps if possible. This problem has been giving me trouble. Any help is appreciated. public class Node { private String element; // we assume elements are character strings private Node next; /* Creates a node with the given element and next node. */ public Node(String s, Node n) { //fill in body } /* Returns the element of this node. */ public String getElement() { //fill in body } /* Returns the next node of this node. */ public Node getNext() { //fill in body } // Modifier methods: /* Sets the element of this node. */ public void setElement(String newElem) { //fill in body } /* Sets the next node of this node. */ public void setNext(Node newNext) { //fill in body } }arrow_forwardI want convert the code from singly-linked list to doubly-linked list package Problem; class Node{ private Object value; // Holds the data of the node private Node next; // Holds the follwoing node's address Node(Object v, Node n){ this.value =v; this.next = n; } public Node getNext(){ return this.next; } public Object getValue(){ return this.value; } public void setNext(Node n){ this.next=n; } public void setValue(Object v) { this.value=v; } } class SLL{ public Node head; SLL(){ this.head=null; } public void display(){ Node curr = this.head; while(curr != null) { System.out.print(curr.getValue()); if(curr.getNext()!=null)…arrow_forward
- Computer Science PLEASE HELP TO CONVERT THIS FOR LOOP TO FOREACH LOOP using System;using System.Collections.Generic; namespace SwinAdventure{public class IdentifierableObject{//field ,datapublic List<string> Identifier = new List<string>(); //constructorpublic IdentifierableObject(string[] ident){for (int i = 0; i < ident.Length; i++){Identifier.Add(ident[i]);}} // Methodpublic void AddIdentifier(string id){Identifier.Add(id.ToLower());} // propertiespublic string FirstID{get{return Identifier[0];}} public bool AreYou(string id){for (int i = 0; i < Identifier.Count; i++){if (Identifier[i] == id.ToLower()){return true;} }return false;}}}arrow_forwardImplement a Single linked list to store a set of Integer numbers (no duplicate) • Instance variable• Constructor• Accessor and Update methods 3. Define TestSLinkedList Classa. Declare an instance of the Single List class.b. Test all the methods of the Single List class.arrow_forwardJava Solution with explanation in detailed please!arrow_forward
- Consider the instance variables and constructors. Given Instance Variables and Constructors: public class SimpleLinkedList<E> implements SimpleList<E>, Iterable<E>, Serializable { // First Node of the List private Node<E> first; // Last Node of the List private Node<E> last; // Number of List Elements private int count; // Total Number of Modifications (Add and Remove Calls) private int modCount; /** * Creates an empty SimpleLinkedList. */ publicSimpleLinkedList(){ first = null; last = null; count = 0; modCount = 0; } ... Assume the class contains the following methods that work correctly: public boolean isEmpty() public int size() public boolean add(E e) public E get(int index) private void validateIndex(int index, int end) Complete the following methods based on the given information from above. /** * Adds an element to the list at the…arrow_forwardI want convert the code from singly-linked list to doubly-linked list package Problem; class Node{ private Object value; // Holds the data of the node private Node next; // Holds the follwoing node's address Node(Object v, Node n){ this.value =v; this.next = n; } public Node getNext(){ return this.next; } public Object getValue(){ return this.value; } public void setNext(Node n){ this.next=n; } public void setValue(Object v) { this.value=v; } } class SLL{ public Node head; SLL(){ this.head=null; } public void display(){ Node curr = this.head; while(curr != null) { System.out.print(curr.getValue()); if(curr.getNext()!=null)…arrow_forwardA) Write a generic Java queue class (a plain queue, not a priority queue). Then, call it GenericQueue, because the JDK already has an interface called Queue. This class must be able to create a queue of objects of any reference type. Consider the GenericStack class shown below for some hints. Like the Stack class below, the GenericQueue should use an underlying ArrayList<E>. Write these methods and any others you find useful: enqueue() adds an E to the queue peek() returns a reference to the object that has been in the queue the longest, without removing it from the queue dequeue() returns the E that has been in the queue the longest, and removes it from the queue contains(T t) returns true if the queue contains at least one object that is equal to t *in the sense that calling .equals() on the object with t the parameter returns true.* Otherwise contains returns false. size() and isEmpty() are obvious.arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





