You have been provided with java code for SomeList class.  This code is for a general linked list implementation where the elements are not ordered.  For this assignment you will modify the code provided to create a SortedList class that will maintain elements in a linked list in ascending order and allow the removal of objects from both the front and back.  You will be required to add methods for inserting an object in order (InsertInorder) and removing an object from the front or back.  You will write a test program, ListTest, that inserts 25 random integers, between 0 and 100, into the linked list resulting in an in-order list.  Your code to remove an object must include the exception NoSuchElementException.  Demonstrate your code by displaying the ordered linked list and including the capability to remove nodes from the front and back.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

JAVA CODE

Learning Objectives: Detailed understanding of the linked list and its implementation. Practice with inorder sorting. Practice with use of Java exceptions. Practice use of generics.

 

You have been provided with java code for SomeList<T> class.  This code is for a general linked list implementation where the elements are not ordered.  For this assignment you will modify the code provided to create a SortedList<T> class that will maintain elements in a linked list in ascending order and allow the removal of objects from both the front and back.  You will be required to add methods for inserting an object in order (InsertInorder) and removing an object from the front or back.  You will write a test program, ListTest, that inserts 25 random integers, between 0 and 100, into the linked list resulting in an in-order list.  Your code to remove an object must include the exception NoSuchElementException.  Demonstrate your code by displaying the ordered linked list and including the capability to remove nodes from the front and back. 

 

//class to represent one node in a list

class ListNode<T> {

T data; // data for this node

ListNode<T> nextNode; // reference to the next node in the list

 

//constructor creates a ListNode that refers to object

ListNode(T object) { 

 this(object, null); 

 

//constructor creates ListNode that refers to the specified

//object and to the next ListNode

ListNode(T object, ListNode<T> node) {

 data = object;  

 nextNode = node; 

}

 

//return reference to data in node

T getData() { 

 return data; // return item in this node

}

 

//return reference to next node in list

ListNode<T> getNext() { 

 return nextNode; // get next node

}

}

 

//class SomeList definition

public class SomeList<T> {

private ListNode<T> firstNode;

private ListNode<T> lastNode;

private String name; // string used in printing

 

//constructor creates empty List with "my list" as the name

public SomeList() { 

 this("my list"); 

}

 

//constructor creates an empty List with a name

public SomeList(String listName) {

 name = listName;

 firstNode = lastNode = null;

}

 

//insert item in front

public void insertAtFront(T insertItem) {

 if (isEmpty()) {

  firstNode = lastNode = new ListNode<T>(insertItem);

 }

 else {

  firstNode = new ListNode<T>(insertItem, firstNode);

 }

}

 

 

 

//determine whether list is empty

public boolean isEmpty() { 

 return firstNode == null; // return true if list is empty

}

 

//output list contents

public void print() {

 if (isEmpty()) {

  System.out.printf("Empty %s\n", name);

  return;

 }

 

 System.out.printf("%s is: ", name);

 ListNode<T> current = firstNode;

 

 // while not at end of list, output current node's data

 while (current != null) {

  System.out.printf("%s ", current.data);

  current = current.nextNode;

 }

 

 System.out.println();

}

}

 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education