LinkedQueue.java program • This is almost same test as the test for the ArrayQueue class, as all we had to change is the name of the constructor! • QueueInterface<Integer> queue = new ArrayQueue<Integer>(5); • QueueInterface<Integer> queue = new LinkedQueue<Integer>();
interface: • You do NOT have to rewrite the all code in the client program when you use a different class implementation • QueueInterface<Integer> queue = new ArrayQueue<Integer>(5); • QueueInterface<Integer> queue = new LinkedQueue<Integer>();
to store the address of a node that points to the element at the front of in the queue? • Instead of keeping track of both the end node and front node, it is easier to write code for a class that has an end node and a circular list of nodes
to implement a queue, the end node points to the front node • The front node (1st node) points to the 2nd node in the queue • The 2nd node in the queue points to the 3rd node in the queue, etc. • Finally, the (n-1)th node points to the end node (the nth node)
the queue is empty or not • To do this, the method simply compares the Node variable endNode to null • If no nodes have been added, endNode will be null • If one or more nodes have been added, endNode will NOT be null
queue, point newEndNode to itself 3. If queue is not empty: a. Point newEndNode's next to the front node in the queue b. Point endNode's next to newEndNode 4. Point endNode to newEndNode
is O(1) 1. Instantiate newEndNode O(1) 2. If empty queue, point newEndNode to itself O(1) 3. If queue is not empty: O(1) a. Point newEndNode's next to front O(1) b. endNode's next = newEndNode O(1) 4. endNode = newEndNode O(1)
which points to a node pointing to object 10 Node<T> newEndNode = new Node<T>(element, null); number element newEndNode queue endNode null 10 data next null
looks nice and pretty • Variable endNode points to node with end element 20, and then this node points to node with front element 10 number queue endNode 10 data next 20 data next
the address of the front node (the end node points to the front node) b. Get the address of the front element c. Return address of the front element 2. If queue is empty: return null
of the algorithm: O(1) + O(1) + O(1) + O(1) + O(1) = O(1) 1. If queue is NOT empty: O(1) a. Get the address of the front node O(1) b. Get address of the front element O(1) c. Return address of front element O(1) 2. If queue is empty: return null O(1)
Get address of front node b. If only 1 node, make an empty queue c. If more than 1 node, set endNode's next to node after front node d. Return address of front element 2. If queue is empty: return null
is O(1), the sum is O(1) 1. If queue is NOT empty: O(1) a. Get address of front node O(1) b. If 1 node, make empty queue O(1) c. If > 1 node, endNode's = 2nd node O(1) d. Return address of front element O(1) 2. If queue is empty: return null O(1)
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture