1 / 15

CS203 Programming with Data Structures Queues and Stacks

CS203 Programming with Data Structures Queues and Stacks. Chengyu Sun California State University, Los Angeles. Queue. Queue properties Always insert at the end of the queue ( enqueue ) Always remove from the front of the queue ( dequeue ) First-In-First-Out (FIFO). Front of the queue.

shawngeorge
Download Presentation

CS203 Programming with Data Structures Queues and Stacks

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. CS203 Programming with Data StructuresQueues and Stacks Chengyu Sun California State University, Los Angeles

  2. Queue • Queue properties • Always insert at the end of the queue (enqueue) • Always remove from the front of the queue (dequeue) • First-In-First-Out (FIFO) Front of the queue End of the queue John Joe Jane Jake

  3. The Queue Interface • void enqueue( Object o ) • Object dequeue() • NoSuchElementException • Return and remove • Object front() • Return but not remove • boolean isEmpty() • void clear()

  4. Queue Example // assume queue is empty queue.enqueue( “John” ); queue.enqueue( “Joe” ); System.out.println( queue.front() ); // ?? System.out.println( queue.front() ); // ?? System.out.println( queue.front() ); // ?? System.out.println( queue.dequeue() ); // ?? System.out.println( queue.dequeue() ); // ?? System.out.println( queue.dequeue() ); // ??

  5. Create an Exception Class • Inherits from Exception • Inherits from RuntimeException

  6. Queue Implementations • Using LinkedList • Using ArrayList • What are the complexity of enqueue() and dequeue() in each case??

  7. Queue Implementation Using Circular Array • For simplicity, assume there will be at most N objects in queue at any time • enqueue() and dequeue() do not require shifting all the elements in the array

  8. Circular Array enqueue(4) 3 4 6 1 enqueue(2) 2 dequeue() 3 4 1 enqueue(5) 2 5 dequeue() 3 4 enqueue(6) 2 5 6 enqueue(7) 3 4 7 dequeue() 5 6 dequeue() 6 enqueue(1) 6 1 enqueue(3) 3 6 1

  9. Circular Array Implementation public class CircularArrayQueue implements Queue { Object elements[]; int front, end; int size; … }

  10. Stack properties Insertions (push) and removals (pop) happen only at the top of the stack First-In-Last-Out (FILO) Stack Top of the stack John Joe Jane Jake

  11. The Stack Interface • void push( Object o ) • Object pop() • NoSuchElementException • Return and remove • Object top() • Return but not remove • boolean isEmpty() • void clear()

  12. Stack Example // assume stack is empty stack.push( “John” ); stack.push( “Joe” ); System.out.println( stack.top() ); // ?? System.out.println( stack.top() ); // ?? System.out.println( stack.top() ); // ?? System.out.println( stack.pop() ); // ?? System.out.println( stack.pop() ); // ?? System.out.println( stack.pop() ); // ??

  13. Stack Implementations • Using list • Using array

  14. Stack Application – Balancing Symbols • Given a program (as a text file or a string), check whether (), [], and {} in the program are balanced

  15. Stack Application – Evaluating Postfix Expressions 6 5 2 3 + 8 * + 3 + * 6 5 5 8 * + 3 + * 6 5 40 + 3 + * 6 45 3 + * 6 48 * 288

More Related