Question
Submit LinkedList.java (Given Below) with the following methods added and dont forget to add the backwards method so we can check but directions! Chapter 16,
Submit
Chapter 16, Text Exercises:
#7. deleteBack #8. switchPairs #9. stutter #14. removeAll
IMPORTANT:
Do NOT use the primitive int versions, but make it generic
- MUST start with my file LinkedList.java and add these four methods (or more).
- Above file is modified from text (e.g. fixed List, Iterable, Comparable).
- Added a backwards() method so we can check both directions.
- All your methods must work for empty lists.
- Test code is written at the end so make sure your output works!
WRITE YOUR CODE IN HERE:
LinkedList.java
import java.util.*; // Class LinkedList
public class LinkedList
// post: constructs an empty list public LinkedList() { front = new ListNode
// post: returns the current number of elements in the list public int size() { return size; }
// pre : 0 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
// 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
// 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 size) { throw new IndexOutOfBoundsException("index: " + index); } ListNode
// post: appends all values in the given list to the end of this list public void addAll(List
// pre : 0 current = nodeAt(index - 1); current.next = current.next.next; current.next.prev = current; size--; }
// pre : 0 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
// pre : 0 nodeAt(int index) { ListNode
// post: throws an IndexOutOfBoundsException if the given index is // not a legal index of the current list private void checkIndex(int index) { if (index = size()) { throw new IndexOutOfBoundsException("index: " + index); } }
private static class ListNode
// 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
private class LinkedIterator implements Iterator
// 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
//TEST CODE!!!
public static void main(String[] args) {
LinkedList
LinkedList
B.add(1); B.add(19); B.add(4); B.add(17);
B.stutter();
System.out.println(B.toString()); //[1, 1, 19, 19, 4, 4, 17, 17]
System.out.println(B.backwards()); //[17, 17, 4, 4, 19, 19, 1, 1]
A.removeAll("nothing");
}
}
7. Write a method called deleteBack that deletes the last value (the value at the back of the list) and returns the deleted value. If the list is empty 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 second pair (31 and 47), and the third pair (52 and 68) to yield [25, 10, 47, 31, 68, 52, 77] 9. Write a method called stutter that doubles the size of a list by replacing every integer in the list with two of that integer. For example, suppose a variable list stores the values [1, 8, 19, 4, 17], after a call of list.stutter(), it should store [1, 1, 8, 8, 19, 19, 4, 4, 17, 17] 14. Write a method called removeAll that removes all occurrences of a particular value. For example, if a variable list stores the values [3, 9, 4, 2, 3, 8, 17, 4, 3, 18],the call of list.removeAll (3); would change the list to store [9, 4, 2, 8, 17, 4, 18] 7. Write a method called deleteBack that deletes the last value (the value at the back of the list) and returns the deleted value. If the list is empty 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 second pair (31 and 47), and the third pair (52 and 68) to yield [25, 10, 47, 31, 68, 52, 77] 9. Write a method called stutter that doubles the size of a list by replacing every integer in the list with two of that integer. For example, suppose a variable list stores the values [1, 8, 19, 4, 17], after a call of list.stutter(), it should store [1, 1, 8, 8, 19, 19, 4, 4, 17, 17] 14. Write a method called removeAll that removes all occurrences of a particular value. For example, if a variable list stores the values [3, 9, 4, 2, 3, 8, 17, 4, 3, 18],the call of list.removeAll (3); would change the list to store [9, 4, 2, 8, 17, 4, 18]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started