I need help with the Implementation of an Ordered List
(Template Files)
public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); }
import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private Node previous; private Node next; private Node ( Comparable value, Node previous, Node next ) { this.value = value; this.previous = previous; this.next = next; } } // Instance variables private Node head; // Representation of the empty list. public OrderedList() { // Your code here. } // Calculates the size of the list public int size() { // Remove line below and add your implementation. throw new UnsupportedOperationException( "not implemented yet!" ); } public Object get( int pos ) { // Remove line below and add your implementation. throw new UnsupportedOperationException( "not implemented yet!" ); } // Adding an element while preserving the order public boolean add( Comparable o ) { // Remove line below and add your implementation. throw new UnsupportedOperationException( "not implemented yet!" ); } //Removes one item from the position pos. public void remove( int pos ) { // Remove line below and add your implementation. throw new UnsupportedOperationException( "not implemented yet!" ); } // Knowing that both lists store their elements in increasing // order, both lists can be traversed simultaneously. public void merge( OrderedList other ) { // Remove line below and add your implementation. throw new UnsupportedOperationException( "not implemented yet!" ); } }
Here is a step-by-step approach for implementing the class OrderedList. This will be a top-down approach, focusing on the general organization of the class first (adding the variables, the nested class, as well as empty methods). Once, the overall implementation is complete, we will implement the methods one by one.
First, let’s create a template for the class. At this point, we are ignoring details such as the implementation of the body of the methods, and instead we are focusing on the necessary variables and the need for a static class called Node. This template needs to contain a static nested class called Node, the necessary instance variables, and empty definitions for all the methods of the interface OrderedStructure. You should not use a variable int size in this implementation. Also, the class Node should save a value of type Comparable
The compiler will not allow us to have empty declarations for the methods that return a result. To circumvent this problem, we could add a “dummy” return statement, such as returning the value -99 for the method size.
However, we may later forget to change the implementation, and this will cause all sorts of problems. Because of this, it is better to create an initial method that consists a throw statement.
int size(){ throw new UnsupportedOperationException("not implemented yet!"); }
Do the same for every method.
You can now compile the class and start working on the implementation of the methods one by one. Any attempt at using a method that has not been implemented yet will be signaled with the proper exception to remind us that we still have to implement that method. You can ask your TA for further information if needed.
You can now compile the class and start working on the implementation of the methods one by one. Any attempt at using a method that has not been implemented yet will be signaled with the proper exception to remind us that we still have to implement that method. You can ask your TA for further information if needed.
Create a test class called OrderedListTest; at this point it will contain only a main method that declares an OrderedList and creates an OrderedList object.
Make sure that your implementation is correct, i.e. has all the elements presented above and compiles. When you are done compiling all the files, proceed with the next step, implementing the method size().
We now have a complete implementation of the interface OrderedStructure.
The objectives are to learn how to traverse and transform a doubly linked list. Therefore, you are not allowed to use any auxiliary or existing methods, in particular add, for your implementation!
Solution:
.............................................................................................................................................................
==========================================================================
interface OrderedStructure<E extends Comparable<E>> { public abstract int size(); public abstract boolean add(E obj) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList<E> other ); } public class OrderedList<E extends Comparable<E>> implements OrderedStructure<E> { // Implementation of the doubly linked nodes (nested-class) private static class Node<E> { private E value; private Node<E> previous; private Node<E> next; private Node (E value, Node<E> previous, Node<E> next ) { this.value = value; this.previous = previous; this.next = next; } } // Instance variables private Node<E> head; // Representation of the empty list. public OrderedList() { head = new Node<E>(null, null, null); // dummy node. } // Calculates the size of the list public int size() { // head is always present. int count = 0; Node<E> ptr = head.next; while(ptr != null) { count++; ptr = ptr.next; } return count; } public Object get( int pos ) { Node<E> ptr = head.next; while(ptr != null && pos != 0) { pos--; ptr = ptr.next; } if(pos == 0 && ptr != null) { return ptr.value; } return null; } // Adding an element while preserving the order public boolean add(E o ) { Node<E> prev = head; while(prev.next != null && prev.next.value.compareTo(o) < 0) { prev = prev.next; } prev.next = new Node<>(o, prev, prev.next); if(prev.next.next != null) { prev.next.next.previous = prev.next.next; } return true; } //Removes one item from the position pos. public void remove( int pos ) { Node<E> prev = head; Node<E> ptr = head.next; while(ptr != null && pos != 0) { pos--; prev = ptr; ptr = ptr.next; } if(pos == 0 && ptr != null) { prev.next = ptr.next; if(ptr.next != null) { ptr.previous = prev; } } } private void addAfter(Node<E> oldNode, E value) { Node<E> newNode = new Node<>(value, oldNode, oldNode.next); if(oldNode.next != null) { oldNode.next.previous = newNode; } oldNode.next = newNode; } // Knowing that both lists store their elements in increasing // order, both lists can be traversed simultaneously. @Override public void merge(OrderedList<E> other) { Node<E> prev1 = head; Node<E> start1 = head.next; Node<E> start2 = other.head.next; while(start2 != null) { // we need to insert start2 value. while(start1 != null && start1.value.compareTo(start2.value) < 0) { prev1 = start1; start1 = start1.next; } addAfter(prev1, start2.value); start1 = prev1.next; start2 = start2.next; } } public void printList() { // head is always present. Node<E> ptr = head.next; while(ptr != null) { System.out.print(ptr.value + " "); ptr = ptr.next; } System.out.println(); } public static void main(String args[]) { OrderedList<Integer> list1 = new OrderedList<>(); list1.add(8); list1.add(18); list1.add(16); list1.add(23); list1.add(3); list1.add(7); list1.printList(); // remember remove taks position, not value list1.remove(0); list1.printList(); list1.remove(2); list1.printList(); System.out.print("List1: "); list1.printList(); OrderedList<Integer> list2 = new OrderedList<>(); list2.add(8); list2.add(18); list2.add(13); list2.add(71); list2.add(9); list2.add(15); System.out.print("List2: "); list2.printList(); list1.merge(list2); System.out.print("Merged List: "); list1.printList(); } } Please upvote. i did the extra credit ones too.
I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...
java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods commented with: // TODO: TIP: Do not just go from memory of your assignment implementation, be sure to consider carefully the constructor and method implementation provided to you. NOTE: You do not have to provide an implementation for any methods intentionally omitted public class Heap PriorityQueue implements PriorityQueue private final static int DEFAULT SIZE 10000 private Comparable [ ] storage private int currentSize: public...
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> { /** * Append an item to the end of the list * * @param item – item to be appended */ public void append(E item); /** * Insert an item at a specified index position * * @param item – item to be...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....
JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden methods @Override public int lastIndexOf(Object arg0) { required code } @Override public ListIterator<R> listIterator() { required code } @Override public ListIterator<R> listIterator(int arg0) { required code } PLEASE NOTE: Below are some of other overridden methods that are already implemented, they are meant to be examples of what the answers to the above questions for the 3 methods should...
Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...
Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...
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...