Top Banner
ISOM MIS 215 Module 3 – Stacks and Queues
21

ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

Dec 28, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

MIS 215 Module 3 – Stacks and Queues

Page 2: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Where are we?

2

Intro toJava, Course

Java lang.basics

Arrays

Introduction

Newbie Programmers Developers ProfessionalsDesigners

MIS215

Binary Search

SearchTechniques Sorting Techniques

Bubblesort

Basic Algorithms

Fast Sorting algos(quicksort, mergesort)

Hashtables

Graphs,Trees

LinkedLists

Stacks,Queues

List Structures Advanced structures

Page 3: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

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

Page 4: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

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

Page 5: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

What are some stacks?

Page 6: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Stacks: Concepts

Q

Q A

Q

APush box A onto stack:

Pop a box from stack:

Pop a box from stack: (empty)

Push box Q onto empty stack:

Page 7: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

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 retrievingsize, full, empty

Page 8: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

• 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

Implementation of Stacks

Page 9: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Contiguous Stacks

----------

DianaBarbBobRickJimSue

MaryJohnTom

Stack

012345678910111213

top

bottom

----------------

RickJimSue

MaryJohnTom

Stack

012345678910111213

top

----------

DianaBarbBobRickJimSue

MaryJohnTom

Stack

012345678910111213

top

bottom

After 9 “pushes” onto an empty stack After 3 “pops”

Page 10: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

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

Page 11: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Queue ADT

• Propertiesitems are ordered by arrival (so not sorted)you access the oldest one inserted

• Actionsinsert/enqueue/addremove/dequeue/deletepeek, see next item w/o removingsize, full, empty

Page 12: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

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)

Page 13: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Example of a Circular Array

This is only a logical circle!

Page 14: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

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?

Page 15: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Stack and Queue Complexity

Operation Stack Queue

create/constructor O(1) O(1)

size

clear

push N/A

pop N/A

peek

Insert/enqueue N/A

remove/dequeue N/A

Page 16: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Priority queues

• keep the items ordered by priority in the queueso, clearly, not FIFO, right?can an item be starved?

Page 17: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Priority queue efficiency

• Typical Array Implementationinsertion: O(N)

• you gotta move things around to keep them ordered

deletion: O(1)

Page 18: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Discussion – Uses of Stacks and Queues

• Reversing words

• Checking for palindromes

• Matching brackets in expressions

• CPU scheduling in Operating Systems

• Later… Traversal of Trees

Page 19: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

How would you reverse a word?

• Input: structure Output: erutcurts

Page 20: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

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?

Page 21: ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Parsing steps