

A queue is a structure where values are added from behind and removed from the
front
          ----------
 insert-> ||||||||||->remove
          ----------

Queues can be modelled as arrays 

>data Queue a = Q [a]

>insert (Q xs) val = Q (val:xs)

>remove (Q xs)     = (last xs,Q init(xs))


>insert :: O(1)

>remove :: O(n)



We can do better if queues are implented as two list joined at the tails 

Hence insertion and removal are operations that involve removing just the 
topmost entry

>data Queue a = Q [a] [a]

>insert (Q [] []) val = (Q [] [a])
>insert (Q l1 l2) val = (Q (val:l1) l2)

>remove (Q ls (r:rs))
>       |rs==[])   =(r,Q [] (reverse ls))
>       |otherwise =(r,Q ls rs)

The complexity is less in the average case

However the complexity turns out to be O(n) in the worst case


  
 
