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()); } }
These are the errors I get when I compile this program
/main.java:1: error: class, interface, or enum expected LinkedList.java ^ /main.java:4: error: class, interface, or enum expected public int size() ^ /main.java:7: error: class, interface, or enum expected int HopperIndex=0; ^ /main.java:8: error: class, interface, or enum expected if(first==null) ^ /main.java:10: error: class, interface, or enum expected else ^ /main.java:18: error: class, interface, or enum expected HopperIndex++; ^ /main.java:19: error: class, interface, or enum expected } ^ /main.java:22: error: class, interface, or enum expected } ^ 8 errors
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