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; } }
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2ae76079-938b-4b5d-8ceb-1dfbc10f88ce%2F14eddcc1-5645-4345-b485-453c2b1c0c76%2Frbs0le7_processed.png&w=3840&q=75)


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 step
Solved in 3 steps
