we can implement queue in java using arraylist in case of scala lists immutable how can implement queue using list in scala.somebody give me hint it.
with immutable lists, have return new list after modifying operation. once you've grasped that, it's straightforward. minimal (but inefficient) implementation queue immutable might be:
class queue[t](content:list[t]) { def pop() = new queue(content.init) def push(element:t) = new queue(element::content) def peek() = content.last override def tostring() = "queue of:" + content.tostring } val q= new queue(list(1)) //> q : lists.queue.queue[int] = queue of:list(1) val r = q.push(2) //> r : lists.queue.queue[int] = queue of:list(2, 1) val s = r.peek() //> s : int = 1 val t = r.pop() //> t : lists.queue.queue[int] = queue of:list(2)
Comments
Post a Comment