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;
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](https://content.bartleby.com/qna-images/question/2ae76079-938b-4b5d-8ceb-1dfbc10f88ce/14eddcc1-5645-4345-b485-453c2b1c0c76/rbs0le7_thumbnail.png)


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:
Step by stepSolved in 3 steps

- Implement 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_forwardsee the image and answer the 2 questions. Thanksarrow_forward8. Write down the insertBefore method which inserts a new element in the list before the node containing the given element. The method takes as parameters a dummy headed doubly linked circular list, the element existing in the list and new element to be added. public void insertBefore (Node head, Object elem, Object newElement) { //to do OR def insertBefore (head, elem, newElement): pass insertBefore (head, 3, 50) Sample Input Sample Output Ox21 2 223 240 O x21 2 22 50 ² 3 2 4 0arrow_forward