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