1 / 21

MIS 215 Module 3 – Stacks and Queues

MIS 215 Module 3 – Stacks and Queues. Where are we?. MIS215. Basic Algorithms. Introduction. List Structures. Advanced structures. Search Techniques. Intro to Java, Course. Sorting Techniques. Java lang. basics. Linked Lists. Hashtables. Binary Search. Graphs, Trees . Stacks,

patch
Download Presentation

MIS 215 Module 3 – Stacks and Queues

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. MIS 215 Module 3 – Stacks and Queues

  2. Where are we? MIS215 Basic Algorithms Introduction List Structures Advanced structures Search Techniques Intro to Java, Course Sorting Techniques Java lang. basics Linked Lists Hashtables Binary Search Graphs, Trees Stacks, Queues Arrays Bubblesort Fast Sorting algos (quicksort, mergesort) Newbie Programmers Designers Developers Professionals

  3. Today’s buzzwords • Stack • A data structure where items can be added to the top and removed from the top • A LIFO (Last In, First Out) Structure • Queue • A data structure which can be used to add elements at one end, and remove from another • A FIFO (First In, First Out) Structure • Priority Queue • A queue where highest priority items are removed first • Expression Notations • Infix – standard notation A+B+C-D • PostFix – notation for better computability AB+C+D- • PreFix – Functional Notation -++ABCD

  4. Stacks.. • Think of a postman’s mail box... • Before delivery, they “push” items on to it, i.e., put them on the top of the stack • they keep going to the bottom • During delivery, they “pop” items off it (can’t reach down) • they are the last items put on • Other “real-life” scenarios? • Is it possible you would never pop off the first item you pushed on? • that’s starvation

  5. What are some stacks?

  6. Q A Q Q Stacks: Concepts Push box Q onto empty stack: A Push box A onto stack: Pop a box from stack: Pop a box from stack: (empty)

  7. Stack ADT • Properties: • items are ordered by arrival (so not sorted) • can only access the last one stored • commonly called Last-In-First-Out (LIFO) • Actions • “push”: store an item • “pop”: retrieve an item • “peek”: see next item w/o retrieving • size, full, empty

  8. Implementation of Stacks • Implementation of stacks, like the one for any other data structures, includes two basic approaches: • Contiguous implementation • Stack entries are stored in arrays • Data are accessed by using indices • linked implementation • Stacks entries are stored in linked lists • Data are accessed by using references

  9. Contiguous Stacks Stack Stack Stack bottom bottom 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Tom Tom 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Tom John John John Mary Mary Mary Sue Sue Sue Jim Jim Jim Rick Rick Rick top Bob Bob -- Barb Barb -- Diana Diana top top -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- After 3 “pops” After 9 “pushes” onto an empty stack

  10. Queues • You know what a queue is -- it’s a line you wait in • Items get added to the end (rear, back, tail) • Items come off the front (head) • commonly called First-In-First-Out (FIFO) • I personally prefer head/tail or front/rear • Eventually, an item comes off, right? • no starvation

  11. Queue ADT • Properties • items are ordered by arrival (so not sorted) • you access the oldest one inserted • Actions • insert/enqueue/add • remove/dequeue/delete • peek, see next item w/o removing • size, full, empty

  12. Implementation of Queues • The Physical model • A linear array with the front always in the first position • Whenever front is deleted all the entries moved up. • A linear array with two indices always increasing. • A circular array with front and rear indices and one position left vacant. • A circular array with front and rear indices and a Boolean variable to indicate fullness (or emptiness)

  13. Example of a Circular Array This is only a logical circle!

  14. Efficiencies... • What’s the complexity? • Remember what we mean by complexity? • how is the amount of work dependent upon the size of the input data?

  15. Stack and Queue Complexity

  16. Priority queues • keep the items ordered by priority in the queue • so, clearly, not FIFO, right? • can an item be starved?

  17. Priority queue efficiency • Typical Array Implementation • insertion: O(N) • you gotta move things around to keep them ordered • deletion: O(1)

  18. Discussion – Uses of Stacks and Queues • Reversing words • Checking for palindromes • Matching brackets in expressions • CPU scheduling in Operating Systems • Later… Traversal of Trees

  19. How would you reverse a word? • Input: structure Output: erutcurts

  20. In-class exercise: “Parsing” • One simple thing that a parser needs to do is to check for matched brackets { } [ ] ( ) • Problem: Write a Java program that takes in a program and decides if the program is valid or not • How would you solve it?

  21. Parsing steps

More Related