datatype ring=
New of int
| con of ring * int;
fun empty (New(n)) = true
| empty (con(S,i)) = false
;
fun max (New(n)) = n
| max (con(S,i)) = max(S);
fun size (New(n)) = 0
| size (con(B,i)) = if size(B)=max(B)
then max(B)
else size(B)+1
;
fun full (New(m)) = m=0
| full (con(S,i)) = if size(S)>=max(S)-1
then true
else false ;
exception topEmptyStack;
fun rem (New(n)) = New(n)
| rem (con(New(n),i)) = New(n)
| rem (con(S,i))= con(rem(S),i);
fun front (New(n))= raise topEmptyStack
| front(con(New(n),i))= i
| front(con(S,i)) = if full(S)
then i
else front(S);
fun add(New(n),i)= con(New(n),i)|
add(S,i)= if full(S)
then con(rem(S),i)
else con(S,i);
val ns5 = New(5);
val s1 = con(ns5,1);
val s2 = con(s1,2);
val s3 = con(s2,3);
val s4 = add(s3, 4);
val s5 = add(s4, 4);
val s6 = add(s5, 5);
val s7 = add(s6, 6);
New of int
| con of ring * int;
fun empty (New(n)) = true
| empty (con(S,i)) = false
;
fun max (New(n)) = n
| max (con(S,i)) = max(S);
fun size (New(n)) = 0
| size (con(B,i)) = if size(B)=max(B)
then max(B)
else size(B)+1
;
fun full (New(m)) = m=0
| full (con(S,i)) = if size(S)>=max(S)-1
then true
else false ;
exception topEmptyStack;
fun rem (New(n)) = New(n)
| rem (con(New(n),i)) = New(n)
| rem (con(S,i))= con(rem(S),i);
fun front (New(n))= raise topEmptyStack
| front(con(New(n),i))= i
| front(con(S,i)) = if full(S)
then i
else front(S);
fun add(New(n),i)= con(New(n),i)|
add(S,i)= if full(S)
then con(rem(S),i)
else con(S,i);
val ns5 = New(5);
val s1 = con(ns5,1);
val s2 = con(s1,2);
val s3 = con(s2,3);
val s4 = add(s3, 4);
val s5 = add(s4, 4);
val s6 = add(s5, 5);
val s7 = add(s6, 6);