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

Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
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....
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
Add the operation Insert to the linkedListClass. An Insert operation inserts a new item after a...
Add the operation Insert to the linkedListClass. An Insert operation inserts a new item after a given key in the linked list. The method headline can be void Insert(int item, int key), where the first parameter is the new item, and the second parameter is the key of the item before the new item.
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...
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 program that prompts the user to insert an item into a stack, delete an...
Write a program that prompts the user to insert an item into a stack, delete an item from the stack, or display all the items in the stack. Assume that the stack maximum capacity is 10. Here is sample run . Stack operation menu: 1. Insert 2. Delete 3. Display 4. Quit
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.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT