Solved Homework: Question: Need help with this code, Please provide a picture of output. I need to create this method: This…

Need help with this code, Please provide a picture of output.

I need to create this method:

a method to reverse the order of the objects on the list. I.e. after the first reversal the list will be in descending order; after the second it will be ascending again, etc. To receive credit for this method, you must implement this algorithm: For each node except for the last one, remove it from the list and insert it immediately after the original last node Do not create any new nodes or swap the data in the nodes. Remove each node from the list and reinsert it in its new position

This is the code:

public class OrderedList<E extends Comparable<E>> {

private boolean ascendingOrder;
private Node head;

class Node {

E data;
Node next;

Node(E val, Node nxt) {
data = val;
next = nxt;
}
}

public String toString() {
if (isEmpty()) {
return “List is empty.”;
} else {
String str = “”;
Node curr = head;
while (curr != null) {
str += curr.data + ” “;
curr = curr.next;
}
str += “n”;
return str;
}
}

public void insert(E data) {
Node n = new Node(data, null);
if (head == null) {
head = n;
} else {
Node curr = head;
Node prev = null;
while (curr != null) {
if (ascendingOrder && data.compareTo(curr.data) < 0) {
break;
} else if (!ascendingOrder && data.compareTo(curr.data) > 0) {
break;
}
prev = curr;
curr = curr.next;
}
n.next = curr;
if (curr == head) //inserting at head position
{
head = n;
} else {
prev.next = n;
}
}
}

public boolean delete(E data) {
Node curr = head;
Node prev = null;
while (curr != null) {
if (curr.data.equals(data)) {
break;
}
prev = curr;
curr = curr.next;
}
if (curr == null) ///not found
{
return false;
}
if (curr == head) //deleting head node
{
head = head.next;
} else {
prev.next = curr.next;
}
return true;
}

public boolean isEmpty() {
return head == null;
}

public void clear() {
head = null;
}

public void reverse()
{
if (head == null)
{
return;
}
// code here. Please put commets
}

public OrderedList() {
head = null;
ascendingOrder = true;
}

}

=====================================

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
*
* @author
*/
public class Test
{

public static void main(String[] args)
{

OrderedList<Integer> nums = new OrderedList<>();
try
{
Scanner infile = new Scanner(new File(“DataFile.txt”));
while (infile.hasNextLine())
{
String line = infile.nextLine().trim();
if (line.isEmpty()) //skip empty lines
{
continue;
}
System.out.println(line);
String[] tokens = line.split(“\s+”);
if (tokens[0].equalsIgnoreCase(“INSERT”))
{
nums.insert(Integer.parseInt(tokens[1]));
}
else if (tokens[0].equalsIgnoreCase(“DELETE”))
{
if (!nums.delete(Integer.parseInt(tokens[1])))
{
System.out.println(“Could not delete ” + tokens[1]
+ “, not on the list.”);
}
}
else if (tokens[0].equalsIgnoreCase(“CLEAR”))
{
nums.clear();
}
else if (tokens[0].equalsIgnoreCase(“REVERSE”))
{
nums.reverse();
}
else
{
System.out.println(“Unknown command ” + tokens[0]);
}
System.out.println(“Numbers on List: ” + nums);
}
infile.close();
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}

}

a method to reverse the order of the objects on the list. I.e. after the first reversal the list will be in descending order; after the second it will be ascending again, etc. To receive credit for this method, you must implement this algorithm: For each node except for the last one, remove it from the list and insert it immediately after the original last node Do not create any new nodes or “swap” the data in the nodes. Remove each node from the list and reinsert it in its new position

Expert Answer

Solution:

Please find below the reverse() implementation and output. Please note a sample main() is provided in order to show the working of reverse method.

package november;

public class OrderedList<E extends Comparable<E>> {

private boolean ascendingOrder;

private Node head;

class Node {

E data;

Node next;

Node(E val, Node nxt) {

data = val;

next = nxt;

}

}

public String toString() {

if (isEmpty()) {

return “List is empty.”;

} else {

String str = “”;

Node curr = head;

while (curr != null) {

str += curr.data + ” “;

curr = curr.next;

}

str += “n”;

return str;

}

}

public void insert(E data) {

Node n = new Node(data, null);

if (head == null) {

head = n;

} else {

Node curr = head;

Node prev = null;

while (curr != null) {

if (ascendingOrder && data.compareTo(curr.data) < 0) {

break;

} else if (!ascendingOrder && data.compareTo(curr.data) > 0) {

break;

}

prev = curr;

curr = curr.next;

}

n.next = curr;

if (curr == head) //inserting at head position

{

head = n;

} else {

prev.next = n;

}

}

}

public boolean delete(E data) {

Node curr = head;

Node prev = null;

while (curr != null) {

if (curr.data.equals(data)) {

break;

}

prev = curr;

curr = curr.next;

}

if (curr == null) ///not found

{

return false;

}

if (curr == head) //deleting head node

{

head = head.next;

} else {

prev.next = curr.next;

}

return true;

}

public boolean isEmpty() {

return head == null;

}

public void clear() {

head = null;

}

public void reverse()

{

if (head == null)

{

return;

}

Node previousNode = null;

Node currentNode = head;

Node nextNode = null;

while (currentNode != null)

{

nextNode = currentNode.next;

currentNode.next = previousNode;

previousNode = currentNode;

currentNode = nextNode;

}

head = previousNode;

}

public OrderedList() {

head = null;

ascendingOrder = true;

}

public static void main(String[] args) {

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

System.out.println(“List : ” + list.toString());

list.insert(100);list.insert(9); list.insert(10);list.insert(88);

System.out.println(“List : ” + list.toString());

System.out.println(“Reversing list …”);

list.reverse();

System.out.println(“List : ” + list.toString());

}

}

DOrderedListjavaTest.java 89 90 public void reverse( 91 92 >> >> if (head= null)-1 93g 94 return; 95g 96 Node previousNode null; Node currentNode head; Node nextNodenull; while (currentNodenull) 9 100 101 102 103 nextNode- currentNode.next; currentNode.next-previousNode; previousNode : currentNode;·wj R Problems @Javadoc Declaration貝Console X <terminated OrderedList [Java Application] CAProgram FilesJavajrel.8.0_74binjavaw.exe (Nov 10, 2018, 9:30:04 PM List List is empty List: 9 10 88 100 Reversing list List: 100 88 10 9

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

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.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?