Add more methods to the singly linked list class then test them • search(e) // Return one node with 3 values (stuID, stuName, stuScore) which matches a given key e (studentID). • addAfter(e, stuID, stuName, stuScore) //Add a new node with 3 values (stuID, stuName, stuScore) after the node with the key e (studentID). • removeAt(e) //Remove a node which matches a given key e (studentID) • count() //Return a number of nodes of list. • update(stuID, stuName, stuScore) //Update the values of one node three codes below two in pictures one,typed out. public class SlinkedList { private Node head; private Node tail; private int size; public SlinkedList(){ head=null; tail=null; size=0; } public int getSize(){ return size; } public boolean isEmpty(){ return size == 0; } public A getFirstStuId(){ if(isEmpty()) return null; return (A) head.getStuID(); } public B getFirstStuName(){ if(isEmpty()) return null; return (B) head.getStuName(); } public C getFirstStuScore(){ if(isEmpty()) return null; return (C) head.getStuScore(); } public void addFirst(A id, B name, C score){ Node newest = new Node<>(id, name,score); if(isEmpty()) { head=newest; tail=newest; }else{ newest.setNext(head); head=newest; } size++; } public void addLast(A id, B name, C score) { Node newest=new Node<>(id, name, score); if(isEmpty()) { tail=newest; head=newest; }else { tail.setNext(newest); tail=newest; } size++; } public A removeFirst() { if(isEmpty()) return null; A firstID = (A) head.getStuID(); head=head.getNext(); size--; if(getSize()==0) tail=null; return firstID; } public Node search(A id){ if(isEmpty()) { System.out.println("Linked list is empty"); return null; } Node temp= head; do { if(temp.getStuID()==id) return temp; temp=temp.getNext(); }while(temp!=null); System.out.println("can not find node(id"+id+")in linked list"); return null; } public Node update(A key, A nid, B nname, C nscore){ Node updateNode= search(key); if(updateNode==null) return null; updateNode.setStuID(nid); updateNode.setStuName(nname); updateNode.setStuScore(nscore); return updateNode; } public void display() { if (isEmpty()){ System.out.println("Singly linked list is empty."); } else { Node temp = head; System.out.println("=========== beginning of lists=========="); do{ temp.displayNode(); temp = temp.getNext(); }while(temp != null); System.out.println("===========Ending of lists==========="); } } }
Add more methods to the singly linked list class then test them
• search(e) // Return one node with 3 values (stuID, stuName, stuScore) which matches a
given key e (studentID).
• addAfter(e, stuID, stuName, stuScore) //Add a new node with 3 values (stuID,
stuName, stuScore) after the node with the key e (studentID).
• removeAt(e) //Remove a node which matches a given key e (studentID)
• count() //Return a number of nodes of list.
• update(stuID, stuName, stuScore) //Update the values of one node
three codes below two in pictures one,typed out.
public class SlinkedList<A,B,C> {
private Node head;
private Node tail;
private int size;
public SlinkedList(){
head=null;
tail=null;
size=0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public A getFirstStuId(){
if(isEmpty())
return null;
return (A) head.getStuID();
}
public B getFirstStuName(){
if(isEmpty())
return null;
return (B) head.getStuName();
}
public C getFirstStuScore(){
if(isEmpty())
return null;
return (C) head.getStuScore();
}
public void addFirst(A id, B name, C score){
Node<A,B,C> newest = new Node<>(id, name,score);
if(isEmpty()) {
head=newest;
tail=newest;
}else{
newest.setNext(head);
head=newest;
}
size++;
}
public void addLast(A id, B name, C score) {
Node<A,B,C> newest=new Node<>(id, name, score);
if(isEmpty()) {
tail=newest;
head=newest;
}else {
tail.setNext(newest);
tail=newest;
}
size++;
}
public A removeFirst() {
if(isEmpty())
return null;
A firstID = (A) head.getStuID();
head=head.getNext();
size--;
if(getSize()==0)
tail=null;
return firstID;
}
public Node<A,B,C> search(A id){
if(isEmpty()) {
System.out.println("Linked list is empty");
return null;
}
Node<A,B,C> temp= head;
do {
if(temp.getStuID()==id)
return temp;
temp=temp.getNext();
}while(temp!=null);
System.out.println("can not find node(id"+id+")in linked list");
return null;
}
public Node<A,B,C> update(A key, A nid, B nname, C nscore){
Node<A,B,C> updateNode= search(key);
if(updateNode==null)
return null;
updateNode.setStuID(nid);
updateNode.setStuName(nname);
updateNode.setStuScore(nscore);
return updateNode;
}
public void display() {
if (isEmpty()){
System.out.println("Singly linked list is empty.");
}
else {
Node<A,B,C> temp = head;
System.out.println("=========== beginning of lists==========");
do{
temp.displayNode();
temp = temp.getNext();
}while(temp != null);
System.out.println("===========Ending of lists===========");
}
}
}
![The image shows Java code in an IDE (Integrated Development Environment) setup, focusing on a linked list implementation. Below is a transcription of the code:
```java
public class TestSlinkedList {
public static void main(String[] args) {
SlinkedList sl = new SlinkedList();
sl.addFirst("101","John",99.0);
sl.addFirst("102","Mark",90.0);
sl.addFirst("103","Max",88.2);
sl.addFirst("106","Joan",89.2);
sl.display();
Node temp = sl.search("101");
if(temp!=null) {
System.out.println("Got it!");
temp.displayNode();
}
temp = sl.search("108");
if(temp!=null) {
System.out.println("Got it!");
temp.displayNode();
}
temp = sl.update("101","100","Marry_W",99.0);
temp.displayNode();
}
}
```
### Code Explanation:
1. **Class Definition**:
- The code defines a public class named `TestSlinkedList`.
2. **Main Method**:
- The `main` method is the entry point of the program.
3. **Linked List Operations**:
- An instance of a singly linked list, `SlinkedList`, is created.
- Nodes are added to the list using the `addFirst` method. Each node contains an ID, name, and a numerical value.
- The list is displayed with the `display` method.
- The `search` method is used to find nodes with specific IDs. If a node is found, "Got it!" is printed, followed by displaying the node details.
- The `update` method modifies the data of a node and displays the updated node.
This code is designed to demonstrate basic operations on a singly linked list, such as addition, search, and update of nodes.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2ae76079-938b-4b5d-8ceb-1dfbc10f88ce%2Fc8f39973-6740-4544-bf2b-6ae8f1a23580%2F8y1wd7a_processed.png&w=3840&q=75)


Step by step
Solved in 3 steps with 1 images
