Answer to Question #256923 in Java | JSP | JSF for Ntokozo

Question #256923

DESIGN, ANALYSE AND CODE any method for the GENERIC MyLinkedList class that will manipulate the linked list. You can decide yourself what it should be following the specification below:

1. Purpose: The method must make logical sense – it should be of some purpose to somebody.

You should describe in the text who will use the method for which purpose.

2. Clearly explain the problem


1
Expert's answer
2021-10-27T00:26:34-0400
import java.io.*;

class node<K> {
    K data;
    node<K> next;
    node(K data)
    {
        this.data = data;
        this.next = null;
    }
}

class list<L> {
    node<L> head;
    private int length = 0;
    list(){
        this.head = null;
        }
    void add(L data)
    {
        node<L> temp = new node<>(data);
        if (this.head == null) {
            head = temp;
        }
        else {
            node<L> X = head;
            while (X.next != null) {
                X = X.next;
            }
            X.next = temp;
        }
        length++;
    }
    void add(int position, L data)
    {
        if (position > length + 1) {
            System.out.println("Position Unavailable in LikedList");
            return;
        }
        if (position == 1) {
            node<L> temp = head;
            head = new node<L>(data);
            head.next = temp;
            return;
        }
        node<L> temp = head;
        node<L> prev = new node<L>(null);
        while (position - 1 > 0) {
            prev = temp;
            temp = temp.next;
            position--;
        }
        prev.next = new node<L>(data);
        prev.next.next = temp;
    }
    void remove(L key)
    {
        node<L> prev = new node<>(null);
        prev.next = head;
        node<L> next = head.next;
        node<L> temp = head;
        boolean exists = false;
        if (head.data == key) {
            head = head.next;
            exists = true;
        }
        while (temp.next != null) {
            if (String.valueOf(temp.data).equals(
                    String.valueOf(key))) {
                prev.next = next;
                exists = true;
                break;
            }
            prev = temp;
            temp = temp.next;
            next = temp.next;
        }
        if (exists == false && String.valueOf(temp.data).equals(String.valueOf(key))) {
            prev.next = null;
            exists = true;
        }
        if(exists){
            length--;
        }
        else {
            System.out.println("Given value is not present in linked list");
        }
    }
    void clear()
    {
        head = null;
        length = 0;
    }
    boolean empty()
    {
        if (head == null) {
            return true;
        }
        return false;
    }
    int length() { return this.length; }
    // @Override
    public String toString()
    {
        String S = "{ ";
        node<L> X = head;
        if (X == null)
            return S + " }";
        while (X.next != null) {
            S += String.valueOf(X.data) + " -> ";
            X = X.next;
        }
        S += String.valueOf(X.data);
        return S + " }";
    }
}
public class GenericClasss {
    public static void main(String[] args)
    {
        list<Integer> li1 = new list<>();
        System.out.println("Create integer linked list 1:");
        li1.add(1);
        li1.add(2);
        li1.add(3);
        System.out.println("List 1 will be obtained after adding 1,2 and 3:");
        System.out.println(li1);
        li1.remove(200);
        System.out.println("Removing the element 2 :");
        System.out.println(li1);
        list<String> li2 = new list<>();
        System.out.println("\n linked list for the string 2:");
        li2.add("happy");
        li2.add("birthday");
        System.out.println("List 2, after adding the happy birthday :");
        System.out.println(li2);
        li2.add(2, "Generic classes");
        System.out.println("List 2, after adding gfg position at 2 :");
        System.out.println(li2);
        list<Float> li3 = new list<>();
        System.out.println("\n Float linked list created at the list 2");
        li3.add(2.2f);
        li3.add(5.4f);
        li3.add(3.9f);
        System.out.println("List 3 after adding 2.2, 5.4 and 3.9 :");
        System.out.println(li3);
        System.out.println("Clearing list3 :");
        li3.clear();
        System.out.println(li3);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS