Add more methods to the singly 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 three codes below two in pictures one,typed out. public class SlinkedList { private Node head; private Node tail; private int size; public SlinkedList(){ head=null; tail=null; size=0; } public int getSize(){ return size; } public boolean isEmpty(){ return size == 0; } public A getFirstStuId(){ if(isEmpty()) return null; return (A) head.getStuID(); } public B getFirstStuName(){ if(isEmpty()) return null; return (B) head.getStuName(); } public C getFirstStuScore(){ if(isEmpty()) return null; return (C) head.getStuScore(); } public void addFirst(A id, B name, C score){ Node newest = new Node<>(id, name,score); if(isEmpty()) { head=newest; tail=newest; }else{ newest.setNext(head); head=newest; } size++; } public void addLast(A id, B name, C score) { Node newest=new Node<>(id, name, score); if(isEmpty()) { tail=newest; head=newest; }else { tail.setNext(newest); tail=newest; } size++; } public A removeFirst() { if(isEmpty()) return null; A firstID = (A) head.getStuID(); head=head.getNext(); size--; if(getSize()==0) tail=null; return firstID; } public Node search(A id){ if(isEmpty()) { System.out.println("Linked list is empty"); return null; } Node temp= head; do { if(temp.getStuID()==id) return temp; temp=temp.getNext(); }while(temp!=null); System.out.println("can not find node(id"+id+")in linked list"); return null; } public Node update(A key, A nid, B nname, C nscore){ Node updateNode= search(key); if(updateNode==null) return null; updateNode.setStuID(nid); updateNode.setStuName(nname); updateNode.setStuScore(nscore); return updateNode; } public void display() { if (isEmpty()){ System.out.println("Singly linked list is empty."); } else { Node temp = head; System.out.println("=========== beginning of lists=========="); do{ temp.displayNode(); temp = temp.getNext(); }while(temp != null); System.out.println("===========Ending of lists==========="); } } }
Add more methods to the singly 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
three codes below two in pictures one,typed out.
public class SlinkedList<A,B,C> {
private Node head;
private Node tail;
private int size;
public SlinkedList(){
head=null;
tail=null;
size=0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public A getFirstStuId(){
if(isEmpty())
return null;
return (A) head.getStuID();
}
public B getFirstStuName(){
if(isEmpty())
return null;
return (B) head.getStuName();
}
public C getFirstStuScore(){
if(isEmpty())
return null;
return (C) head.getStuScore();
}
public void addFirst(A id, B name, C score){
Node<A,B,C> newest = new Node<>(id, name,score);
if(isEmpty()) {
head=newest;
tail=newest;
}else{
newest.setNext(head);
head=newest;
}
size++;
}
public void addLast(A id, B name, C score) {
Node<A,B,C> newest=new Node<>(id, name, score);
if(isEmpty()) {
tail=newest;
head=newest;
}else {
tail.setNext(newest);
tail=newest;
}
size++;
}
public A removeFirst() {
if(isEmpty())
return null;
A firstID = (A) head.getStuID();
head=head.getNext();
size--;
if(getSize()==0)
tail=null;
return firstID;
}
public Node<A,B,C> search(A id){
if(isEmpty()) {
System.out.println("Linked list is empty");
return null;
}
Node<A,B,C> temp= head;
do {
if(temp.getStuID()==id)
return temp;
temp=temp.getNext();
}while(temp!=null);
System.out.println("can not find node(id"+id+")in linked list");
return null;
}
public Node<A,B,C> update(A key, A nid, B nname, C nscore){
Node<A,B,C> updateNode= search(key);
if(updateNode==null)
return null;
updateNode.setStuID(nid);
updateNode.setStuName(nname);
updateNode.setStuScore(nscore);
return updateNode;
}
public void display() {
if (isEmpty()){
System.out.println("Singly linked list is empty.");
}
else {
Node<A,B,C> temp = head;
System.out.println("=========== beginning of lists==========");
do{
temp.displayNode();
temp = temp.getNext();
}while(temp != null);
System.out.println("===========Ending of lists===========");
}
}
}
Step by step
Solved in 3 steps with 1 images