Free preview: 1 of 3 free topics used. You can open 2 more without an account.
Sign up free to unlock all 99An empty queue: head and tail both point to null.
Operations (8/20)
1class Queue {2Node head, tail; // Node: int val; Node next;34void enqueue(int x) {5Node n = new Node(x);6if (tail == null) head = tail = n;7else { tail.next = n; tail = n; }8}910int dequeue() {11if (head == null)12throw new RuntimeException("empty");13int x = head.val;14head = head.next;15if (head == null) tail = null;16return x;17}1819int peek() {20if (head == null)21throw new RuntimeException("empty");22return head.val;23}24}
Variables
Work done
Shortcuts: Space play/pause · ← → step · M sound