Question

In: Computer Science

Java Data Structure Doubly Linked List /* * Complete the swap(int index) method * No other...

Java Data Structure Doubly Linked List

/*
* Complete the swap(int index) method
* No other methods/variables should be added/modified
*/
public class A3DoubleLL<E> {
   /*
   * Grading:
   * Swapped nodes without modifying values - 2pt
   * Works for all special cases - 1pt
   */
   public void swap(int index) {
       //swap the nodes at index and index+1
       //change the next/prev connections, do not modify the values
       //do not use delete/remove/insert/add to help with this process
       //make sure to account for all special cases
   }

   private Node start, end;
   private int count;
   public A3DoubleLL() {
       start = end = null;
       count = 0;
   }
   public String printList() {
       String output = "";
       Node current = start;
       while(current != null) {
           output += current.value + ",";
           current = current.next;
       }
       return output;
   }
   public String printListRev() {
       String output = "";
       Node current = end;
       while(current != null) {
           output += current.value + ",";
           current = current.prev;
       }
       return output;
   }
   public void add(E val) {
       Node newItem = new Node(val);
       if(start == null) {
           start = newItem;
           end = start;
           count = 1;
       } else {
           end.next = newItem;
           newItem.prev = end;
           end = newItem;
           count++;
       }
   }
   public void insert(E val, int index) {
       if(index < 0) {//fix invalid index
           index = 0;
       }
       if(index >= count) {//goes in last position
           this.add(val);
       } else {
           Node newItem = new Node(val);
           if(index == 0) {//goes in first position
               newItem.next = start;
               start.prev = newItem;
               start = newItem;
           } else {//goes in middle
               Node current = start;
               for(int i = 1; i < index; i++) {
                   current = current.next;
               }
               newItem.next = current.next;
               newItem.prev = current;
               current.next.prev = newItem;
               current.next = newItem;
           }
           count++;
       }
   }
   public void delete(int index) {
       if(index >= 0 && index < count) {//valid index
           if(index == 0) {//remove first
               start = start.next;
               if(start != null) {//as long as there was an item next in list
                   start.prev = null;
               } else {//if only item was removed
                   end = null;
               }
           } else if(index == count-1) {//remove last item
               end = end.prev;
               end.next = null;
           } else {//remove middle item
               Node current = start;
               for(int i = 1; i < index; i++) {
                   current = current.next;
               }
               current.next = current.next.next;
               current.next.prev = current;
           }
           count--;
       }
   }
   public E get(int index) {
       if(index >= 0 && index < count) {//valid index
           Node current = start;
           for(int i = 0; i < index; i++) {
               current = current.next;
           }
           return current.value;
       }
       return null;
   }
   public String toString() {
       return this.printList();
   }
   private class Node {
       E value;
       Node next, prev;
       public Node(E v) {
           value = v;
           next = prev = null;
       }
   }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


public class A3Driver {
  
   public static void main(String[] args) {
      
       A3DoubleLL<Integer> list = new A3DoubleLL<>();
       for(int i = 1; i < 10; i++) {
           list.add(i);
       }
       System.out.println("Before Swap");
       System.out.println(list.printList());
       System.out.println(list.printListRev());
       list.swap(4);
       System.out.println("After Swap");
       System.out.println(list.printList() + ":1,2,3,4,6,5,7,8,9,");
       System.out.println(list.printListRev() + ":9,8,7,6,5,4,3,2,1,");
       System.out.println();
      
       System.out.println("Hot Potatoe");
       A3CircleLL hotPotato = new A3CircleLL();
       hotPotato.playGame(5, 3);
       System.out.println("Correct:");
       System.out.println("Removed Player 4\nRemoved Player 3\nRemoved Player 5\nRemoved Player 2\nWinning player is 1");
       System.out.println();
      
       A3Queue<Integer> queue = new A3Queue<>();
       queue.enqueue(5);
       queue.enqueue(20);
       queue.enqueue(15);
       System.out.println(queue.peek()+":5");
       System.out.println(queue.dequeue()+":5");
       queue.enqueue(25);
       System.out.println(queue.dequeue()+":20");
       System.out.println(queue.dequeue()+":15");

   }
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

/*

* Complete the swap(int index) method

* No other methods/variables should be added/modified

*/

public class A3DoubleLL<E> {

    /*

     * Grading: Swapped nodes without modifying values - 2pt Works for all special

     * cases - 1pt

     */

    public void swap(int index) {

        // swap the nodes at index and index+1

        // change the next/prev connections, do not modify the values

        // do not use delete/remove/insert/add to help with this process

        // make sure to account for all special cases

        if (index >= 0 && index+1 < count) {// valid index

            Node temp = start;

            for (int i = 0; i < index; i++)

                temp = temp.next;

            Node temp_next = temp.next;

            temp.prev.next = temp_next;

            temp_next.next.prev = temp;

            temp.next = temp_next.next;

            temp_next.prev = temp.prev;

            temp_next.next = temp;

            temp.prev = temp_next;

        }        

    }

    private Node start, end;

    private int count;

    public A3DoubleLL() {

        start = end = null;

        count = 0;

    }

    public String printList() {

        String output = "";

        Node current = start;

        while (current != null) {

            output += current.value + ",";

            current = current.next;

        }

        return output;

    }

    public String printListRev() {

        String output = "";

        Node current = end;

        while (current != null) {

            output += current.value + ",";

            current = current.prev;

        }

        return output;

    }

    public void add(E val) {

        Node newItem = new Node(val);

        if (start == null) {

            start = newItem;

            end = start;

            count = 1;

        } else {

            end.next = newItem;

            newItem.prev = end;

            end = newItem;

            count++;

        }

    }

    public void insert(E val, int index) {

        if (index < 0) {// fix invalid index

            index = 0;

        }

        if (index >= count) {// goes in last position

            this.add(val);

        } else {

            Node newItem = new Node(val);

            if (index == 0) {// goes in first position

                newItem.next = start;

                start.prev = newItem;

                start = newItem;

            } else {// goes in middle

                Node current = start;

                for (int i = 1; i < index; i++) {

                    current = current.next;

                }

                newItem.next = current.next;

                newItem.prev = current;

                current.next.prev = newItem;

                current.next = newItem;

            }

            count++;

        }

    }

    public void delete(int index) {

        if (index >= 0 && index < count) {// valid index

            if (index == 0) {// remove first

                start = start.next;

                if (start != null) {// as long as there was an item next in list

                    start.prev = null;

                } else {// if only item was removed

                    end = null;

                }

            } else if (index == count - 1) {// remove last item

                end = end.prev;

                end.next = null;

            } else {// remove middle item

                Node current = start;

                for (int i = 1; i < index; i++) {

                    current = current.next;

                }

                current.next = current.next.next;

                current.next.prev = current;

            }

            count--;

        }

    }

    public E get(int index) {

        if (index >= 0 && index < count) {// valid index

            Node current = start;

            for (int i = 0; i < index; i++) {

                current = current.next;

            }

            return current.value;

        }

        return null;

    }

    public String toString() {

        return this.printList();

    }

    private class Node {

        E value;

        Node next, prev;

        public Node(E v) {

            value = v;

            next = prev = null;

        }

    }

}

public class A3Driver {

    public static void main(String[] args) {

        A3DoubleLL<Integer> list = new A3DoubleLL<>();

        for (int i = 1; i < 10; i++) {

            list.add(i);

        }

        System.out.println("Before Swap");

        System.out.println(list.printList());

        System.out.println(list.printListRev());

        list.swap(4);

        System.out.println("After Swap");

        System.out.println(list.printList() + ":1,2,3,4,6,5,7,8,9,");

        System.out.println(list.printListRev() + ":9,8,7,5,6,4,3,2,1,");

        System.out.println();

        System.out.println("Hot Potatoe");

        // A3CircleLL hotPotato = new A3CircleLL();

        // hotPotato.playGame(5, 3);

        // System.out.println("Correct:");

        // System.out

        //         .println("Removed Player 4\nRemoved Player 3\nRemoved Player 5\nRemoved Player 2\nWinning player is 1");

        // System.out.println();

        // A3Queue<Integer> queue = new A3Queue<>();

        // queue.enqueue(5);

        // queue.enqueue(20);

        // queue.enqueue(15);

        // System.out.println(queue.peek() + ":5");

        // System.out.println(queue.dequeue() + ":5");

        // queue.enqueue(25);

        // System.out.println(queue.dequeue() + ":20");

        // System.out.println(queue.dequeue() + ":15");

    }

}


Related Solutions

java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
I need an example of how to swap and index within a doubly linked list with...
I need an example of how to swap and index within a doubly linked list with index + 1. This is written in java. public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values   ...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int index) void DeleteAt(int index) void Swap(int index, int index) index starts at 0 (just like array's subscript) If the index is out of bound, RetrieveAt returns 0, DeleteAt does nothing, do nothing in Swap. Write your own testing program to test the modified class -----------------------------------------DLinkedlist.java---------------------------- public class DLinkedList { private class Node { String data; Node next; Node prev; public Node(String s) { data...
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
Can you fix please? this is adding the given location of doubly linked list in java...
Can you fix please? this is adding the given location of doubly linked list in java but if I make reverse is not adding the new node that I added but is printing forward correctly. just fix to me that part public void addAtLocation(E newNode, int location) {        Node node = new Node(newNode);               node.data = newNode;               Node previous = head;                      int counter = 1;...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
1. Answer “True” or “False”. (a) A doubly linked list structure is worse than a singly...
1. Answer “True” or “False”. (a) A doubly linked list structure is worse than a singly linked list if we plan to do a lot of insertions. (b) A doubly linked list structure is worse than a singly linked list if we plan to do a lot of deletions. (c) A doubly linked list structure is worse than a singly linked list if we plan to print the entire list frequently. (d) An array implementation of a queue is more...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT