Question

Finish the code wherever it says TODO /**    * Node type for this list. Each...

Finish the code wherever it says TODO

/**
   * Node type for this list. Each node holds a maximum of nodeSize elements in an
   * array. Empty slots are null.
   */
   private class Node {
       /**
       * Array of actual data elements.
       */
       // Unchecked warning unavoidable.
       public E[] data = (E[]) new Comparable[nodeSize];

       /**
       * Link to next node.
       */
       public Node next;

       /**
       * Link to previous node;
       */
       public Node previous;

       /**
       * Index of the next available offset in this node, also equal to the number of
       * elements in this node.
       */
       public int count;

       /**
       * Adds an item to this node at the first available offset. Precondition: count
       * < nodeSize
       *
       * @param item element to be added
       */
       void addItem(E item) {
           if (count >= nodeSize) {
               return;
           }
           data[count++] = item;
           // useful for debugging
           // System.out.println("Added " + item.toString() + " at index " + count + " to
           // node " + Arrays.toString(data));
       }

       /**
       * Adds an item to this node at the indicated offset, shifting elements to the
       * right as necessary.
       *
       * Precondition: count < nodeSize
       *
       * @param offset array index at which to put the new element
       * @param item element to be added
       */
       void addItem(int offset, E item) {
           if (count >= nodeSize) {
               return;
           }
           for (int i = count - 1; i >= offset; --i) {
               data[i + 1] = data[i];
           }
           ++count;
           data[offset] = item;
           // useful for debugging
// System.out.println("Added " + item.toString() + " at index " + offset + " to node: " + Arrays.toString(data));
       }

       /**
       * Deletes an element from this node at the indicated offset, shifting elements
       * left as necessary. Precondition: 0 <= offset < count
       *
       * @param offset
       */
       void removeItem(int offset) {
           E item = data[offset];
           for (int i = offset + 1; i < nodeSize; ++i) {
               data[i - 1] = data[i];
           }
           data[count - 1] = null;
           --count;
       }
   }

   private class StoutListIterator implements ListIterator<E> {
       // constants you possibly use ...

       // instance variables ...

       /**
       * Default constructor
       */
       public StoutListIterator() {
           // TODO
       }

       /**
       * Constructor finds node at a given position.
       *
       * @param pos
       */
       public StoutListIterator(int pos) {
           // TODO
       }

       @Override
       public boolean hasNext() {
           // TODO
       }

       @Override
       public E next() {
           // TODO
       }

       @Override
       public void remove() {
           // TODO
       }

       // Other methods you may want to add or override that could possibly facilitate
       // other operations, for instance, addition, access to the previous element,
       // etc.
       //
       // ...
       //
   }

   /**
   * Sort an array arr[] using the insertion sort algorithm in the NON-DECREASING
   * order.
   *
   * @param arr array storing elements from the list
   * @param comp comparator used in sorting
   */
   private void insertionSort(E[] arr, Comparator<? super E> comp) {
       // TODO
   }

   /**
   * Sort arr[] using the bubble sort algorithm in the NON-INCREASING order. For a
   * description of bubble sort please refer to Section 6.1 in the project
   * description. You must use the compareTo() method from an implementation of
   * the Comparable interface by the class E or ? super E.
   *
   * @param arr array holding elements from the list
   */
   private void bubbleSort(E[] arr) {
       // TODO
   }

}

Homework Answers

Answer #1

1

So I need to remove empty (undefined) items from the multidimensional array. Atm my code looks like this (it's a method I run so that's why i am using this:

f: function(arr) {
    var __ = this;
    arr = arr.filter(function(item) {
        return Array.isArray(item) ? __.f(item) : typeof(item) !== "undefined";
    });
return arr;
}

but if i run console.log(myObject.f([1, 2, , , , , 3, 4, [5, , , , , ], 6, , , , 8, 3, [[[], 9]]])); i get [ 1, 2, 3, 4, [ 5, , , , ], 6, 8, 3, [ [ [], 9 ] ] ] and that is kinda weird result. I goes pretty well for the first layer but removes only one undefined from inner layers. Also I would like to remove a subarray that consists of no items.

Know the answer?
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
Similar Questions
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT