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===========");                  }              }      }

icon
Related questions
Question

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.
Transcribed Image Text: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.
### Understanding Java Classes and Linked Lists

This section provides a detailed look at a Java class implementation for managing linked list nodes. The code defines a `Node` class in Java, which is part of a linked list data structure. Each node stores an ID, name, score, and a reference to the next node.

#### Code Explanation

```java
public class Node {
    private A stuID;
    private B stuName;
    private C stuScore;
    private Node<A,B,C> next;

    public Node(A id, B name, C score) {
        this.stuID = id;
        this.stuName = name;
        this.stuScore = score;
        this.next = null;
    }

    public A getStuID() {
        return this.stuID;
    }

    public B getStuName() {
        return this.stuName;
    }

    public C getStuScore() {
        return this.stuScore;
    }

    public Node<A, B, C> getNext() {
        return this.next;
    }

    public void setStuID(A id) {
        this.stuID = id;
    }

    public void setStuName(B name) {
        this.stuName = name;
    }

    public void setStuScore(C score) {
        this.stuScore = score;
    }

    public void setNext(Node<A, B, C> n) {
        this.next = n;
    }

    public void displayNode() {
        System.out.println("ID=" + this.stuID + ", Name=" + this.stuName + ", Score=" + this.stuScore);
    }
}
```

#### Key Components

1. **Class Definition**: 
   - `public class Node` defines a generic class `Node` for a linked list.

2. **Attributes**: 
   - `private A stuID`: Holds the student ID.
   - `private B stuName`: Holds the student name.
   - `private C stuScore`: Holds the student score.
   - `private Node<A,B,C> next`: Pointer to the next node in the list.

3. **Constructor**:
   - Initializes a node with an ID, name, and score. The `next` pointer is set to `null`.

4. **Getters and Setters**:
   - **Get Methods**: Retrieve the values of `stuID`, `stuName
Transcribed Image Text:### Understanding Java Classes and Linked Lists This section provides a detailed look at a Java class implementation for managing linked list nodes. The code defines a `Node` class in Java, which is part of a linked list data structure. Each node stores an ID, name, score, and a reference to the next node. #### Code Explanation ```java public class Node { private A stuID; private B stuName; private C stuScore; private Node<A,B,C> next; public Node(A id, B name, C score) { this.stuID = id; this.stuName = name; this.stuScore = score; this.next = null; } public A getStuID() { return this.stuID; } public B getStuName() { return this.stuName; } public C getStuScore() { return this.stuScore; } public Node<A, B, C> getNext() { return this.next; } public void setStuID(A id) { this.stuID = id; } public void setStuName(B name) { this.stuName = name; } public void setStuScore(C score) { this.stuScore = score; } public void setNext(Node<A, B, C> n) { this.next = n; } public void displayNode() { System.out.println("ID=" + this.stuID + ", Name=" + this.stuName + ", Score=" + this.stuScore); } } ``` #### Key Components 1. **Class Definition**: - `public class Node` defines a generic class `Node` for a linked list. 2. **Attributes**: - `private A stuID`: Holds the student ID. - `private B stuName`: Holds the student name. - `private C stuScore`: Holds the student score. - `private Node<A,B,C> next`: Pointer to the next node in the list. 3. **Constructor**: - Initializes a node with an ID, name, and score. The `next` pointer is set to `null`. 4. **Getters and Setters**: - **Get Methods**: Retrieve the values of `stuID`, `stuName
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer