Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this exercise, you will write two methods for LinkedList class. You need to

ID: 3859232 • Letter: F

Question

For this exercise, you will write two methods for LinkedList class. You need to download Node. java, LinkedList. java. These were discussed in the lectures. You will add these two methods to the Linkedlist class and then create a demo program and to test these two methods. a) Write a method called countWord. It takes in a String returns the number of times the String is contained in the Linkedlist. public int countword (String word) b) Write a method called lastIndexOf. It takes in a String and returns the index of the last occurrence in the list (assume the first element (front) is at index 0). It returns -1 if the item is not in the list. public int lastIndexOf (String word) c) Write a method called remove which takes in a String as a parameter that removes every occurrence in the Linkedlist (note, this is a void method). public void remove (String word)

Explanation / Answer

Linkedlist.java

package venkanna;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class LinkedList2 {

   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
       int opt;
       String str;
       LinkedList<String> al=new LinkedList<String>();
       System.out.println("----------------------------");
       System.out.println("**Implementaion on Linked List**");
       System.out.println("Enter How many No of String You Want to Store in Linked List");
       int length=sc.nextInt();
       System.out.println("Enter "+length+ " Strings");
       for(int i=0;i<length;i++)
       {
           al.add(sc.next());
       }
       while(true)
       {
           System.out.println("----------------------------");
           System.out.println("******MENU*************");
           System.out.println("1. -count Strings in list:");
           System.out.println("2. -Lastindex of LinkedList:");
           System.out.println("3. -Remove Every occurence of String in a Linked List");
           System.out.println("4. -Display");
           System.out.println("5. -Exit the Menu");
           System.out.println("Select Any Option");
           System.out.println("Enter String to Search");
           opt=sc.nextInt();
           switch(opt)
           {
           case 1:
               System.out.println("Enter String:");
               str=sc.next();
               countWord(al,str);
               break;
           case 2:
               System.out.println("Enter String:");
               str=sc.next();
               Lastindexof(al,str);
               break;
           case 3:
               System.out.println("Enter String:");
               str=sc.next();
               remove(al,str);
               break;
           case 4:
               System.out.println("The Data in a list is:");
               Iterator<String> itr=al.iterator();
               while(itr.hasNext())
               {
               System.out.println(itr.next());
               }
               break;
           case 5:
               System.exit(0);
               default:
                   System.out.println("Invalid OPtion:try next");
           }
       }
         
   }

   private static void remove(LinkedList<String> al, String str)
   {
       al.removeAll(Collections.singleton(str));
       System.out.println("After Removing All Occurences of String in List is");
       Iterator<String> itr=al.iterator();
       while(itr.hasNext())
       {
       System.out.println(itr.next());
       }
   }

   private static void Lastindexof(LinkedList<String> al, String str)
   {
       int index;
       index=al.lastIndexOf(str);
       System.out.println("The Last Occurence of String in LinkedList is"+index);
   }
   private static void countWord(LinkedList<String> al, String str)
   {
       int count=Collections.frequency(al, str);
       System.out.println("The String is "+count+" Times Repeated in a Linked List");
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote