Question

. [Tests knowledge of data structures] Create your own linked list (do not use any of...

. [Tests knowledge of data structures] Create your own linked list (do not use any of the classes provided in the Collections API). Implement the following two operations:

If you are using jdk1.4 or before:

void add(Object ob);

boolean find(Object ob);

String toString();

If you are using j2se5.0 and you know generic programming:

void add(T ob);

boolean find(T ob);

String toString()

The toString method should arrange the elements of the list in a comma-separated sequence, in the following format:

[elem0, elem1, elem2, …, elemN]

Test your linked list in a main method which does the following:

a. Creates an instance of your list and adds the following Strings to it:
“Straight”, “Bent”, “Equals”, “Well”, “Storm”

b. Uses your find function to search for the keys “Well” and “Strength”

c. Outputs both the input list and the search results to the consoleand output the results to the consoleby repeatedly using your add function to populate a new instance of your linked list with Strings, and then outputting to console the boolean result of searching for some String in this list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Linked list class
class LinkedList<T>{
    //Node class
    static class Node<T>{
        //Variable to store data of the node
        T data;
        //Variable to store next node
        Node next;
    }
    //head node
    Node head;

    //Methdo to add data to linked list
    public void add(T t){
        //If head is null, then make the new node as head
        if(head==null){
            head = new Node<T>();
            head.data = t;
            return;
        }
        //Traverse to end of the linked list
        Node<T> temp = head;
        while(temp.next!=null){
            temp = temp.next;
        }
        //Add a new node at the end of the linked list
        temp.next = new Node<T>();
        temp.next.data = t;
    }

    //Method to find the element in the linked list
    public boolean find(T obj){
        //Traverse through all the nodes in the linked list
        Node temp = head;
        while(temp!=null){
            //Check if data equals the node's data
            if(temp.data.equals(obj)){
                return true;
            }
            temp = temp.next;
        }
        return false;
    }

    //Method to print list of strings
    public String toString(){
        //If head is null, then return empty list
        if(head==null){
            return "[ ]";
        }
        String ans = "[";
        Node temp = head;
        //Traverse through the list and add to ans variable
        while(temp!=null){
            ans = ans+temp.data;
            if(temp.next!=null){
                ans=ans+",";
            }
            temp = temp.next;
        }
        return ans+"]";
    }
}

class Main{
    public static void main(String[] args) {
        LinkedList<String> ll = new LinkedList<>();
        System.out.println("LL is "+ll);
        System.out.println("Well found ? "+ll.find("Well"));
        System.out.println("Strength found ? "+ll.find("Strength"));
        ll.add("Straight");
        System.out.println("LL is "+ll);
        System.out.println("Well found ? "+ll.find("Well"));
        System.out.println("Strength found ? "+ll.find("Strength"));
        ll.add("Bent");
        System.out.println("LL is "+ll);
        System.out.println("Well found ? "+ll.find("Well"));
        System.out.println("Strength found ? "+ll.find("Strength"));
        ll.add("Equals");
        System.out.println("LL is "+ll);
        System.out.println("Well found ? "+ll.find("Well"));
        System.out.println("Strength found ? "+ll.find("Strength"));
        ll.add("Well");
        System.out.println("LL is "+ll);
        System.out.println("Well found ? "+ll.find("Well"));
        System.out.println("Strength found ? "+ll.find("Strength"));
        ll.add("Storm");
        System.out.println("LL is "+ll);
        System.out.println("Well found ? "+ll.find("Well"));
        System.out.println("Strength found ? "+ll.find("Strength"));

    }
}

OUTPUT :

Add a comment
Know the answer?
Add Answer to:
. [Tests knowledge of data structures] Create your own linked list (do not use any of...
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
  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • This class implements a doubly linked list in which the nodes are of the class DLNode....

    This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • 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...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • Create a linked list with the following features. A Node class that stores the data type...

    Create a linked list with the following features. A Node class that stores the data type of your choice. A constructor that creates a dummy header node. void display() -- A method for printing the list. void add(item) -- A method for adding a value to the beginning of the list void addEnd(item) -- A method of adding a value to the end of the list. bool contains(item) -- A method for finding a specified value in the list. int...

  • Implement the Unsorted, sorted and indexed lists using Linked List base structure. (LLNode class under support...

    Implement the Unsorted, sorted and indexed lists using Linked List base structure. (LLNode class under support package) Files required: 1. ListInterface.java (given) 2. LLUnsortedList.java (implements ListInterface.java) 3. LLSortedList.java (extends LLUnsortedList.java, implements ListInterface.java) 4. LLIndexedList.java (implements LLIndexedListInterface.java, extends LLUnsortedList.java) 5. Driver ** Maybe create LLIndexedListInterface.java file too even though it is not required file! I need the bolded Java Files (5) to be written separate  classes please! The interfaces can be implemented from below! Thank you zip all your files and submit....

  • Please please help me with this code please Purpose: To learn the basics of linked lists...

    Please please help me with this code please Purpose: To learn the basics of linked lists You are to implement 2 classes, Scene and Movie. Note: Please dont use inbuild linkedList methods and functions The Scene class has the following fields and methods: p rivate String event private Scene nextScene Public Scene(String event) - A constructor that creates a Scene object with the event set and nextScene set to null Public Scene(String event, Scene next) - A constructor that creates...

  • Needs Help with Java Programming language! Lights Camera Action Purpose: To learn the basics of linked...

    Needs Help with Java Programming language! Lights Camera Action Purpose: To learn the basics of linked lists You are to implement 2 classes, Scene and Movie. The Scene class has the following fields and methods: private String event private Scene nextScene Public Scene(String event) - A constructor that creates a Scene object with the event set and nextScene set to null Public Scene(String event, Scene next) - A constructor that creates a Scene object with the event and the nextScene...

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