Question

JAVA coding The Sorted List ADT Implement the SortedList class. The SortedList class extends the ...

JAVA coding

The Sorted List ADT

Implement the SortedList class. The SortedList class extends the AbstractList class. Both can be seen here. Your assignment is to implement (recursively) all of the abstract methods of the AbstractList class.

They are:

  • insert (recursive)

  • iterator

  • remove (recursive)

  • retrieve

  • search (recursive)

You must also implement an Iterator inner class for the SortedList class. You must submit a modified SortedList.java file with your source code.

Do not submit and do not modify the List.java or the AbstractList.java file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class SortedList<E extends Comparable<? super E>>

extends AbstractList<E> {

// Mthod to insert the node in the list.

private void insert(Node<E> previous,

Node<E> current,Node<E> node)

{

// check if the correct place to insert the value

// in the list.

if(current == null ||

node.data.compareTo(current.data) < 0)

{

if(previous == null)

{

node.next = head;

head = node;

}

else

{

node.next = previous.next;

previous.next = node;

}

}

// if the correct place is not found then search

// for the next place with recursion.

else

insert(current, current.next, node);

}

// Method to insert the value in the list/.

@Override

public void insert(E value)

{

// crate a temporary node.

Node<E> temp = new Node<E>(value);

insert(null, head, temp);

}

// method to remove the node from the list.

private void remove(Node<E> previous,

Node<E> current, E value)

{

// check if the current node is not or not.

if(current == null)

return;

if(current.data.compareTo(value) == 0)

{

// check if the previous node is null

// the remove the head node

if(previous == null)

head = current.next;

else

previous.next = current.next;

}

else if(current.data.compareTo(value) > 0)

return;

else

remove(current, current.next, value);

}

// method to remove the value from the list.

@Override

public void remove(E value)

{

remove(null, head, value);

}

@Override

public E retrieve(int index)

{

// create a node and assign it as head node.

Node<E> current = head;

for(int position = 0; current != null &&

position < index; position++)

{

current = current.next;

}

if(current == null)

return null;

else

return current.data;

}

// method to search an element i the list.

private boolean search(Node<E> node, E value)

{

// check if the node is having the same

// value or not.

if(node == null)

return false;

// compare the value.

if(node.data.equals(value))

return true;

else

return search(node.next, value);

}

// method to search the value in the list.

@Override

public boolean search(E value)

{

return search(head, value);

}

@Override

public java.util.Iterator<E> iterator() {

return new Iterator();

}

// inner iterator class to iterarte the list.

class Iterator implements java.util.Iterator<E>

{

Node<E> current = head;

// method to check if te lict has any element.

@Override

public boolean hasNext()

{

return current != null;

}

// method to find the next value of teh list.

@Override

public E next()

{

E value = current.data;

current = current.next;

return value;

}

}

}

DriverClass.java:

import java.util.Iterator;

// Create the class.

public class DriverCLass

{

// Method to display the values of the list.

public static void DispalyList(SortedList<Integer>list)

{

System.out.print("Sorted List:");

// use for loop to iterate the list.

for(Iterator<Integer> iterator = list.iterator();

iterator.hasNext(); )

{

System.out.print(" "+iterator.next());

}

System.out.println("\n");

}

// Start the main method of the program.

public static void main(String[] args)

{

// Craete an object of the SortedList class.

SortedList<Integer> values =

new SortedList<Integer>();

// insert the values in teh list.

values.insert(80);

values.insert(20);

values.insert(10);

// Display the list.

DispalyList(values);

// Remove the value from the list.

System.out.println

("Removing the value 10 from the list.");

values.remove(10);

// Display the list.

DispalyList(values);

// inert the value in the list.

System.out.println

("Inserting the value 50 to the list.");

values.insert(50);

System.out.println

("Inserting the value 30 to the list.");

values.insert(30);

// Display the list.

DispalyList(values);

// Retreive the value from the list.

System.out.println("Value at the index 2 is: " +

values.retrieve(2));

System.out.println("Value at the index 10 is: "

+ values.retrieve(10)+"\n");

// Seach the value in the list.

System.out.println("Searching the value 50: "+

values.search(50)+"\n");

// Display.

DispalyList(values);

}

}

Add a comment
Know the answer?
Add Answer to:
JAVA coding The Sorted List ADT Implement the SortedList class. The SortedList class extends the ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What need to be done What’s given Problem Binary Search Tree Implement the BinarySearchTree class. The...

    What need to be done What’s given Problem Binary Search Tree Implement the BinarySearchTree class. The BinarySearchTree class extends the BinaryTree class. Both can be seen here. Your assignment is to implement all of the abstract methods of the BinaryTree class recursively. They are: . insert iterator (non-recursive) * remove . search You must also implement an Iterator inner class for the BinarySearchTree class. You must submit a modified BinarySearchTree.java file with your source code. Do not submit and do...

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25...

    Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25 items. The pseudocode for the ADT Sorted List Operations are provided on page 210. Use this information to create an ADT for handling a collection of Person objects, where each object will contain a Social Insurance Number (validate this), a first name, a last name, a gender and a data of birth. This implementation should prevent duplicate entries – that is, the Social Insurance...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT....

    Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT. Our goal is to implement the interface that is provided for this ADT. Notice that there are two interfaces: OrderedListADT builds on ListADT. In this homework, you'll only be responsible for the OrderedListADT. Figure 1: UML Overview 1 Requirements Create a doubly linked implementation of the OrderedListADT interface. Note that the book includes most of the source code for a singly linked implementation of...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list...

    Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list using bubble sort Do not modify the test code in each function. You can look at that code for some ideas for implementing the methods. import java.lang.Comparable; import java.util.*; public class IteratorExercise {       public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {        // first line to start you off        ListIterator<E> iit = c.listIterator(), jit;...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • JAVA CODING: (14 points) Abstract Class exercise You are given two abstract classes with no abstract...

    JAVA CODING: (14 points) Abstract Class exercise You are given two abstract classes with no abstract method in the NoGo.java file. The purpose of this exercise is to get familiar with abstract class and its object creation 4. Follow the steps below: (be sure common on each to score points) 1) 2) 3 points) Create a subclass Go1 with Nogo1 as its super class and create Go1() constructor 3) (2 points) Now inside the NoGo class create an instance of...

  • Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp....

    Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp. Add the ouput of the implementation. use recursive functions to traverse the tree - read the implementation notes on using helper functions. Please use C++ programming ////////////////////////////////////////////////////////////// #include "BSTree.h" template <typename DataType, class KeyType> BSTree<DataType, KeyType>::BSTreeNode::BSTreeNode ( const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr ) { } template < typename DataType, class KeyType > BSTree<DataType, KeyType>::BSTree () {    root = 0; } template...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT