Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Note : Can someone help me with the private E get(int index, int currentIndex, Node n) method. The code is not running. Also I need

Note: Can someone help me with the private E get(int index, int currentIndex, Node n)method. The code is not running. Also I need help with the public void add(int index, E element) method too. Please use the parameters provided with the code to solve. 
package edu.ust.cisc; import java.util.Iterator; import java.util.NoSuchElementException; public class CiscSortedLinkedList> implements CiscList { /** * A reference to this list's dummy node. Its next reference should refer to the node containing the first element * in this list, or it should refer to itself if the list is empty. The next reference within the node containing * the last element in this list should refer to dummy, thus creating a cyclic list. */ private Node dummy; /** * Number of elements in the list. */ private int size; /** * Constructs an empty CiscSortedLinkedList instance with a non-null dummy node whose next reference refers to * itself. */ public CiscSortedLinkedList() { dummy = new Node<>(null, null); dummy.next = dummy; } /** * Returns the number of elements in this list. * * @return the number of elements in this list */ @Override public int size() { return size; } /** * Returns {@code true} if this list contains no elements. * * @return {@code true} if this list contains no elements */ @Override public boolean isEmpty() { return size==0; } /** * Returns {@code true} if this list contains the specified element (compared using the {@code equals} method). * This implementation should stop searching as soon as it is able to determine that the specified element is not * present. * * @param o element whose presence in this list is to be tested * @return {@code true} if this list contains the specified element * @throws NullPointerException if the specified element is null */ @Override public boolean contains(Object o) { if(o==null){ throw new NullPointerException(); } return containsHelper(o,dummy.next); } private boolean containsHelper(Object o, Node node){ if(node== dummy){ return false; } else if(o.equals(node.data)){ return true; } else{ return containsHelper(o,node.next); } } /** * Returns an iterator over the elements in this list in proper sequence. * * @return an iterator over the elements in this list in proper sequence */ @Override public Iterator iterator() { //return new Iterator(){ //private Node curr = dummy.next; //private Node prev = dummy; //private boolean canRemove = false; return null; } /** * Returns an array containing all of the elements in this list in proper sequence (from first to last element). * 

The returned array will be "safe" in that no references to it are maintained by this list. (In other words, * this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify * the returned array. * * @return an array containing all of the elements in this list in proper sequence */ @Override public Object[] toArray() { Object[] arr = new Object[size]; int i = 0; for(Node current = dummy;current!=null;current= current.next){ arr[i++] = current.data; } return arr; } /** * {@link #toArray} recursive helper method. Adds the element contained in the specified node to the specified index * in the specified array. Recursion stops when the specified node is the dummy node. * * @param arr the array into which each element in this list should be added * @param index the index into which the next element in this list should be added * @param n the node containing the next element in this list to add to the array */ private void toArray(Object[] arr, int index, Node n) { } /** * Adds the specified element to its sorted location in this list. * *

Lists may place the specified element at arbitrary locations if desired. In particular, an ordered list will * insert the specified element at its sorted location. List classes should clearly specify in their documentation * how elements will be added to the list if different from the default behavior (end of this list). * * @param value element to be added to this list * @return {@code true} * @throws NullPointerException if the specified element is null */ @Override public boolean add(E value) { return false; } /** * {@link #add} recursive helper method. Adds the specified value to the list in a new node following the specified * node (if that is the appropriate location in the list). * * @param value element to be added to this list * @param n a reference to the node possibly prior to the one created by this method */ private void add(E value, Node n) { } /** * Removes the first occurrence of the specified element from this list, if it is present. If this list does not * contain the element, it is unchanged. Returns {@code true} if this list contained the specified element. This * implementation should stop searching as soon as it is able to determine that the specified element is not * present. * * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element * @throws NullPointerException if the specified element is null */ @Override public boolean remove(Object o) { if(o==null){ throw new NullPointerException(); } Node curr = dummy.next; Node prev = dummy; while (curr != dummy && !o.equals(curr.data)) { prev = curr; curr = curr.next; } if (curr == dummy) { return false; } else { System.out.println("Removing Data:"+curr.data); prev.next = curr.next; size--; return true; } } /** * {@link #remove} recursive helper method. Removes the node following n if it contains the specified element. This * implementation should stop searching as soon as it is able to determine that the specified element is not * present. * * @param o element to be removed from this list, if present * @param n a reference to the node prior to the one possibly containing the value to remove * @return */ private boolean remove(Object o, Node n) { return false; } /** * Removes all of the elements from this list. The list will be empty after this call returns. @Override public void clear() { dummy.next=dummy; size=0; } /** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException if the index is out of range */ @Override public E get(int index) { if(index<0 || index>=size){ throw new IndexOutOfBoundsException(); } Node curr = dummy.next; for(int i=0;i n) { if(index<0 || index>=size){ throw new IndexOutOfBoundsException(); } Node curr = head; for(int i=0;i next_curr=curr.next; curr.next=n; while(n.next) { n = n.next; //finding end of n headed linked list n.next = next_curr;//adding rest linked list at end of n headed linked list } if(index==0) { return n; } else { return head; } } /** * * @param index index of the element to replace * @param element element to be stored at the specified position * @return the element previously at the specified position * @throws UnsupportedOperationException if the {@code set} operation is not supported by this list */ @Override public E set(int index, E element) { throw new UnsupportedOperationException(); } /** * This operation is not supported by CiscSortedLinkedList. * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws UnsupportedOperationException if the {@code set} operation is not supported by this list */ @Override public void add(int index, E element) { } /** * Appends all elements in the specified list to the end of this list, in the order that they are returned by the * specified list's iterator. * * @param c list containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws NullPointerException if the specified list is null */ @Override public boolean addAll(CiscList c) { return false; } /** * Removes the element at the specified position in this list. Shifts any subsequent elements to the left * (subtracts one from their indices). Returns the element that was removed from the list. * * @param index the index of the element to be removed * @return the element previously at the specified position * @throws IndexOutOfBoundsException if the index is out of range */ @Override public E remove(int index) { return null; } /** * {@link #remove} recursive helper method. Removes the node following n if it contains the element at the specified * index. * * @param index the index of the element to be removed * @param currentIndex the index of the node parameter * @param n the node containing the element at currentIndex * @return */ private E remove(int index, int currentIndex, Node n) { return null; } /** * Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not * contain the element (compared using the {@code equals} method). This implementation should stop searching as * soon as it is able to determine that the specified element is not present. * @param o element to search for * @return the index of the first occurrence of the specified element in this list, or -1 if this list does not * contain the element * @throws NullPointerException if the specified element is null */ @Override public int indexOf(Object o) { if(o==null){ throw new NullPointerException(); } return indexOf(o,0, dummy.next); } /** * {@link #indexOf} recursive helper method. Returns currentIndex if the element contained in the node parameter is * equal to the specified element to search for. This implementation should stop searching as soon as it is able to * determine that the specified element is not present. * * @param o element to search for * @param currentIndex the index of the node parameter * @param n the node containing the element at currentIndex * @return the index of the first occurrence of the specified element in this list, or -1 if this list does not * contain the element */ private int indexOf(Object o, int currentIndex, Node n) { if(n.data.equals(o)){ return currentIndex; } else if(n==dummy){ return -1; } else if(n.data.compareTo((E)o)>0){ return -1; } else{ return indexOf(o,currentIndex+1,n.next); } } /** * Returns a string representation of this list. This string should consist of a comma separated list of values * contained in this list, in order, surrounded by square brackets (examples: [3, 6, 7] and []). * * @return a string representation of this list */ public String toString() { return null; } /** * {@link #toString} recursive helper method. Returns a string representation of this list, beginning with the * element in the specified node * * @param n the node containing the next element to append to the string * @return a string representation of this list, beginning with the element in the specified node */ private String toString(Node n) { return null; } private static class Node { private E data; private Node next; private Node(E data, Node next) { this.data = data; this.next = next; } } private class CiscLinkedListIterator implements Iterator { /** * A reference to the node containing the next element to return, or to the dummy node if there are no more * elements to return. */ private Node nextNode; /** * Constructs an iterator ready to return the first element in the list (if present). */ public CiscLinkedListIterator() { } /** * Returns {@code true} if the iteration has more elements. (In other words, returns {@code true} if * {@link #next} would return an element rather than throwing an exception.) * * @return {@code true} if the iteration has more elements */ @Override public boolean hasNext() { return false; } /** * Returns the next element in the iteration. * * @return the next element in the iteration * @throws NoSuchElementException if the iteration has no more elements */ @Override public E next() { return null; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions