Question
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:
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
}
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 positionStep 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