Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Submit LinkedList.java (Given below) with the added following methods: Further explanation: Exercises 16.9 is the stutter method, that takes this list (a LinkedList) and doubles

Submit LinkedList.java (Given below) with the added following methods:

image text in transcribed

Further explanation: Exercises 16.9 is the stutter method, that takes this list (a LinkedList) and doubles the size by producing a two consecutive data for each. For example [1,2,3] will change to a longer list [1,1,2,2,3,3] after stutter is called.

A. isPerfectStutter() is a boolean method that can tell if this list has two E data throughout. Empty lists and lists of length 1 will return false, because it takes two or more E's to be stuttered.

B undoStutter() will undo the stutter effect created by the stutter() method, and needs to first check if isPerfectStutter() is true, because you cannot undo the stutter() signature if it's not present.

GIVEN LinkedList.java with TEST CODE at the end to make sure the code works:

import java.util.*; // Class LinkedList can be used to store a list of values of type E.

public class LinkedList> implements Iterable{ // removed implements List due to version differences of List private ListNode front; // first value in the list private ListNode back; // last value in the list private int size; // current number of elements

// post: constructs an empty list public LinkedList() { front = new ListNode(null); back = new ListNode(null); clear(); } // METHODS perfectStutter and undoStutter here

//END CODE HERE

// 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 current = front.next.next; while (current != back) { result += ", " + current.data; current = current.next; } result += "]"; return result; } } // post: creates a comma-separated, bracketed version of the list // Iverson creation public String backwards() { if (size == 0) { return "[]"; } else { String result = "[" + back.prev.data; ListNode current = back.prev.prev; while (current != front) { result += ", " + current.data; current = current.prev; } 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 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 size) { throw new IndexOutOfBoundsException("index: " + index); } ListNode current = nodeAt(index - 1); ListNode newNode = new ListNode(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 other) { for (E value: other) { add(value); } }

// 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 iterator() { return new LinkedIterator(); }

// pre : 0 nodeAt(int index) { ListNode current; if (index = 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 = size()) { throw new IndexOutOfBoundsException("index: " + index); } }

private static class ListNode { public E data; // data stored in this node public ListNode next; // link to next node in the list public ListNode 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 next, ListNode prev) { this.data = data; this.next = next; this.prev = prev; } }

private class LinkedIterator implements Iterator { private ListNode 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 prev2 = current.prev.prev; prev2.next = current; current.prev = prev2; size--; removeOK = false; } } }

A. isPerfectStutter): returns true if this list has exactly two data throughout the list (like stutter) was just used on the list) B. undoStutter); a void method that will undo the stutter) operation for a list where isPerfectStutter) is true. A. isPerfectStutter): returns true if this list has exactly two data throughout the list (like stutter) was just used on the list) B. undoStutter); a void method that will undo the stutter) operation for a list where isPerfectStutter) is true

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

4. Describe cultural differences that influence perception

Answered: 1 week ago