Write a Java Class to Implement a Generic Ordered List An ordered list is one in
ID: 3701897 • Letter: W
Question
Write a Java Class to Implement a Generic Ordered List
An ordered list is one in which the objects on the list appear in order. When a new object is added, it is inserted into the list in its proper position, so there is never a need to “sort” the list. Your list will be in ascending order initially.
To receive credit, your list must be implemented as a singly-linked list of generic nodes, where each node has two instance variables: an object of the “type variable” class, and a pointer to the next node on the list
To receive credit, no data structures may be used other than your own OrderedList class
Your class will have a proper constructor and these methods:
an overridden toString method that returns the objects on the list in the order in which they appear or an appropriate message if the list is empty
an insert method that inserts a new node containing its parameter in its proper place in the list
a boolean delete method that searches the list for a node containing its parameter. If found, the node is removed from the list and true is returned; else, false is returned
a boolean isEmpty method
a method to clear the list
a method to reverse the order of the objects on the list. I.e. after the first reversal the list will be indescending order. To receive credit for this method, you must implement this algorithm:
Traversing the list sequentially, remove each node and insert it as the new first node. Do not merely “swap” the objects stored in the nodes or create any new nodes - remove each node from the list and reinsert it in its new position
Write a Test Class for Your Ordered List Class
Your main method will read and execute list operation instructions from a data file, until end-of-file. Each operation is to be implemented by calling a method of your list class.
After each operation is executed, print the instruction and the updated list. If an operation cannot be done or is unnecessary because the list is empty, say so
The data file to be used is available on the class web site and the instructions are:
INSERT X - Insert a new node with info x in its proper place in the list
DELETE X - Remove the node that contains x from the list. (If x does not appear on the list, say so)
CLEAR - Clear the list
REVERSE - Reverse the order of the objects on the list (using the algorithm specified above)
Hopefully Helpful Hints
Because an ordered list involves comparisons, it will be necessary to constrain (i.e. “limit”) the type parameter to those classes that implement Java’s Comparable interface
See the “Generic Classes” document online
An iterator allows the user of a collection to access each object stored without having to know anything about how the collection is implemented. For this assignment, the client doesn’t require that capability
Begin by creating the skeleton of your OrderedList class, with just the method “stubs” (i.e. method declaration with an empty body and bogus return value if the method returns a value). That way your program will compile and run and you can code the method bodies separately
To insert a new object in its proper place, we need to know whether the list is in ascending or descending order. This cries out for a boolean instance variable to so indicate. Design your insert method to call privateutility methods insertAscending or insertDescending to handle the insertion
Order list txt :
Explanation / Answer
package com.chegg.vinoth;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class GenericOrderedList<T extends Comparable<T>> {
Node start;
class Node<T extends Comparable<T>> {
private T data;
private Node nextNode;
public Node(){
}
public Node(T data){
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
}
public boolean insert(T data){
Node node = new GenericOrderedList().new Node();
node.setData(data);
if(start==null){
start = node;
}else{
Node temp= start;
while(temp!=null){
if(temp.getNextNode()==null){
temp.setNextNode(node);
break;
}else
temp = temp.getNextNode();
}
}
return true;
}
public boolean orderedInsert(T data) {
Node node = new GenericOrderedList().new Node();
node.setData(data);
if(start==null) {
start = node;
return true;
}else {
Node temp = start;
// Data is less than data at start
if(temp.getData().compareTo(data)>0) {
node.nextNode = start;
start = node;
}else {
while(temp!=null) {
if(temp.nextNode!=null && temp.nextNode.getData().compareTo(data)>=0) {
node.nextNode = temp.nextNode;
temp.nextNode = node;
break;
}else if (temp.nextNode!=null && temp.nextNode.getData().compareTo(data)<0) {
temp = temp.nextNode;
}
else {
//node.nextNode = temp.nextNode;
temp.nextNode = node;
break;
}
//temp.nextNode = node;
}
}
}
return true;
}
public boolean delete(T data){
if(start==null){
return false;
}else{
Node temp= start;
while(temp!=null){
if(temp.getNextNode() != null && (temp.getNextNode().getData() == data)){
Node nextNode = temp.getNextNode().getNextNode();
temp.setNextNode(nextNode);
return true;
}else
temp = temp.getNextNode();
}
}
return false;
}
public void clearList(){
start = null;
}
public boolean isEmpty() {
if(start==null) {
return true;
}
return false;
}
public boolean reverseList(){
Node current = start;
while(current!=null && current.getNextNode()!=null){
Node next = current.getNextNode();
Node nexttonext = next.getNextNode();
current.setNextNode(nexttonext);
next.setNextNode(start);
start = next;
}
//printList();
return true;
}
public boolean printList(){
Node temp = start;
while(temp!=null){
System.out.print(temp.getData() + " ");
temp = temp.getNextNode();
}
return true;
}
public static void main(String args[]) throws IOException {
GenericOrderedList<Integer> gl = new GenericOrderedList<>();
BufferedReader br = new BufferedReader(new FileReader("OrderList.txt"));
String line = "";
while((line=br.readLine()) != null) {
line = line.trim();
System.out.println(" Input Command: " + line);
if(line.contains(" ")) {
String[] inp = line.split(" " );
if(inp[0].equals("INSERT")) {
int inpNum = Integer.parseInt(inp[1]);
gl.orderedInsert(inpNum);
System.out.println("List after inserting " + inpNum);
gl.printList();
}else if(inp[0].equals("DELETE")) {
int inpNum = Integer.parseInt(inp[1]);
boolean flag = gl.delete(inpNum);
if(!flag)
System.out.println(inpNum + " Not in list to delete");
System.out.println("List after deleting " + inpNum);
gl.printList();
}
}else {
if(line.equals("CLEAR")) {
gl.clearList();
System.out.println("List after clear");
gl.printList();
}else if (line.equals("REVERSE")) {
gl.reverseList();
System.out.println("Reversed List");
gl.printList();
}
}
}
}
}
=========================
Input Command: INSERT 5
List after inserting 5
5
Input Command: INSERT 20
List after inserting 20
5 20
Input Command: INSERT 12
List after inserting 12
5 12 20
Input Command: INSERT 27
List after inserting 27
5 12 20 27
Input Command: DELETE 12
List after deleting 12
5 20 27
Input Command: INSERT 37
List after inserting 37
5 20 27 37
Input Command: DELETE 99
99 Not in list to delete
List after deleting 99
5 20 27 37
Input Command: INSERT 73
List after inserting 73
5 20 27 37 73
Input Command: DELETE 5
5 Not in list to delete
List after deleting 5
5 20 27 37 73
Input Command: DELETE 73
List after deleting 73
5 20 27 37
Input Command: INSERT 48
List after inserting 48
5 20 27 37 48
Input Command: REVERSE
Reversed List
48 37 27 20 5
Input Command: INSERT 99
List after inserting 99
48 37 27 20 5 99
Input Command: INSERT 23
List after inserting 23
23 48 37 27 20 5 99
Input Command: INSERT 11
List after inserting 11
11 23 48 37 27 20 5 99
Input Command: DELETE 99
List after deleting 99
11 23 48 37 27 20 5
Input Command: DELETE 11
11 Not in list to delete
List after deleting 11
11 23 48 37 27 20 5
Input Command: REVERSE
Reversed List
5 20 27 37 48 23 11
Input Command: REVERSE
Reversed List
11 23 48 37 27 20 5
Input Command: CLEAR
List after clear
Input Command: DELETE 37
37 Not in list to delete
List after deleting 37