I must implement all methods in the case class node. All implementations must run around O(logn) except for fill and reverse those can be linear/ O(n). Rest is a list of pairs, and +: is cons/prepend method. The Due date for this project is April 18th at 1200 PM EST. Please need help badly and need support! The attached file has some that i tried to complete but are not completely right.
The methods below with Question Marks following the equal signs are the ones that need to be implemented. Case Class Node is what defines this PList. The PList can either be empty as the first Case Class States, or Node which means it is not empty.It contains. This is the scala language and i have defined what the trait of a PList[A] is and what case class empty is i just need help writing the methods that are followed by questions marks below. Ill provide a brief description of each method....
+: is prepend which means that you would add the input to the front of the list and adjust it accordinglytail means that the first element is taken away and then you return the rest of the list adjusted accordinglyapply means is just indexing. so given an integer return what element is at that index.Updated is given and integer and an element find that index and update it to the element passed into the functionsize is just length of the listfill is given n the size of list and x which is an element fill the list with the same element for length nreverse means if list is (1,2,(3,4)) it should return (4,3,(2,1))
below is the cleaned up code not in the file. I attached the file of stuff that i have tried to implement with tail and +: and apply.trait PList[A]{ def isEmpty: Boolean def +:(x:A):PList[A] def tail: PList[A] def apply(i: Int): A def updated(i: Int, x:A): PList[A] def size: Int def fill(n:Int, x:A):PList[A] def reverse(p:PList[A]):PList[A] }
case class Empty[A]() extends PList[A]{ def isEmpty: Boolean = true def +:(x:A): PList[A] = Node(x,None,Empty[(A,A)]) def tail: PList[A] = throw new UnsupportedOperationException("tail of Empty") def apply(i: Int):A = throw new IndexOutOfBoundsException(s"apply of Empty") def updated(i: Int, x:A): PList[A] = throw new IndexOutOfBoundsException(s"updated of Empty") def size: Int = 0 def fill(n:Int, x:A) = ??? def reverse(p:PList[A]):PList[A] = throw new IndexOutOfBoundsException(s"reversed an Empty")
} case class Node[A](fst: A, snd: Option[A], rest: PList[(A,A)]) extends PList[A]{
def isEmpty: Boolean = false def +:(x:A): PList[A] = ??? def tail: PList[A] = ??? def apply(i:Int):A = ??? def updated(i: Int, x:A): PList[A] = ??? def size: Int = ??? def fill(n:Int, x:A): PList[A] = ??? def reverse(p:PList[A]):PList[A] = ???}