Question

In JAVA, please. THANK YOU In this project you will create a generic linked list using...

In JAVA, please.
THANK YOU

In this project you will create a generic linked list using
Java Generics.



Description:

   Create a generic class called GenLinkedList.  GenLinkedList will use nodes
   that store a value of the generic type to store its contents.

   It should have the following methods.  The methods should 
   all operate on the object making the call (none are static).  

   Perform checking of the parameters and throw exceptions where 
   appropriate.

   The linked list should be singly-linked.

   It should not use sentinel nodes (empty header and tail nodes).  

   You should strive for an efficient implementation of each method.
   g.  swap
        receives two index positions as parameters, and swaps the nodes at
        these positions, provided both positions are within the current size

   h.  shift
        receives an integer as a parameter, and shifts the list forward or
        backward this number of nodes, provided it is within the current size


   k.  insertList
        receives a generic List (a Java List) and an index position as parameters, 
        and copies each value of the passed list into the current list starting
        at the index position, provided the index position does not exceed the size.
        For example, if list has a,b,c and another list having 1,2,3 is inserted at
        position 2, the list becomes a,b,1,2,3,c



   i.  removeMatching
        receives a value of the generic type as a parameter and removes all
        occurrences of this value from the list.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

HI, Please find the solution and upvote the answer.

public class Node<X> {
    private X value;
    public Node<X> next;

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public void setNext(Node<X> next) {
        this.next = next;
    }

    public X getValue() {
        return value;
    }

    public void setValue(X value) {
        this.value = value;
    }

    public Node(X val) {
        this.value = val;
        next = null;
    }
}
import java.util.Collections;
import java.util.List;

public class LinkedList<T> {
    private Node<T> head;

    public void printAll(){
        Node<T> temp= head;
        while(temp!=null){
            System.out.print(temp+", ");
            temp = temp.next;
        }
        System.out.println();
    }
    public int size(){
        int count = 0;
        Node<T> temp= head;
        while(temp!=null){
            temp=temp.next;
            count++;
        }
        return count;
    }
    void swap(int index1,int index2){
        int size = size();
        if(!(index1>=0 && index1< size && index2>=0 && index2< size)){
            return;//if indexes are not proper
        }
        Node<T> temp,temp1 = null,temp2=null;
        temp = head;
        for (int i = 0; i < size(); i++) {
            if(i==index1){
                temp1 = temp;
            }if(i==index2){
                temp2 = temp;
            }
            temp = temp.next;
        }
        T val = temp1.getValue();
        temp1.setValue(temp2.getValue());
        temp2.setValue(val);

    }

    void shift(int index){
        if(index<0 || index>size()){
            return;
        }
        for (int i = index; i < size(); i++) {
            Node<T> del = delete(i);
            addBeginning(del);
        }
    }
    void insertList(List<T> list, int index){
        int k=0;
        int size = size();
        Collections.reverse(list);
        for (int i = index; i < size && k<list.size(); i++) {
            insert(new Node<T>(list.get(k++)),index);
        }
    }

    void addBeginning(Node<T> node){
        if(node!=null) {
            node.next = head;
            head = node;
        }
    }
    void add(Node<T> node){
        node.next=null;
        if(head==null){
            head=node;
            return;
        }
        Node<T> temp = head;
        while(temp.next!=null){
            temp = temp.next;
        }
        temp.next = node;

    }
    void removeMatching(Node<T> remove){
        Node<T> temp = head;
        int i=0;
        LinkedList<Integer> ints = new LinkedList<>();
        while(temp!=null){//this loop adds all occurrences
            if(temp.getValue().equals(remove.getValue())){
                ints.add(new Node<>(i));
            }
            temp = temp.next;//moving pointers
            i++;
        }
        for (int j = 0; j < ints.size(); j++) {//this loop deletes them
            delete(ints.get(j).getValue());
        }
    }
    Node<T> get(int index){
        if(head==null){
            return null;
        }
        Node temp = head;
        int i=0;
        while(temp.next!=null && index!=i++){
            temp=temp.next;
        }
        if(i-1==index || i==index ){
            return new Node(temp.getValue());
        }
        else
            return null;//index not present
    }

    Node<T> delete(int index){
        if(index==0){
            Node<T> temp = head;
            head = head.next;
            temp.next = null;
            return temp;
        }
        Node<T> temp = head;
        Node<T> tempNext = head.next;
        int i=1;
        while(tempNext!=null && i++!=index){
            temp = temp.next;
            tempNext = tempNext.next;
        }
        temp.next = tempNext.next;//temp is pointing to index-1 node
        tempNext.next = null;
        return tempNext;

    }

    void set(Node<T> value,int index){
        value.next=null;
        value.next=null;
        if(head==null){
            head = value;
            head.next=null;
            return;
        }
        if(index==0){
            head.setValue(value.getValue());
            return;
        }
        Node temp = head;
        int i=0;
        while(temp.next!=null && index!=i++){
            temp=temp.next;
        }
        if(head!=temp && index!=0)
            temp.setValue(value.getValue());
    }

    void insert(Node<T> value,int index){
        value.next=null;
        if(head==null){
            head = value;
            head.next=null;
            return;
        }
        if(index==0){
            value.next=head;
            head=value;
        }
        Node<T> temp = head;
        Node<T> tempNext = head.next;
        int i=0;
        while(tempNext!=null && index!=i++){
            temp=temp.next;
            tempNext =  tempNext.next;
        }
        value.next = temp.next;
        temp.next=value;
    }

}
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        Node<Character> a = new Node<>('a');
        Node<Character> b = new Node<>('b');
        Node<Character> c = new Node<>('c');
        Node<Character> d = new Node<>('d');
        Node<Character> e = new Node<>('e');
        Node<Character> f = new Node<>('f');


        Node<Character> one = new Node<>('1');
        Node<Character> two = new Node<>('2');
        Node<Character> three = new Node<>('3');
        Node<Character> four = new Node<>('4');
        Node<Character> five = new Node<>('5');
        LinkedList<Character> list = new LinkedList<>();


        list.add(a);
        list.add(b);
        list.add(c);
        list.add(d);
        list.add(e);
        list.add(f);
        System.out.println("List after adding");
        list.printAll();
        System.out.println();
        System.out.println("List after swap 0 with 5");
        list.swap(0,5);
        list.printAll();
        System.out.println();
        System.out.println("List after shifting 4");
        list.shift(4);
        list.printAll();
        System.out.println();
        System.out.println("List after inserting another list");
        list.insertList(Arrays.asList('t','u','v'),3);
        list.printAll();
        System.out.println("After removing node 't'");
        list.removeMatching(new Node<Character>('t'));
        list.printAll();;
    }
}

Some variables are unused.Feel free to use them in Main.java..

Sample output:

Add a comment
Know the answer?
Add Answer to:
In JAVA, please. THANK YOU In this project you will create a generic linked list using...
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
  • Generic Linked Lists ( Program should be written in Java language). Modify the linked list class...

    Generic Linked Lists ( Program should be written in Java language). Modify the linked list class presented in this chapter so it works with generic types. Add the following methods drawn from the java.util.List interface: -void clear(): remove all elements from the list. -E get(int index): return the element at position index in the list. -E set(int index, E element): replace the element at the specified position with the specified element and return the previous element. Test your generic linked...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • Write a Java program to work with a generic list ADT using a fixed size array,...

    Write a Java program to work with a generic list ADT using a fixed size array, not ArrayList. Create the interface ListInterface with the following methods a) add(newEntry): Adds a new entry to the end of the list. b) add(newPosition, newEntry): Adds a new entry to the list at a given position. c) remove(givenPosition): Removes the entry at a given position from the list. d) clear( ): Removes all entries from the list . e) replace(givenPosition, newEntry): Replaces the entry...

  • Linked List in Java The data node should be modeled somewhat like this: class node{ int...

    Linked List in Java The data node should be modeled somewhat like this: class node{ int node iNum; node next; } Write a program that creates a linked list and loads it with the numbers 0 to 9. Start with an empty list and then use a "for loop" to fill it. Create a linked list class, a node class, etc. Routines like makeNode and findTail should be methods in the linked list class. Create a showList function to display...

  • Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link cl...

    Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link class by writing methods described below. Do not use loops, or create any more methods (other than those specified), class or instance variables. public class Link private Link next; /null if this is the last link private int value; public Link(Link n, int v) nextn valuev; Do not use loops, or create any more methods (other than those specified), class or...

  • Using an appropriate definition of ListNode, design a simple linked list class called StringList with the...

    Using an appropriate definition of ListNode, design a simple linked list class called StringList with the following member functions: void add (std::string); int positionOf (std::string); bool setNodeVal(int, std::string); std::vector<std::string> getAsVector(); a default constructor a copy constructor a destructor The add() function adds a new node containing the value of the parameter to the end of the list. The positionOf() function returns the (zero-based) position in the list for the first occurrence of the parameter in the list, or -1 if...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • i need to create methods that: append_element(self, val) This method should increase the size of the...

    i need to create methods that: append_element(self, val) This method should increase the size of the list by one, adding the specified value in the new tail position. This is the only way to add a value as the tail. insert_element_at(self, val, index) If the provided index identifies a valid zero-based position within the list, then insert the specified value at that position, increasing the length by one. This method can be used to insert at the head of a...

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