1 / 16

Comp 352 – Fall 2012

Comp 352 – Fall 2012. Tutorial Session 3. Session Outline. Quick Overview on Stacks: Definition Methods Quick Overview on Queues : Definition Methods Implementation General Overview Highlights on Queue Implementation Using a Circular Array Problem Solving.

washi
Download Presentation

Comp 352 – Fall 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Comp 352 – Fall 2012 Tutorial Session 3

  2. Session Outline • Quick Overview on Stacks: • Definition • Methods • Quick Overview on Queues: • Definition • Methods • Implementation • General Overview • Highlights on Queue Implementation Using a Circular Array • Problem Solving

  3. The Stack ADT -Definition A stack is a collection of objects that are inserted and removed according to the last-in first-out (LIFO) principle. Objects can be inserted into a stack at any time, but only the most recently inserted (that is, "last") object can be removed at any time.

  4. The Stack ADT -Methods • push(e): Insert element e, to be the top of the stack. • pop(): Remove from the stack and return the top element on the stack; an error occurs if the stack is empty. • size(): Return the number of elements in the stack. • isEmpty(): Return a Boolean indicating if the stack is empty. • top(): Return the top element in the stack, without removing it; an error occurs if the stack is empty.

  5. The Queue ADT -Definition • A queue, a close "cousin" of the stack, is a collection of objects that are inserted and removed according to the first-in first-out (FIFO) principle. That is, elements can be inserted at any time, but only the element that has been in the queue the longest can be removed at any time. • We usually say that elements enter a queue at the rear and are removed from the front.

  6. The Queue ADT -Methods • enqueue(e): Insert element e at the rear of the queue. • dequeue(): Remove and return from the queue the object at the front; an error occurs if the queue is empty. • size(): Return the number of objects in the queue. • isEmpty(): Return a Boolean value that indicates whether the queue is empty. • front(): Return, but do not remove, the front object in the queue; an error occurs if the queue is empty.

  7. The Stack & Queue ADT -Implementations • The methods of both the Stack and Queue ADTs can be implemented via: • An array-based implementation • A generic linked list-based implementation • A circular array is used for the implementation of a Queue to avoid an O(n) performance for its Dequeue() operation, as well as an array-out-of-bounds error.

  8. Highlights on a Queue Implementation Using a Circular Array • The following variables are defined: • f: index to the cell storing the first element in the queue (candidate to be removed) • r: index to the next available cell • Operations: • size()  (N – f + r) modN • isEmpty()  f = r • enqueue(x)  r = (r + 1) modN • dequeue()  f = (f + 1) modN

  9. Problem Solving -R-5.3 Suppose an initially empty stack S has performed a total of 25 push operations, 12 top operations, and 10 pop operations, 3 of which generated StackEmptyExceptions, which were caught and ignored. What is the current size of S?

  10. Problem Solving -R-5.5 Describe the output of the following series of stack operations: push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7), push(6), pop(), pop(), push(4), pop(), pop().

  11. Problem Solving -R-5.6 Give a recursive method for removing all the elements in a stack.

  12. Problem Solving -R-5.8 Describe the output for the following sequence of queue operations: enqueue(5), enqueue(3), dequeue(), enqueue(2), enqueue(8), dequeue(), dequeue(), enqueue(9), enqueue(1), dequeue(), enqueue(7), enqueue(6), dequeue(), dequeue(), enqueue(4), dequeue(), dequeue().

  13. Problem Solving -R-5.9 Suppose an initially-empty queue Q has performed a total of 32 enqueueoperations, 10 front operations, and 15 dequeue operations, 5 of which generated QueueEmptyExceptions, which were caught and ignored. What is the current size of Q?

  14. Problem Solving -R-5.10 If the queue of the previous problem was implemented with an array of capacity N = 30, as described in the chapter, and it never generated a FullQueueException, what would be the current values of f and r?

  15. Problem Solving -C-5.4 Suppose Alice has picked three distinct integers and placed them into a stack S in random order. Write a short, straight line 0piece of pseudo-code (with no loops or recursion) that uses only one comparison and only one variable x, yet guarantees with probability 2/3 that at the end of this code the variable x will store the largest of Alice's three integers. Argue why your method is correct.

  16. Problem Solving -C-5.5 Describe how to implement the stack ADT using two queues. What is the running time of the push() and pop() methods in this case?

More Related