Question

DATA STRUCTURES JAVA In the doubly linked list develop the following operators: 1. Clone without duplicates....

DATA STRUCTURES JAVA

In the doubly linked list develop the following operators:

1. Clone without duplicates. Clone a linked list by removing the duplicates.

public LinkedList cloneWithoutDuplicates( )

2. At each occurrence of element x in the list, if y is the element before x and z is the one after x, substitute y with x and z with y.

public void substitute(AnyType x, Comparator<AnyType> cmp)

3. Exchange elements in index1 and index2 with the following condition: if the element in index1 is larger than the one in index2 then swap, otherwise swap the elements in index1+1 and index2+1.

public void checkAndSwap(index1, index2)

4. Copy elements from index1 to index2, and paste them before index3 preserving the order. public void copyAndPaste(index1, index2, index3)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Comparator;

public class DoublyLinkedList {
    Node head;

    class Node {
        int data;
        Node prev;
        Node next;

        Node(int d) {
            data = d;
        }
    }

    public void push(int data) {
        Node newNode = new Node(data);
        newNode.next = head;
        newNode.prev = null;
        if (head != null)
            head.prev = newNode;
        head = newNode;
    }

    public Node insertAfter(Node prev_Node, int new_data) {
        if (prev_Node == null) {
            System.out.println("The given previous node cannot be null");
            return null;
        }
        Node new_node = new Node(new_data);
        new_node.next = prev_Node.next;
        prev_Node.next = new_node;
        new_node.prev = prev_Node;
        if (new_node.next != null)
            new_node.next.prev = new_node;
        return new_node;
    }


    int[] search(int x) {
        Node temp = head;  // Initialize current
        int count = 0;
        int[] arr = null;
        while (temp != null) {
            if (temp.data == x) {
                count++;
            }
            temp = temp.next;
        }
        arr = new int[count];
        count = 0;
        int i = 0;
        while (temp != null) {
            if (temp.data == x) {
                arr[count] = i;
                count++;
            }
            temp = temp.next;
            i++;
        }
        return arr;
    }

    boolean searchvalue(int x) {
        Node temp = head;  // Initialize current
        while (temp != null) {
            if (temp.data == x)
                return true;
            temp = temp.next;
        }
        return false;
    }

    public DoublyLinkedList cloneWithoutDuplicates() {
        DoublyLinkedList linkedList = new DoublyLinkedList();
        Node temp = head;
        while (temp != null) {
            if (!linkedList.searchvalue(temp.data))
                linkedList.push(temp.data);
            temp = temp.next;
        }
        return linkedList;

    }

    public void substitute(int x, Comparator<Integer> cmp) {
        int[] arr = search(x);
        /*System.out.println("x" + arr[0]);*/
        if (arr != null) {
            Node temp = head;
            int x1 = 0;
            int y1 = 0;
            int z1 = 0;
            for (int i = 0; i < arr.length; i++) {
                temp = head;
                int count = 0;
                while (temp != null) {
                    if (count == arr[i])
                        x1 = temp.data;
                    if (count == arr[i] - 1)
                        y1 = temp.data;
                    if (count == arr[i] + 1)
                        z1 = temp.data;
                    temp = temp.next;
                    count++;
                }
               /* System.out.println("x1" + x1);
                System.out.println("y1" + y1);
                System.out.println("z1" + z1);*/

                temp = head;
                count = 0;
                while (temp != null) {
                    if (count == (arr[i] - 1))
                        temp.data = x1;
                    else if (count == (arr[i] + 1))
                        temp.data = y1;
                    temp = temp.next;
                    count++;
                }

            }
        }


    }

    public void checkAndSwap(int index1, int index2) {
        int value1 = 0;
        int value2 = 0;
        int value3 = 0;
        int value4 = 0;
        Node temp = head;
        int count = 0;
        while (temp != null) {
            if (count == index1)
                value1 = temp.data;
            if (count == index2)
                value2 = temp.data;
            if (count == index1 + 1)
                value3 = temp.data;
            if (count == index2 + 1)
                value4 = temp.data;
            temp = temp.next;
            count++;
        }
        if (value1 > value2) {
            temp = head;
            count = 0;
            while (temp != null) {
                if (count == index1)
                    temp.data = value2;
                else if (count == index2)
                    temp.data = value1;
                temp = temp.next;
                count++;
            }
        } else {
            temp = head;
            count = 0;
            while (temp != null) {
                if (count == index1 + 1)
                    temp.data = value4;
                else if (count == index2 + 1)
                    temp.data = value3;
                temp = temp.next;
                count++;
            }

        }

    }
    

    public void printlist() {
        Node node = head;
        Node last = null;
        System.out.println("Traversal in forward Direction");
        while (node != null) {
            System.out.print(node.data + " ");
            last = node;
            node = node.next;
        }
        System.out.println();
        System.out.println("Traversal in reverse direction");
        while (last != null) {
            System.out.print(last.data + " ");
            last = last.prev;
        }
    }

    public static void main(String... args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.push(1);
        dll.push(3);
        dll.push(5);
        dll.push(6);
        dll.push(2);
        dll.push(6);
        dll.printlist();
        dll.checkAndSwap(1, 2);
        dll.printlist();
        DoublyLinkedList ll = dll.cloneWithoutDuplicates();
        ll.printlist();

    }


}

Add a comment
Know the answer?
Add Answer to:
DATA STRUCTURES JAVA In the doubly linked list develop the following operators: 1. Clone without duplicates....
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
  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

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

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a...

    Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList {    private class Node {        private Object data; //Assume data implemented Comparable        private Node next, prev;        private Node(Object data, Node pref, Node next)        {            this.data = data;       ...

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

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

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