Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this code, Please provide a picture of output. I need to create this method: This is the code: public class OrderedList >

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

I need to create this method:

image text in transcribed

This is the code:

public class OrderedList> {

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 += " "; 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; } 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) //ot 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 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

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

What is the managerial capacity problem?

Answered: 1 week ago

Question

=+ Why do we have markets and, according to economists,

Answered: 1 week ago