(Solved Homework): package csci3230.hw3; import java.io.File; import java.io.IOException; import java.util.ListIterator;

package csci3230.hw3;

import java.io.File;
import java.io.IOException;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Random;

import csci3230.hw3.OutputWriter;

public class DoublyLinkedList<Item> implements Iterable<Item> {
private int n; // number of elements on list
private Node pre; // sentinel before first item
private Node post; // sentinel after last item

public DoublyLinkedList() {
pre = new Node();
post = new Node();
pre.next = post;
post.prev = pre;
}

// linked list node helper data type
private class Node {
private Item item;
private Node next;
private Node prev;
}

public boolean isEmpty() { return n==0;} // your code}
public int size() { return n;}// your code }

// add the item to the list
public void add(Item item) {
Node last = post.prev;
Node x = new Node();
x.item = item;
x.next = post;
x.prev = last;
post.prev = x;
last.next = x;
n++; // your code
}

public ListIterator<Item> iterator() { return new DoublyLinkedListIterator(); }

// assumes no calls to DoublyLinkedList.add() during iteration
private class DoublyLinkedListIterator implements ListIterator<Item> {
private Node current = pre.next; // the node that is returned by next()
private Node lastAccessed = null; // the last node to be returned by prev() or next()
// reset to null upon intervening remove() or add()
private int index = 0;

public boolean hasNext() { return index<n;}// your code }
public boolean hasPrevious() { return index >0;}// your code }
public int previousIndex() { return index – 1;}// your code }
public int nextIndex() { return index;}// your code }

public Item next() {
if (!hasNext()) throw new NoSuchElementException();
lastAccessed = current;
Item item = current.item;
current = current.next;
index++;
return item;// your code
}

public Item previous() {
if (!hasPrevious()) throw new NoSuchElementException();
current = current.prev;
index–;
lastAccessed = current;
return current.item;// your code
}

// replace the item of the element that was last accessed by next() or previous()
// condition: no calls to remove() or add() after last call to next() or previous()
public void set(Item item) {
if (lastAccessed == null) throw new IllegalStateException();
lastAccessed.item = item;// your code
}

// remove the element that was last accessed by next() or previous()
// condition: no calls to remove() or add() after last call to next() or previous()
public void remove() {
if (lastAccessed == null) throw new IllegalStateException();
Node x = lastAccessed.prev;
Node y = lastAccessed.next;
x.next = y;
y.prev = x;
n–;
if (current == lastAccessed)
current = y;
else
index–;
lastAccessed = null;// your code
}

// add element to list
public void add(Item item) {
Node x = current.prev;
Node y = new Node();
Node z = current;
y.item = item;
x.next = y;
y.next = z;
z.prev = y;
y.prev = x;
n++;
index++;
lastAccessed = null;// your code
}

}

public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + ” “);
return s.toString();
}

/* a test client
* arg[1]: number of input values
* arg[2]: random seed
* arg[3]: output file path
*/
public static void main(String[] args) throws IOException {

int n, seed;
File outputFile;
File dir = new File(“.”);

if (args.length > 0) {
n = Integer.parseInt(args[0]);
seed = Integer.parseInt(args[1]);
}else {
n = 10;
seed = 1;
}

if (args.length == 3) {
outputFile = new File(args[2]);
} else {
outputFile = new File(dir.getCanonicalPath() + File.separator + “Files/testOut_DLL”);
}
OutputWriter out = new OutputWriter(outputFile);

Random r = new Random(seed);

// add elements 1, …, n
out.writeOutput(“Integer items to insert: “+ n);
DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>();
for (int i = 0; i < n; i++)
list.add(r.nextInt(100));
out.writeOutput(list.toString());
ListIterator<Integer> iterator = list.iterator();

// go forwards with next() and set()
out.writeOutput(“add 1 to each element via next() and set()”);
while (iterator.hasNext()) {
int x = iterator.next();
iterator.set(x + 1);
}
out.writeOutput(list.toString());
// go backwards with previous() and set()
out.writeOutput(“multiply each element by 3 via previous() and set()”);
while (iterator.hasPrevious()) {
int x = iterator.previous();
iterator.set(x + x + x);
}
out.writeOutput(list.toString());

// remove all elements that are multiples of 4 via next() and remove()
out.writeOutput(“remove elements that are a multiple of 4 via next() and remove()”);
while (iterator.hasNext()) {
int x = iterator.next();
if (x % 4 == 0) iterator.remove();
}
out.writeOutput(list.toString());

// remove all even elements via previous() and remove()
out.writeOutput(“remove elements that are even via previous() and remove()”);
while (iterator.hasPrevious()) {
int x = iterator.previous();
if (x % 2 == 0) iterator.remove();
}
out.writeOutput(list.toString());

// add elements via next() and add()
out.writeOutput(“add elements via next() and add()”);
while (iterator.hasNext()) {
int x = iterator.next();
iterator.add(x + 1);
}
out.writeOutput(list.toString());

// add elements via previous() and add()
out.writeOutput(“add elements via previous() and add()”);
while (iterator.hasPrevious()) {
int x = iterator.previous();
iterator.add(x * 10);
iterator.previous();
}
out.writeOutput(list.toString());
}
}

The class below(LinkedListStack) is where I need help. Im suppose to complete the rest of the methods (pop,push, size, isEmpty, top). I also need help with the method that finds the maximum value.

package csci3230.hw3;

import java.io.File;
import java.io.IOException;
import java.util.ListIterator;
import java.util.Random;

public class LinkedListStack {

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

public int size() {
// your code
}

public void push(Integer s){
// your code
}

public Integer pop() {
// your code
}

public Integer top() {
// your code
}

public boolean isEmpty() {
// your code
}

public String displayStack() {
return (list.toString());
}

public static void main(String[] args) throws IOException {
LinkedListStack stack = new LinkedListStack();

int n, seed;
File outputFile;
File dir = new File(“.”);

if (args.length > 0) {
n = Integer.parseInt(args[0]);
seed = Integer.parseInt(args[1]);
}else {
n = 10;
seed = 1;
}

if (args.length == 3) {
outputFile = new File(args[2]);
} else {
outputFile = new File(dir.getCanonicalPath() + File.separator + “Files/testOut_Stack”);
}
OutputWriter out = new OutputWriter(outputFile);

Random r = new Random(seed);

Integer nextval;
for (int i = 0; i < n; i++) {
nextval = r.nextInt(10000);
// your code
/* retain the max value that you see among the integers generated in the stack.
* In the end there should be only one integer in stack, which is the max value
*/
}

// write the content of stack — which is the max value — to file
out.writeOutput(stack.displayStack());
}
}

Expert Answer

 Here is the stack implementation using the doublylinked list code above. Please don’t forget to rate the answer if it helped. Thank you very much.

package csci3230.hw3;
import java.io.File;
import java.io.IOException;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Random;
public class LinkedListStack {

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

public int size() {
return list.size();
}

public void push(Integer s){
list.iterator().add(s);
}

public Integer pop() {

ListIterator<Integer> iterator = list.iterator();
if(!iterator.hasNext())
throw new NoSuchElementException();

Integer value = iterator.next();
iterator.remove();
return value;

}

public Integer top() {
ListIterator<Integer> iterator = list.iterator();
if(!iterator.hasNext())
throw new NoSuchElementException();

Integer value = iterator.next();
return value;
}

public boolean isEmpty() {
return list.isEmpty();
}
public String displayStack() {
return (list.toString());
}
public static void main(String[] args) throws IOException {
LinkedListStack stack = new LinkedListStack();
int n, seed;
File outputFile;
File dir = new File(“.”);

if (args.length > 0) {
n = Integer.parseInt(args[0]);
seed = Integer.parseInt(args[1]);
}else {
n = 10;
seed = 1;
}

if (args.length == 3) {
outputFile = new File(args[2]);
} else {
outputFile = new File(dir.getCanonicalPath() + File.separator + “Files/testOut_Stack”);
}
OutputWriter out = new OutputWriter(outputFile);

Random r = new Random(seed);

Integer nextval;
for (int i = 0; i < n; i++) {
nextval = r.nextInt(10000);
/* retain the max value that you see among the integers generated in the stack.
* In the end there should be only one integer in stack, which is the max value
*/
if(stack.isEmpty())
stack.push(nextval);
else if(stack.top() < nextval)
{
stack.pop();
stack.push(nextval);
}
}

// write the content of stack — which is the max value — to file
out.writeOutput(stack.displayStack());
out.close();
}
}

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents