Question: Q .Add a method size to our implementation of the LinkedList class that computes the number of elements in the list by following links and

Q.Add a method size to our implementation of the LinkedList class that computes the number of elements in the list by following links and counting the elements until the end of the list is reached.?

Plz send me a working porgram

Not Working..!!!

LinkedList.java // Compute the size of the link list by following the links //and counting the elements public int size() { Node Hopper=first; int HopperIndex=0; if(first==null) return 0; else { while(Hopper!=null) { // it will move on until it reaches the end. //Also the size of HopperIndex will increase //by 1 until loop not terminates. Hopper=Hopper.next; HopperIndex++; } } return HopperIndex; } Main.java public class main { public static void main(String[] args) { //Declaring linked list and iterator LinkedList List = new LinkedList(); ListIterator listIterator = List.listIterator(); int Index=0; System.out.println("Program starts"); for(Index=0;Index<3;Index++) { //Variable to store list element Object Element= new Object(); //inserting an element listIterator.add(Element); } //display the size of linked list. System.out.println("The size of the list is : " +List.size()); for(Index=0;Index<5;Index++) { //Variable to store list element Object Element= new Object(); //inserting an element listIterator.add(Element); } //display the size of linked list. System.out.println("The size of the list is : " +List.size()); for(Index=0;Index<2;Index++) { //Removing an element List.removeFirst(); } //display the size of linked list. System.out.println("The size of the list is : " +List.size()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!