Question
USING JAVA LANGUAGE 1 import java.util.*; 2 import java.io.*; 3 4 public class Node 5 { 6 int data; 7 public Node next; 8 9
USING JAVA LANGUAGE
1 import java.util.*; 2 import java.io.*; 3 4 public class Node 5 { 6 int data; 7 public Node next; 8 9 } 10 11 class SinglyLinkedList{ 12 private Node head; 13 14 15 public void insert(int data) { 16 Node newNode = new Node(); 17 newNode.data = data; 18 newNode.next = head; 19 head = newNode; 20 }a 21 22 23 public int getSize(){ 24 Node current = head; 25 int count=1; 26 while (current.next != null) { 27 current = current.next; 28 count++; 29 } 30 return count; 31 } 32 33 34 35 public boolean isEmpty(){ 36 return(head==null); 37 } 38 39 40 public int getFirst(){ 41 final Node f = head; 42 if (f == null) 43 throw new NoSuchElementException(); 44 return f.data; 45 46 } 47 public int getLast(){ 48 Node current = head; 49 while (current.next != null) { 50 current = current.next; 51 } 52 return current.data; 53 } 54 55 56 public void addFirst(int data){ 57 Node newNode = new Node(); 58 newNode.data = data; 59 newNode.next = head; 60 head = newNode; 61 } 62 63 public void addLast(int data){ 64 Node current = head; 65 while (current.next != null) { 66 current = current.next; 67 } 68 Node newNode = new Node(); 69 newNode.data = data; 70 current.next = newNode; 71 } 72 73 public int removeFirst(){ 74 Node temp = head; 75 head = head.next; 76 return temp.data; 77 } 78 79 public void displayList(){ 80 Node current = head; 81 while (current != null) { 82 System.out.println(current.data); 83 current = current.next; 84 } 85 System.out.println(); 86 }
4.2 Add more methods to the singly linked list class then test them (Extra 30 points): search(e) // Returns an element which matches a given key e. (6 points) addAfter(e, x) //Adds a new element x to a singly linked list after the element e. (6 points) . removeAt(e) //Removes a element e from a singly linked list. (6 points) count0//Return a number of nodes of list. (6 points) update(e)//Update the value of one elements of a singly linked list. (6 points)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started