Question

(Java) Linked List. Please explain this with one example or code to understand Linked List: ****Implement...

(Java) Linked List. Please explain this with one example or code to understand Linked List:

****Implement the method contains that check if a node is contained in a Linked List.*****

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
import java.util.LinkedList;
//Class My linked list
public class MyLinkedList
{
   //Creates a linked list of type string
    LinkedList<String> list = new LinkedList<String>();
   //Method to add a node to linked list
   void addItem(String s)
   {
       //Adds a string as a node to the linked list
       list.add(s);
   }
   //Checks whether the searched node is present in the Linked list or not
   void searchItem(String se)
   {
       //Iterates through the list
   for (String s : list)
       {
           //Compares the Searched string(i.e., se) with the retrived node from the linked list
           if (s.contains(se))
           {
               //If present displays the Node contents, searched data with searched status as Yes
               System.out.println("Yes," + s + " Contains " + se);
           }
           else
           {
               //If not present displays the Node contents, searched data with searched status as No
               System.out.println("No," + s + " Does Not Contain " + se);
           }
   }
   }
   //Menu for user
   void menu()
   {
       System.out.println("A - For Insert ");
       System.out.println("S - For Search ");
       System.out.println("E - Exit ");
   }

   public static void main(String[] args)
   {
       //Creates an object of the My linked list class
       MyLinkedList ml = new MyLinkedList();
       //Scanner class object to accept data
       Scanner sc = new Scanner(System.in);
       char ch = ' ';
       String s = " ";
       String se = " ";
       //Loops till 'E' Entered by the user
       do
       {
           //Displays the menu
           ml.menu();
           //Accepts choice
           System.out.println("Enter your choice: ");
               ch = sc.next().charAt(0);
           //Flushes the buffer
           sc.nextLine();
           //Operation as per the user choice entered by the user
           switch(ch)
           {
               //To add a string type node
               case 'A':
               case 'a':
                   System.out.println("Enter a string to Add: ");
                   s = sc.nextLine();
                   ml.addItem(s);
               break;
               //Search a string node
               case 'S':
               case 's':
                   System.out.println("Enter a string to search: ");
                   se = sc.nextLine();
                   ml.searchItem(se);
               break;
               //Exit
               case 'E':
               case 'e':
                   System.exit(0);
               default:
                   System.out.println("Wrong Choice: ");
           }
       }while(true);
   }
}

Output:

A - For Insert
S - For Search
E - Exit
Enter your choice:
a
Enter a string to Add:
This is demo
A - For Insert
S - For Search
E - Exit
Enter your choice:
s
Enter a string to search:
is
Yes,This is demo Contains is
A - For Insert
S - For Search
E - Exit
Enter your choice:
s
Enter a string to search:
do
No,This is demo Does Not Contain do
A - For Insert
S - For Search
E - Exit
Enter your choice:
A
Enter a string to Add:
Check this string.
A - For Insert
S - For Search
E - Exit
Enter your choice:
S
Enter a string to search:
check
No,This is demo Does Not Contain check
No,Check this string. Does Not Contain check
A - For Insert
S - For Search
E - Exit
Enter your choice:
s
Enter a string to search:
Check
No,This is demo Does Not Contain Check
Yes,Check this string. Contains Check
A - For Insert
S - For Search
E - Exit
Enter your choice:
e

Add a comment
Know the answer?
Add Answer to:
(Java) Linked List. Please explain this with one example or code to understand Linked List: ****Implement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • how to implement a linked list object using only singly linked list toolkit. Then implement a...

    how to implement a linked list object using only singly linked list toolkit. Then implement a FREQUENCY function to count the ovcurrence of each element in the list. task#1: Add = operator to node: implement the assignment operator for the node such that setting a node = overwrites the value in the node with the value. task#2:Linked List class implement the methods of linked list. insert search and locate remove node* operator [] task#3: Implement the Frequency

  • (Java) This is a delete method for a linked list, add a system print or something...

    (Java) This is a delete method for a linked list, add a system print or something when the item I want to delete isn’t in the linked list This is the method I want to use so please help me without writing a different code but instead add upon this code public void Delete(int item) f if (IsEmpty) ) Node 1-headNode; Node m headNode.link; while ((m.datum!- item)&& (m endNode)) f 1 01 m m.link; 1- 1.link İf (m.datum..item) { 1.link-m.link;

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that...

    In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that traverses the list and prints the following. Do not forget to consider the case where the list is empty. The "LLNode" class is given: public class LLNode { protected T info; protected LLNode link; public LLNode(T info) { this.info = info; link = null; } public void setInfo(T info) { this.info = info; } public T getInfo() { return info; } public void setLink(LLNode...

  • please explain how to code using java -. Implement the ListNode and LinkedIntList as we did...

    please explain how to code using java -. Implement the ListNode and LinkedIntList as we did in class (you can add other methods if needed). You can reference the coxle in powerpoint slides of LinkedList and your textbook. 2. Implement a LinkedIntList class with the following public operations. a. add(int value) - Adds the given value to the end of the list b.get(int index) - Retums value in list at given index. C.add( int index, int value) - Inserts the...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • JAVA Program: reverse a linked list and find the middle node in the linked list. inFile...

    JAVA Program: reverse a linked list and find the middle node in the linked list. inFile (use argv[1]): A text file contains a list of English words (strings), giving below outFile1 (use argv[2])a text file includes       i) The completed sorted linked list, in ascending order. //With caption indicating you are printing the original sorted list       ii) The reversed linked list. //With caption indicating you are printing the reversed sorted list       outFile2( use argv[3]): All debugging outputs.                        ...

  • Java - Write an insertBefore method for a List implemented as a Linked List that contains...

    Java - Write an insertBefore method for a List implemented as a Linked List that contains two dummy nodes. - Write an insertAfter method for a List implemented as a Linked List that contains two dummy nodes.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT