Add more methods to the doubly 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 two codes below  package DlinkedList; class Node { private A stuID; private B stuName; private C stuScore; private Node next; private Node prev;   public Node(A id, B name, C score) { stuID = id; stuName = name; stuScore = score; next = null; prev = null; } public A getStuID() { return stuID; } public B getStuName() { return stuName;   } public C getStuScore() { return stuScore; } public Node getNext(){ return next; } public Node getPrev(){ return prev; } public void setStuID(A ID) { stuID = ID; } public void setStuName(B name) { stuName = name; }    public void setStuScore(C score) {     stuScore = score;      }   public void setNext(Node n) { next = n; } public void setPrev(Node p) { prev = p; }   public void displayNode() { System.out.println( "ID="+getStuID()+", Name="+getStuName()+", Score="+getStuScore());   } }   package DlinkedList; public class DLinkedList {     private Node header;     private Node trailer;     private int size;          public DLinkedList() {         header = new Node<>(null, null, null);         trailer = new Node<>(null, null, null);         header.setNext(trailer);         trailer.setPrev(header);              }     public int getSize() {         return size;     }     public boolean isEmpty() {         return size==0;     }     public A getFirstStuID() {         return header.getNext().getStuID();     }     public B getFirstStuName() {         return header.getNext().getStuName();     }     public C getFirstStuScore() {         return header.getNext().getStuScore();          }          public A getLastStuID() {         return trailer.getPrev().getStuID();     }     public B getLastStuName() {         return trailer.getPrev().getStuName();     }     public C getLastStuScore() {         return trailer.getPrev().getStuScore();     }          private void addBetween(A id, B name, C score, Node predecessor, Node successor) {         Node newest = new Node<>(id, name, score);         newest.setPrev(predecessor);         newest.setNext(successor);         predecessor.setNext(newest);         successor.setPrev(newest);         size++;         }     public void addFirst(A id, B name, C score) {         addBetween(id, name,score,header,header.getNext());     }     public void addLast(A id, B name, C score) {         addBetween(id, name,score, trailer.getPrev(), trailer);     }     private Node remove(Node node){         Node predecessor = node.getPrev();         Node successor = node.getNext();         predecessor.setNext(successor);         successor.setPrev(predecessor);         size--;         return node;     }     public Node removeFirst(){         if(isEmpty())             return null;         return remove(header.getNext());     }     public Node removeLast(){         if(isEmpty())             return null;         return remove(trailer.getPrev());     }     public Node search(A key){         if(isEmpty())             return null;         Node temp = header.getNext();         do{             if(temp.getStuID()== key)                 return temp;             temp = temp.getNext();             }while ( temp != null);         return null;     }          public void addAfter(A key, A id, B name, C score) {         if(isEmpty())             return;         Node node = search(key);         if(node == null)        return;         addBetween(id, name, score, node, node.getNext());     }          public void display() {         if(isEmpty()) {             System.out.println("Linked list is empty.");             return;         }         Node temp = header.getNext();         do {             temp.displayNode();             temp = temp.getNext();         }while(temp.getNext() !=null);     }     public void addBefore(A key, A id, B name, C score) {         if(isEmpty())             return;         Node node = search(key);         if(node == null)             return;         addBetween( id, name, score, node.getPrev(), node);     }     public Node removeAt( A key){         if (isEmpty())             return null;         Node node = search(key);         if(node == null)             return null;         return remove(node);     }     public Node update(A key, A nid, B nname, C nscore){         if(isEmpty())             return null;         Node node = search(key);         if(node == null)             return null;         node.setStuID(nid);         node.setStuName(nname);         node.setStuScore(nscore);         return node;     } }

icon
Related questions
Question

Add more methods to the doubly 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

two codes below 

package DlinkedList;
class Node<A,B,C> {
private A stuID;
private B stuName;
private C stuScore;
private Node<A,B,C> next;
private Node <A,B,C> prev;
 
public Node(A id, B name, C score) {
stuID = id;
stuName = name;
stuScore = score;
next = null;
prev = null;
}
public A getStuID() {
return stuID;
}
public B getStuName() {
return stuName;
 
}
public C getStuScore() {
return stuScore;
}
public Node<A,B,C> getNext(){
return next;
}
public Node<A,B,C> getPrev(){
return prev;
}
public void setStuID(A ID) {
stuID = ID;
}
public void setStuName(B name) {
stuName = name;
}
   public void setStuScore(C score) {
    stuScore = score;
     }
 
public void setNext(Node<A,B,C> n) {
next = n;
}
public void setPrev(Node<A,B,C> p) {
prev = p;
}
 
public void displayNode() {
System.out.println( "ID="+getStuID()+", Name="+getStuName()+", Score="+getStuScore());
 
}
}
 

package DlinkedList;
public class DLinkedList<A,B,C> {
    private Node<A,B,C> header;
    private Node<A,B,C> trailer;
    private int size;
    
    public DLinkedList() {
        header = new Node<>(null, null, null);
        trailer = new Node<>(null, null, null);
        header.setNext(trailer);
        trailer.setPrev(header);
        
    }
    public int getSize() {
        return size;
    }
    public boolean isEmpty() {
        return size==0;
    }
    public A getFirstStuID() {
        return header.getNext().getStuID();
    }
    public B getFirstStuName() {
        return header.getNext().getStuName();
    }
    public C getFirstStuScore() {
        return header.getNext().getStuScore();
    
    }
    
    public A getLastStuID() {
        return trailer.getPrev().getStuID();
    }
    public B getLastStuName() {
        return trailer.getPrev().getStuName();
    }
    public C getLastStuScore() {
        return trailer.getPrev().getStuScore();
    }
    
    private void addBetween(A id, B name, C score, Node<A,B,C> predecessor, Node<A,B,C> successor) {
        Node<A,B,C> newest = new Node<>(id, name, score);
        newest.setPrev(predecessor);
        newest.setNext(successor);
        predecessor.setNext(newest);
        successor.setPrev(newest);
        size++;
        }
    public void addFirst(A id, B name, C score) {
        addBetween(id, name,score,header,header.getNext());
    }
    public void addLast(A id, B name, C score) {
        addBetween(id, name,score, trailer.getPrev(), trailer);
    }
    private Node<A,B,C> remove(Node<A,B,C> node){
        Node<A,B,C> predecessor = node.getPrev();
        Node<A,B,C> successor = node.getNext();
        predecessor.setNext(successor);
        successor.setPrev(predecessor);
        size--;
        return node;
    }
    public Node<A,B,C> removeFirst(){
        if(isEmpty())
            return null;
        return remove(header.getNext());
    }
    public Node<A,B,C> removeLast(){
        if(isEmpty())
            return null;
        return remove(trailer.getPrev());
    }
    public Node<A,B,C> search(A key){
        if(isEmpty())
            return null;
        Node<A,B,C> temp = header.getNext();
        do{
            if(temp.getStuID()== key)
                return temp;
            temp = temp.getNext();
            }while ( temp != null);
        return null;
    }
    
    public void addAfter(A key, A id, B name, C score) {
        if(isEmpty())
            return;
        Node<A,B,C> node = search(key);
        if(node == null)
       return;
        addBetween(id, name, score, node, node.getNext());
    }
    
    public void display() {
        if(isEmpty()) {
            System.out.println("Linked list is empty.");
            return;
        }
        Node<A,B,C> temp = header.getNext();
        do {
            temp.displayNode();
            temp = temp.getNext();
        }while(temp.getNext() !=null);
    }
    public void addBefore(A key, A id, B name, C score) {
        if(isEmpty())
            return;
        Node<A,B,C> node = search(key);
        if(node == null)
            return;
        addBetween( id, name, score, node.getPrev(), node);
    }
    public Node<A,B,C> removeAt( A key){
        if (isEmpty())
            return null;
        Node<A,B,C> node = search(key);
        if(node == null)
            return null;
        return remove(node);
    }
    public Node<A,B,C> update(A key, A nid, B nname, C nscore){
        if(isEmpty())
            return null;
        Node<A,B,C> node = search(key);
        if(node == null)
            return null;
        node.setStuID(nid);
        node.setStuName(nname);
        node.setStuScore(nscore);
        return node;
    }
}

```java
public class TestDLinkedList {
    public static void main(String[] args) {

        DlinkedList dl = new DlinkedList();
        System.out.println("\ndisplay:");
        dl.display();

        System.out.println("\naddFirst:");
        dl.addFirst("101", "Marty", 90.8);
        dl.addFirst("104", "Mark", 84.3);
        dl.addFirst("106", "Joan", 88.0);
        dl.addFirst("109", "Jay", 68.8);
        dl.display();

        System.out.println("\naddLast:");
        dl.addLast("103", "Michael", 90.8);
        dl.addLast("102", "Paul", 80.3);
        dl.addLast("105", "Steve", 90.8);
        dl.display();

        System.out.println("\nremoveFirst:");
        dl.removeFirst();
        dl.display();
        System.out.println("\nremoveFirst:");
        dl.removeFirst();
        dl.display();

        System.out.println("\nsearch:");
        Node temp = dl.search("101");
        if (temp != null) {
            System.out.println("Found a record:");
            temp.displayNode();
        }

        System.out.println("\nsearch:");
    }
}
```

### Explanation:

- **Purpose of the Code**: This Java program demonstrates operations on a doubly linked list, such as adding, removing, and searching nodes.
  
- **Key Operations**:
  - **addFirst()**: Adds a new node with a specific ID, name, and score to the start of the list.
  - **addLast()**: Adds a new node to the end of the list.
  - **removeFirst()**: Removes the node from the beginning of the list.
  - **search()**: Finds a node using a specific key (e.g., ID) and displays its details if found.
  - **display()**: Prints out the current state of the list.

- **Structure**: 
  - The main method starts by creating a new `DlinkedList` object.
  - Various operations are performed sequentially, with the state of the list displayed after key operations for better understanding.
  
- **Output**: The program provides output through console printing statements to show results after major steps are executed, aiding in debugging and comprehension.

This code is typically used in educational contexts to teach
Transcribed Image Text:```java public class TestDLinkedList { public static void main(String[] args) { DlinkedList dl = new DlinkedList(); System.out.println("\ndisplay:"); dl.display(); System.out.println("\naddFirst:"); dl.addFirst("101", "Marty", 90.8); dl.addFirst("104", "Mark", 84.3); dl.addFirst("106", "Joan", 88.0); dl.addFirst("109", "Jay", 68.8); dl.display(); System.out.println("\naddLast:"); dl.addLast("103", "Michael", 90.8); dl.addLast("102", "Paul", 80.3); dl.addLast("105", "Steve", 90.8); dl.display(); System.out.println("\nremoveFirst:"); dl.removeFirst(); dl.display(); System.out.println("\nremoveFirst:"); dl.removeFirst(); dl.display(); System.out.println("\nsearch:"); Node temp = dl.search("101"); if (temp != null) { System.out.println("Found a record:"); temp.displayNode(); } System.out.println("\nsearch:"); } } ``` ### Explanation: - **Purpose of the Code**: This Java program demonstrates operations on a doubly linked list, such as adding, removing, and searching nodes. - **Key Operations**: - **addFirst()**: Adds a new node with a specific ID, name, and score to the start of the list. - **addLast()**: Adds a new node to the end of the list. - **removeFirst()**: Removes the node from the beginning of the list. - **search()**: Finds a node using a specific key (e.g., ID) and displays its details if found. - **display()**: Prints out the current state of the list. - **Structure**: - The main method starts by creating a new `DlinkedList` object. - Various operations are performed sequentially, with the state of the list displayed after key operations for better understanding. - **Output**: The program provides output through console printing statements to show results after major steps are executed, aiding in debugging and comprehension. This code is typically used in educational contexts to teach
Here is the transcription of the provided code snippet suitable for an educational website:

```java
System.out.println("\nsearch:");
temp = dl.search("120");
if(temp!=null) {
    System.out.println("Found a record:");
    temp.displayNode();
}

System.out.println("\nupdate:");
System.out.println("\ndisplay:");
dl.display();
temp = dl.update("101","101","Mary.H",95.0);
System.out.println("\nupdated node:");

temp.displayNode();

System.out.println("\ndisplay:");
dl.display();

System.out.println("\nadd after:");
dl.addAfter("101", "130", "Ben", 74.3);
System.out.println("\ndisplay:");
dl.display();

System.out.println("\nadd before:");
dl.addBefore("101", "135", "Mike", 70.0);
System.out.println("\ndisplay:");
dl.display();

System.out.println("\nremove at:");
dl.removeAt("101");
System.out.println("\ndisplay:");
dl.display();
```

Explanation of the Code:

1. **Search Operation**:
   - The code first searches for a node with a specific identifier ("120"). If found, it reports and displays the node details.

2. **Update Operation**:
   - It updates a node with a new value ("Mary.H", 95.0) and then displays the updated node.

3. **Add After Operation**:
   - A new node with details ("130", "Ben", 74.3) is added after the node with identifier "101".

4. **Add Before Operation**:
   - A new node with details ("135", "Mike", 70.0) is added before the node with identifier "101".

5. **Remove Operation**:
   - It removes the node at the position with identifier "101".

6. **Display Operations**:
   - After each update, addition, or removal action, the list is displayed to show current nodes and their order.

This script is part of a management system for a data structure, likely a doubly-linked list, enabling dynamic data manipulation through different operations (search, update, add, and remove). The console print statements are used to indicate the operations being performed and display the results after each operation.
Transcribed Image Text:Here is the transcription of the provided code snippet suitable for an educational website: ```java System.out.println("\nsearch:"); temp = dl.search("120"); if(temp!=null) { System.out.println("Found a record:"); temp.displayNode(); } System.out.println("\nupdate:"); System.out.println("\ndisplay:"); dl.display(); temp = dl.update("101","101","Mary.H",95.0); System.out.println("\nupdated node:"); temp.displayNode(); System.out.println("\ndisplay:"); dl.display(); System.out.println("\nadd after:"); dl.addAfter("101", "130", "Ben", 74.3); System.out.println("\ndisplay:"); dl.display(); System.out.println("\nadd before:"); dl.addBefore("101", "135", "Mike", 70.0); System.out.println("\ndisplay:"); dl.display(); System.out.println("\nremove at:"); dl.removeAt("101"); System.out.println("\ndisplay:"); dl.display(); ``` Explanation of the Code: 1. **Search Operation**: - The code first searches for a node with a specific identifier ("120"). If found, it reports and displays the node details. 2. **Update Operation**: - It updates a node with a new value ("Mary.H", 95.0) and then displays the updated node. 3. **Add After Operation**: - A new node with details ("130", "Ben", 74.3) is added after the node with identifier "101". 4. **Add Before Operation**: - A new node with details ("135", "Mike", 70.0) is added before the node with identifier "101". 5. **Remove Operation**: - It removes the node at the position with identifier "101". 6. **Display Operations**: - After each update, addition, or removal action, the list is displayed to show current nodes and their order. This script is part of a management system for a data structure, likely a doubly-linked list, enabling dynamic data manipulation through different operations (search, update, add, and remove). The console print statements are used to indicate the operations being performed and display the results after each operation.
Expert Solution
Step 1: Part1:

Great! Let's add the new methods to the `DLinkedList` class and test them. I've added the `count` method as well. Check the modifications below:


steps

Step by step

Solved in 3 steps

Blurred answer