dd 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 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
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===========");
}
}
}
![⇒ eclipse-workspace - WhereAreMyFiles/src/Node.java - Eclipse IDE
File Edit Source Refactor Navigate Search Project Run Window Help
2
25
1
2
Lecturel.java
RO
9
public class Node<A,B,C> []
privata & stuID;
privata BatuName;
privata e stuScore;
private Nade<A, B, C> next;
$10
11
12
13
14
$150
16
17
18
19 public B getStuName() [
20
return this.stuName;
1
46
47
48
public Node (A id, B name, C score) [
this.stuID = id;
this.stuNama-name;
1
StringBuild....
1
21 1
22 public egetstuscore () {
23
1
1
public A getStuID() {
return this.stuID;
25
26
27 public Node<A, B, C getNext() [
28
return this.next;
29
$30 1
31
32
33
this.stuscore score
this.next=null;
public void setStuID (A id) {
this.stuID = id;
1
return this.stuScore;
35 public void setStuName (B name) [
36
this.stuName - name;
37 1
38 public void setStuScore (C score) [
39
this.stuscore - score;
40
41 public void setNext (Node<A,B,C n) [
42
this.next na
843
1
44 public void displayNode () {
75°F
Sunny
#
Ⓒ
Node.java X Singlelista...
¶¶¶
System.out.println("ID-"+this.stuID+ ", Name-"+this.stuName+", Score-"+this.stuScore);
樱画]
Q Search
node.java
▾▾
DoublyLinke...
Writable
a
TestDoublyLi...
Slinked List....
Smart Insert
TestDoublyLi...
47:3:919
R
TestSlinkedL...
"88
Q
8:41 PM
10/5/2023
X
* * * + @ 隐旦
8
%
鼎
2
@
@](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2ae76079-938b-4b5d-8ceb-1dfbc10f88ce%2F1beb893b-4d4f-4eaf-9128-d69ede8cb4ff%2Fhechao_processed.png&w=3840&q=75)
![⇒ eclipse-workspace - WhereAreMyFiles/src/TestSlinked List.java - Eclipse IDE
File Edit Source Refactor Navigate Search Project Run Window Help
-2
20
Node.java
2
Lecturel.java
음... 1
30
10
11
12
13
14
15
16
17
2018
19
20
21
22
23
24
25
26
27
28
public class TestSlinkedList {
}
75°F
Sunny
StringBuild....
}
public static void main(String [] args) {
SlinkedList s1= new SlinkedList ();
sl.addFirst ("101","mary", 98.9);
sl.addFirst ("102","mark",90.0);
sl.addFirst ("106", "jark", 88.2);
sl.addFirst ("109","joan", 89.2);
sl.display();
Node temp = sl.search ("101");
if (temp!=null) {
}
System.out.println("got it!");
temp.displayNode ();
temp = s1.search("108");
if (temp! -null) {
Singlelista...
System.out.println("got it!");
temp.displayNode ();
■
}
temp = sl.update("101","100","marry H",99.0);
temp.displayNode ();
樱画]
Q Search
node.java
▾▾
DoublyLinke...
Writable
a
TestDoublyLi...
Slinked List....
Smart Insert
TestDoublyLi...
23:55: 643
R
TestSlinked L... X
>>
"88
4x
a
X
8:40 PM
10/5/2023
AC
2* en
鼎](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2ae76079-938b-4b5d-8ceb-1dfbc10f88ce%2F1beb893b-4d4f-4eaf-9128-d69ede8cb4ff%2F2zmh9q_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 3 steps
