In: Computer Science
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");
   }
}
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");
}
}
