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;
}
}
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