Question

In: Computer Science

JAVA Write a class to implement a list with the ability to insert a new item...

JAVA

Write a class to implement a list with the ability to insert a new item at any location within the list, remove an item, and search for an item. The list should use a linked list implementation. This implementation should make use of a dummy node at the head of the list and should have explict references head and previous. Add a method to the list that inserts elements in acsending order assuming the list is already sorted before each insert.

Solutions

Expert Solution

Hi, Please find the solution and rate the answer.

import java.util.Random;

public class Main {

    public static void main(String[] args) {
        List list = new List();
        Random random = new Random();
        list.insertAsc(new Node(random.nextInt(1000)));
        list.insertAsc(new Node(random.nextInt(1000)));
        list.insertAsc(new Node(random.nextInt(1000)));
        list.insertAsc(new Node(random.nextInt(1000)));
        list.insertAsc(new Node(random.nextInt(1000)));
        list.insertAsc(new Node(random.nextInt(1000)));
        list.insertAsc(new Node(random.nextInt(1000)));
        list.insertAsc(new Node(random.nextInt(1000)));
        list.insertAsc(new Node(random.nextInt(1000)));
        System.out.println("Before");
        list.printList();
        System.out.println("After");
        int x = random.nextInt(10000);
        System.out.println("Insert "+x+" at location 5");
        list.insertAt(new Node(x),5);
        list.printList();
        int x1 = random.nextInt(10000);
        System.out.println("Insert "+x1+" at location 5");
        list.insertAt(new Node(x1),5);
//        list.insertAt(new Node(random.nextInt(10000)),5);

        list.printList();
        list.insertAsc(new Node(100));
        Node search = list.search(new Node(100));
        System.out.println();
        System.out.println(search ==null?"Not Found":search+" Found");
    }
}
public class Node {
    private int x;
    public Node previous;
    public Node next;

    public Node(int x) {
        this.x = x;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    @Override
    public String toString() {
        return "Node{" +
                "x=" + x +
                '}';
    }
}
public class List {
    Node head = new Node(-100);//dummy node

    public void insertAsc(Node x) {
        Node temp = head;
        if (head.next == null) {
            head.next = x;
            x.previous = head;
            x.next = null;
            return;
        }
        while (temp.next!=null && temp.getX() < x.getX()) {
            temp = temp.next;
        }
        temp.previous.next = x;//realigning 4 pointer references
        x.next = temp;
        x.previous = temp.previous;
        temp.previous = x;
    }

    /**
     * @param x
     * @param loc if the loc is more than the no of elements it will place the element in th last
     */
    public void insertAt(Node x, int loc) {
        Node temp = head;
        int i = 0;
        while (temp.next != null) {
            temp = temp.next;
            i++;
            if (i == loc) {
                break;
            }
        }
        if (i == loc) {
            temp = temp.previous;
        }
        temp.next.previous = x;
        x.previous = temp;
        x.next = temp.next;
        temp.next = x;


    }

    public void remove(Node x) {
        Node temp = head;
        while (temp.next != null) {
            if (temp.getX() == x.getX()) {
                break;
            }
            temp = temp.next;
        }
        Node tempp = temp;
        temp.previous.next = temp.next;//self explanatory
        temp.next.previous = temp.previous;
        temp.next = null;
        temp.previous = null;

    }

    public Node search(Node x) {
        Node temp = head;
        while (temp.next != null) {
            if (temp.getX() == x.getX()) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }

    public void printList() {
        Node temp = head;
        while (temp.next != null) {
            System.out.println(temp.next);
            temp = temp.next;

        }
    }

}

Sample output:


Related Solutions

Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
Start with the provided code for the class linkedListType. Be sure to implement search, insert, and...
Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. Note: the link pointer of the last element of the list is NULL. Test your new function in main. Submit a .zip of your entire project....
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT