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===========");
}
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps
- In python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds). It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list. The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it). As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class…arrow_forwardAdd more methods to the singly linked list class then test them• search(e) // Return one node with 3 values (stuID, stuName, stuScore) which matches agiven 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())…arrow_forwardJAVA please Given main() in the ShoppingList class, define an insertAtEnd() method in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list. The output is: Kale Lettuce Carrots Peanuts Code provided in the assignment ItemNode.java:arrow_forward
- Java Programming language Help please.arrow_forwardSorting Create a MyLinkedList class with inner Node class, data fields, and the insert(element) method. Implement a toString method to return all nodes in MyLinkedList. Implement a recursive sorting and a non-recursive sorting method.arrow_forwardWhat is the benefit of having a link-based implementation of the List that also tracks a reference to the last Node in the chain? Select one: a. It requires more pointes to be adjusted when performing the basic List operations b. It makes adding a new entry to a specified position an O(1) operation c. It makes the remove operation an O(1) operation d. It makes adding a new entry to the back of the List an O(1) operationarrow_forward
- Given main.py and a PersonNode class, complete the PersonList class by writing find_first() and find_last() methods at the end of the PersonList.py file. The find_first() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the find_last() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is not found, None should be returned. The program will replace the name value of each found node with a new name. Ex. If the input is: Alex 23 Tom 41 Michelle 34 Vicky 23 -1 23 Connor 34 Michela main.py(can not edit) from PersonNode import PersonNodefrom PersonList import PersonList if __name__ == "__main__": person_list = PersonList() # Read input lines until encountering '-1' input_line = input() while input_line != '-1': split_input = input_line.split(' ') name = split_input[0] age =…arrow_forwardGiven the source code of linked List, answer the below questions(image): A. Fill out the method printList that print all the values of the linkedList: Draw the linked list. public void printList() { } // End of print method B. Write the lines to insert 10 at the end of the linked list. You must draw the final linked List. Notice that you can’t use second or third nodes. Feel free to define a new node. Assume you have only a head node C. Write the lines to delete node 2. You must draw the final linked list. Notice that you can’t use second or third node. Feel free to define a new node. Assume you have only a head nodearrow_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