Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture20.pdf

Avatar for William Albritton William Albritton
September 14, 2014
63

 lecture20.pdf

Avatar for William Albritton

William Albritton

September 14, 2014
Tweet

Transcript

  1. Memory Upload • Node-based queue • Algorithms for queue methods

    • Big-O for queue methods • A queue object in memory
  2. Implementing a Queue • We can either implement a queue

    using an array or linked nodes • Node implementation: • Use a circular linked list with reference to the end Node object
  3. Example Code • Check out the main() method of the

    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>();
  4. Example Code • This is another advantage of using an

    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>();
  5. Node-Based Queue • The LinkedQueue class has only one (1)

    data field, a variable that has the address of a node which points to the element at the end of the queue private Node<T> endNode = null;
  6. Node-Based Queue • Why don’t we have another data field

    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
  7. Node-Based Queue • When using 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)
  8. Constructor • The constructor for LinkedQueue class has no code

    within it • This is because the data field has already been initialized private Node<T> endNode = null;
  9. Constructor in Memory • The LinkedQueue object instantiated (created) by

    the constructor QueueInterface<Integer> queue = new LinkedQueue<Integer>(); queue endNode null
  10. Method empty() • Method empty() must check to see if

    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
  11. Big-O for empty() • Big-O for method empty() is O(1)

    • The simple if-else statement is not dependent on the number of nodes in the queue • In other words, the method has no loops, so the Big-O is O(1)
  12. Offer (Add) Algorithm 1. Instantiate (create) newEndNode 2. If empty

    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
  13. Offer (Add) Big-O • All steps are O(1), so Big-O

    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)
  14. Offer (Add) in Memory • Add 10 to an empty

    queue Integer number = new Integer(10); queue.offer(number); number queue endNode null 10
  15. Offer (Add) in Memory • Jump to method declaration, element

    parameter points to Integer object 10 public boolean offer(T element){ number element queue endNode null 10
  16. Offer (Add) in Memory • Make a Node local variable,

    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
  17. Offer (Add) in Memory • The if statement evaluates to

    true if(this.empty()){ number element newEndNode queue endNode null 10 data next null
  18. Offer (Add) in Memory • Point newEndNode to itself newEndNode.setNext(newEndNode);

    number element newEndNode queue endNode null 10 data next
  19. Offer (Add) in Memory • Point endNode to newEndNode endNode

    = newEndNode; number element newEndNode queue endNode null 10 data next
  20. Offer (Add) in Memory • Return to main() method Integer

    number = new Integer(10); queue.offer(number); number queue endNode 10 data next
  21. Offer (Add) in Memory • Offer integer 20 to the

    queue number = new Integer(20); queue.offer(number); number queue endNode 10 data next 20
  22. Offer (Add) in Memory • Jump to offer() method definition

    public boolean offer(T element) { number element queue endNode 10 data next 20
  23. Offer (Add) in Memory • Make new node pointing to

    20 Node<T> newEndNode = new Node<T>(element, null); number element queue newEndNode endNode 10 data next 20 data next null
  24. Offer (Add) in Memory • The if statement is false

    if(this.empty()) { number element queue newEndNode endNode 10 data next 20 data next null
  25. Offer (Add) in Memory • Point newEndNode's next to the

    front of the queue newEndNode.setNext(endNode.getNext()); number element queue newEndNode endNode 10 data next 20 data next
  26. Offer (Add) in Memory • Point next of the node

    endNode to the node newEndNode endNode.setNext(newEndNode); number element queue newEndNode endNode 10 data next 20 data next
  27. Offer (Add) in Memory • Point the variable endNode to

    the node newEndNode endNode = newEndNode; number element queue newEndNode endNode 10 data next 20 data next
  28. Offer (Add) in Memory • Return to line of code

    after offer() number = new Integer(20); queue.offer(number); number queue endNode 10 data next 20 data next
  29. Offer (Add) in Memory • Rearrange queue object so it

    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
  30. Offer (Add) in Memory • Offer integer 30 to the

    queue number = new Integer(30); queue.offer(number); number queue 30 endNode 10 data next 20 data next
  31. Offer (Add) in Memory • Jump to method declaration, element

    parameter points to Integer object 30 public boolean offer(T element){ number element queue 30 endNode 10 data next 20 data next
  32. Offer (Add) in Memory • Point data of newEndNode to

    30 Node<T> newEndNode = new Node<T>(element, null); number element queue newEndNode data next null 30 endNode 10 data next 20 data next
  33. data next Offer (Add) in Memory • Point newEndNode’s next

    to the front node in the queue newEndNode.setNext(endNode.getNext()); number element queue newEndNode null 30 endNode 10 data next 20 data next
  34. Offer (Add) in Memory • Point endNode’s next to the

    node newEndNode endNode.setNext(newEndNode); number element queue newEndNode data next 30 endNode 10 data next 20 data next
  35. data next endNode Offer (Add) in Memory • Point the

    variable endNode to the node newEndNode endNode = newEndNode; number element queue newEndNode 30 10 data next 20 data next
  36. Offer (Add) in Memory • Return to main() method and

    make our drawing nice and pretty number queue data next 30 endNode 10 data next 20 data next
  37. Offer (Add) in Memory • Add Integer 40 object to

    end of queue number = new Integer(40); queue.offer(number); number queue data next data next 30 endNode 10 data next 20 data next 40
  38. Peek Algorithm 1. If queue is NOT empty: a. Get

    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
  39. Peek Big-O • Add up the Big-O for each step

    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)
  40. • Assign number to null number = null; number queue

    data next Peek in Memory data next null 30 endNode 10 data next 20 data next 40
  41. • Returns address of front element number = queue.peek(); number

    queue data next Peek in Memory data next 30 endNode 10 data next 20 data next 40
  42. Poll (Remove) Algorithm 1. If queue is NOT empty: a.

    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
  43. Poll (Remove) Algorithm • Since the Big-O for each step

    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)
  44. Poll (Remove) in Memory • Assume that number is null

    number = queue.poll(); number queue data next null 30 endNode 10 data next 20 data next 40 data next
  45. • Jump to poll() method definition Node<T> frontNode = endNode.getNext();

    number Queue frontNode data next Poll (Remove) in Memory data next null 30 endNode 10 data next 20 data next 40
  46. • Set next of endNode to 2nd node endNode.setNext(frontNode.getNext()); number

    queue frontNode Poll (Remove) in Memory data next null 30 endNode 10 data next 20 data next 40 data next
  47. • Get address of front element T frontElement = frontNode.getData();

    number front- element queue frontNode data next Poll (Remove) in Memory null 30 endNode 10 data next 20 data next 40 data next
  48. data next Poll (Remove) in Memory • Return address of

    front element return frontElement; number front- element queue frontNode data next null 30 endNode 10 data next 20 data next 40
  49. data next Poll (Remove) in Memory data next 30 endNode

    10 data next 20 data next 40 • Assign front element to number number = queue.poll(); number front- element queue frontNode
  50. Poll (Remove) in Memory • All local variables popped off

    stack, and Java’s garbage collection cleans up heap number queue data next 30 endNode 10 data next 20 data next 40
  51. Memory Defragmenter • Using linked nodes to implement a queue

    • Algorithms for the methods of class LinkedQueue • Big-O for LinkedQueue methods • Memory for a LinkedQueue object
  52. Task Manager • Before the next class, you need to:

    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