Submit generic LinkedList.java with the following methods added: Chapter 16, Tex
ID: 3674660 • Letter: S
Question
Submit generic LinkedList.java with the following methods added:
Chapter 16, Text Exercises:
#2. min (page 999)
#8. switchPairs
#19. rotate
#21. reverse (page 1001)
Note this is the generic version of the linked list from our text author, and I've carefully selected the exercises that could be any see example below. Since you probably have a good solution for the primitive int version already (like Practice-IT), I suggest you start with that code, then modify for this generic list with next/prev front/back nodes.
Here is some test code to get things started:
LinkedList A = new LinkedList();
LinkedList B = new LinkedList();
B.add(1); B.add(19); B.add(4); B.add(17);
B.rotate();
A.reverse();
For the first method (min), you will need to cast (Comparable) and use the .compareTo() method. You can insert the following to get rid of the warning messages:
@SuppressWarnings({ "unchecked", "rawtypes" })
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#2. Write a method called min that returns the minimum value in a list of integers. If the list is empty, it should throw a NoSuchElementException.
#8. Write a method called switchPairs that switches the order of values in the list in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. If the list contains an odd number of values, the final element is not moved. For example, if the list initially stores [10, 25, 31, 47, 52, 68, 77], your method should switch the first pair (10 and 25), the sec- ond pair (31 and 47), and the third pair (52 and 68) to yield [25, 10, 47, 31, 68, 52, 77].
#19. Write a method called rotate that moves the value at the front of a list of integers to the end of the list. For example, if a variable called list stores the values [8, 23, 19, 7, 45, 98, 102, 4], then the call of list.rotate(); should move the value 8 from the front of the list to the back of the list, changing the list to store [23, 19, 7, 45, 98, 102, 4, 8]. If the method is called for a list of 0 elements or 1 element, it should have no effect on the list. You may neither construct any new nodes to solve this problem nor change any of the data val- ues stored in the nodes. You must solve the problem by rearranging the links of the list.
#21.Write a method called reverse that reverses the order of the elements in the list. (This is very challenging!) For example, if the variable list initially stores the values [1, 8, 19, 4, 17], the call of list.reverse(); should change the list to store [17, 4, 19, 8, 1].
-------------------------------------------------------------------------------------------------------------
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
// Class LinkedList<E> can be used to store a list of values of type E.
// from Buildingjavaprograms.com
public class LinkedList<E> { // removed implements List due to version differences
private ListNode<E> front; // first value in the list
private ListNode<E> back; // last value in the list
private int size; // current number of elements
// post: constructs an empty list
public LinkedList() {
front = new ListNode<E>(null);
back = new ListNode<E>(null);
clear();
}
// ADD MORE METHODS HERE:
// post: returns the current number of elements in the list
public int size() {
return size;
}
// pre : 0 <= index < size() (throws IndexOutOfBoundsException if not)
// post: returns the value at the given index in the list
public E get(int index) {
checkIndex(index);
ListNode<E> current = nodeAt(index);
return current.data;
}
// post: creates a comma-separated, bracketed version of the list
public String toString() {
if (size == 0) {
return "[]";
} else {
String result = "[" + front.next.data;
ListNode<E> current = front.next.next;
while (current != back) {
result += ", " + current.data;
current = current.next;
}
result += "]";
return result;
}
}
// post : returns the position of the first occurrence of the given
// value (-1 if not found)
public int indexOf(E value) {
int index = 0;
ListNode<E> current = front.next;
while (current != back) {
if (current.data.equals(value)) {
return index;
}
index++;
current = current.next;
}
return -1;
}
// post: returns true if list is empty, false otherwise
public boolean isEmpty() {
return size == 0;
}
// post: returns true if the given value is contained in the list,
// false otherwise
public boolean contains(E value) {
return indexOf(value) >= 0;
}
// post: appends the given value to the end of the list
public void add(E value) {
add(size, value);
}
// pre: 0 <= index <= size() (throws IndexOutOfBoundsException if not)
// post: inserts the given value at the given index, shifting subsequent
// values right
public void add(int index, E value) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index: " + index);
}
ListNode<E> current = nodeAt(index - 1);
ListNode<E> newNode = new ListNode<E>(value, current.next, current);
current.next = newNode;
newNode.next.prev = newNode;
size++;
}
// post: appends all values in the given list to the end of this list
public void addAll(List<E> other) {
for (E value: other) {
add(value);
}
}
// pre : 0 <= index < size() (throws IndexOutOfBoundsException if not)
// post: removes value at the given index, shifting subsequent values left
public void remove(int index) {
checkIndex(index);
ListNode<E> current = nodeAt(index - 1);
current.next = current.next.next;
current.next.prev = current;
size--;
}
// pre : 0 <= index < size() (throws IndexOutOfBoundsException if not)
// post: replaces the value at the given index with the given value
//public void set(int index, E value) {
// checkIndex(index);
// ListNode<E> current = nodeAt(index);
// current.data = value;
//}
// post: list is empty
public void clear() {
front.next = back;
back.prev = front;
size = 0;
}
// post: returns an iterator for this list
public Iterator<E> iterator() {
return new LinkedIterator();
}
// pre : 0 <= index < size()
// post: returns the node at a specific index. Uses the fact that the list
// is doubly-linked to start from the front or the back, whichever
// is closer.
private ListNode<E> nodeAt(int index) {
ListNode<E> current;
if (index < size / 2) {
current = front;
for (int i = 0; i < index + 1; i++) {
current = current.next;
}
} else {
current = back;
for (int i = size; i >= index + 1; i--) {
current = current.prev;
}
}
return current;
}
// post: throws an IndexOutOfBoundsException if the given index is
// not a legal index of the current list
private void checkIndex(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("index: " + index);
}
}
private static class ListNode<E> {
public E data; // data stored in this node
public ListNode<E> next; // link to next node in the list
public ListNode<E> prev; // link to previous node in the list
// post: constructs a node with given data and null links
public ListNode(E data) {
this(data, null, null);
}
// post: constructs a node with given data and given links
public ListNode(E data, ListNode<E> next, ListNode<E> prev) {
this.data = data;
this.next = next;
this.prev = prev;
}
}
private class LinkedIterator implements Iterator<E> {
private ListNode<E> current; // location of next value to return
private boolean removeOK; // whether it's okay to remove now
// post: constructs an iterator for the given list
public LinkedIterator() {
current = front.next;
removeOK = false;
}
// post: returns true if there are more elements left, false otherwise
public boolean hasNext() {
return current != back;
}
// pre : hasNext()
// post: returns the next element in the iteration
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
E result = current.data;
current = current.next;
removeOK = true;
return result;
}
// pre : next() has been called without a call on remove (i.e., at most
// one call per call on next)
// post: removes the last element returned by the iterator
public void remove() {
if (!removeOK) {
throw new IllegalStateException();
}
ListNode<E> prev2 = current.prev.prev;
prev2.next = current;
current.prev = prev2;
size--;
removeOK = false;
}
}
}
Explanation / Answer
to find minimum in the given list
to rotate the list
include
Rotatelist() {
}
i have a set of solution to reverse a linked list, hope this is helpful
to swap the list element pairwise
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
void pairWiseSwap()
{
Node temp = head;
/* Traverse only till there are atleast 2 nodes left */
while (temp != null && temp.next != null) {
/* Swap the data */
int k = temp.data;
temp.data = temp.next.data;
temp.next.data = k;
temp = temp.next.next;
}
}
public double minimum()