Contains standard libraries for ZenCode.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LinkedList.zs 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. export class LinkedList<T> {
  2. var first as Node?;
  3. var last as Node?;
  4. var size as usize : get;
  5. public get isEmpty as bool
  6. => first == null;
  7. public add(value as T) as void {
  8. if first == null {
  9. first = last = new Node(value);
  10. } else {
  11. val node = new Node(value);
  12. last.next = node;
  13. node.prev = last;
  14. last = node;
  15. }
  16. size++;
  17. }
  18. public clear() as void {
  19. first = last = null;
  20. size = 0;
  21. }
  22. [Precondition(ENFORCE, index < size, "Index out of bounds")]
  23. public [](index as usize) as T {
  24. var node = first;
  25. while index > 0 && node != null
  26. node = node.next;
  27. if node == null
  28. panic "index out of bounds";
  29. return node.value;
  30. }
  31. public implements Queue<T> {
  32. [Precondition(ENFORCE, first != null, "Cannot poll an empty queue")]
  33. poll() as T {
  34. val result = first.value;
  35. first = first.next;
  36. if first == null
  37. last = null;
  38. else
  39. first.prev = null;
  40. size--;
  41. }
  42. peek() as T? {
  43. return first == null ? null : first.value;
  44. }
  45. offer(value as T) as void
  46. => add(value);
  47. }
  48. private struct Node {
  49. var next as Node?;
  50. var prev as Node?;
  51. val value as T;
  52. this(value as T) {
  53. this.value = value;
  54. }
  55. }
  56. }