A queue is a standard FIFO data structure. See wikipedia
In this exercise, we implement a queue with a pair of two lists (front, back)
such that front @ List.rev back
represents the sequence of elements in the queue.
-
Write a function
is_empty : queue -> bool
such thatis_empty q
is true if and only ifq
has no element. -
Write a function
enqueue : int -> queue -> queue
such thatenqueue x q
is the queue asq
except thatx
is at the end of the queue. -
Write a function
split : int list -> int list * int list
such thatsplit l = (front, back)
wherel = back @ List.rev front
and the length ofback
andfront
isList.length l / 2
orList.length l / 2 + 1
-
Write a function
dequeue : queue -> int * queue
such thatdequeue q = (x, q')
wherex
is the front element of the queueq
andq'
corresponds to remaining elements. This function assumes thatq
is non empty.
type queue = int list * int list
let is_empty (front, back) =
"Replace this string with your implementation." ;;
let enqueue x (front, back) =
"Replace this string with your implementation." ;;
let split l =
"Replace this string with your implementation." ;;
let dequeue (front, back) =
"Replace this string with your implementation." ;;