stack.rig 989 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. -- Implementation of stack when the length of the stack
  2. -- and the list are stored both in a tree #stack_abstract.
  3. #STACKS
  4. $S:=#NEWSTACK(T);
  5. #PUSH(A $S);
  6. #PUSH(B $S);
  7. PRINT #TOP($S);
  8. #POP($S);
  9. #PUSH(C $S);
  10. #POP($S);
  11. #POP($S);
  12. PRINT $S;
  13. ##
  14. #stack_abstract
  15. <. size: #NUMBER,
  16. [ list : (. (+ $ELEMENT +) .) ]
  17. .>
  18. ##
  19. #NEWSTACK
  20. / RETURN 'stack'::<.size:0.> /
  21. -- This 'stack' is added for better understanding of printouts
  22. -- only.
  23. ##
  24. #PUSH $EL
  25. $STACK:=
  26. <. size : $size / $size+:=1 /,
  27. [ list : $list
  28. /IF #LEN($list)>=$size -> $list[$size]:=$EL
  29. ELSIF T -> $list!.:=$EL
  30. FI;
  31. RETURN T/
  32. ]
  33. .>
  34. / $STACK++:=<.list:(.$EL.).> /
  35. ##
  36. #PUSH2 $EL $S -- The same as push, without patterns
  37. /$S.size+:=1;
  38. IF $S.list ->
  39. IF #LEN($S.list)>=$S.size -> $S.list[$S.size]:=$EL
  40. ELSIF T -> $S.list!.:=$EL
  41. FI;
  42. ELSIF T->$S++:=<.list:(.$EL.).>
  43. FI;
  44. /##
  45. #POP $S
  46. / IF $S.size>0 -> $S.size+:=-1; FI; /
  47. ##
  48. #TOP $S
  49. / RETURN $S.list[$S.size] /
  50. ##