CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Similar documents
CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CPSC 221: Algorithms and Data Structures Lecture #0: Introduction. Come up and say hello! Fibonacci. Fibonacci. Fibonacci. Fibonacci. (Welcome!

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Unit Outline. Course Work


Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Lecture 3: Stacks & Queues

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

CSE 332: Data Abstractions. Ruth Anderson Spring 2014 Lecture 1

CSC148 Week 2. Larry Zhang

12 Abstract Data Types

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Name CPTR246 Spring '17 (100 total points) Exam 3

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Standard ADTs. Lecture 19 CS2110 Summer 2009

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

Stacks and Queues. CSE 373 Data Structures Lecture 6

LIFO : Last In First Out

Lecture 4 Stack and Queue

csci 210: Data Structures Stacks and Queues

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

STACKS AND QUEUES. Problem Solving with Computers-II

ITI Introduction to Computing II

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Lecture Data Structure Stack

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Apply to be a Meiklejohn! tinyurl.com/meikapply

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Chapter 18: Stacks And Queues

1 P age DS & OOPS / UNIT II

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

== isn t always equal?

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Stacks Fall 2018 Margaret Reid-Miller

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CSC148H Week 3. Sadia Sharmin. May 24, /20

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

ADTs Stack and Queue. Outline

Basic Data Structures

CSE373: Data Structures and Algorithms Lecture 1: Introduc<on; ADTs; Stacks/Queues

CS24 Week 4 Lecture 2

Stacks and queues (chapters 6.6, 15.1, 15.5)

3137 Data Structures and Algorithms in C++

COURSE MECHANICS. Welcome! CSE 332 Data Abstractions: Introduction and ADTs. Concise to-do list. Today in Class. Instructor: Kate Deibel

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

Chapter 18: Stacks And Queues

Data Abstraction and Specification of ADTs

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

CMSC 132: Object-Oriented Programming II. Stack and Queue

Basic Data Structures 1 / 24

Queues Fall 2018 Margaret Reid-Miller

Stacks and Queues

Queues. Stacks and Queues

BBM 201 DATA STRUCTURES

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Motivation for Queues

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Information Science 2

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Linked Structures. See Section 3.2 of the text.

CS301 - Data Structures Glossary By

CSC 1052 Algorithms & Data Structures II: Queues

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Postfix (and prefix) notation

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

CS 206 Introduction to Computer Science II

UNIT-2 Stack & Queue

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CS W3134: Data Structures in Java

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Ashish Gupta, Data JUET, Guna

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

What Can You Use a Queue For?

infix expressions (review)

cs Java: lecture #6

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

CSE 143. Lecture 4: Stacks and Queues

Data Structure. Recitation VII

CS 216 Exam 1 Fall SOLUTION

Chapter 9 STACK, QUEUE

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Transcription:

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1

Lab 1 available very soon! Instructions for Lab 1 will be posted on course webpage very soon: http://www.ugrad.cs.ubc.ca/~cs221 Labs start on Monday. Read instructions and do any pre-labs before your lab section.

Today s Outline Abstract Data Types and Data Structures Queues Stacks Abstract Data Types vs. Data Structures 3

What is an Abstract Data Type? Abstract Data Type (ADT) - Formally: Mathematical description of an object and the set of operations on the object In Practice: The interface of a data structure, without any information about the implementation 4

Data Structures Algorithm A high level, language independent description of a step-by-step process for solving a problem Data Structure A set of algorithms which implement an ADT Don t get too obsessed with this distinction. Let s look at some examples 5

Queue ADT Queue operations create destroy enqueue dequeue is_empty G enqueue F E D C B dequeue Queue property: if x is enqueued before y is enqueued, then x will be dequeued before y is dequeued. FIFO: First In First Out A 6

Why is it called a queue?

Applications of Queues Hold jobs for a printer Store packets on network routers Make waitlists fair Breadth first search Etc. etc. etc. Basically, any time you need to hold a bunch of stuff for a bit, where you want to keep them in order. 8

Abstract Queue Example enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue dequeue enqueue E dequeue 9

Implementing Queues Many different ways to do this! What would you do?

Circular Array Q Data Structure Q 0 size - 1 b c d e f front back void enqueue(object x) { Q[back] = x back = (back + 1) % size Object dequeue() { x = Q[front] front = (front + 1) % size return x bool is_empty() { return (front == back) bool is_full() { return front == (back + 1) % size This is pseudocode. Do not correct my semicolons 11

Circular Array Q Example enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue dequeue enqueue E dequeue What are the final contents of the array? 12

enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue dequeue enqueue E dequeue Circular Array Q Example Assuming we can distinguish full and empty (could add a boolean) What are the final contents of the array? 13

Linked List Q Data Structure b c d e f front back void enqueue(object x) { if (is_empty()) front = back = new Node(x) else back->next = new Node(x) back = back->next Object dequeue() { assert(!is_empty) return_data = front->data temp = front front = front->next delete temp return return_data bool is_empty() { return front == null 14

Linked List Q Data Structure b c d e f front back void enqueue(object x) { if (is_empty()) front = back = new Node(x) else back->next = new Node(x) back = back->next Object dequeue() { assert(!is_empty) return_data = front->data temp = front front = front->next delete temp return return_data What s with the red text? bool is_empty() { return front == null 15

Circular Array vs. Linked List Which is better? Why? 16

Circular Array vs. Linked List Which is better? Why? They both have plusses and minuses! In general, many different data structures can implement an ADT, each with different trade-offs. You must pick the best for your needs. 17

Stack ADT Stack operations create destroy push pop top is_empty Stack property: if x is pushed before y is pushed, then x will be popped after y is popped LIFO: Last In First Out 18

Stack operations create destroy push pop top is_empty Stack ADT Stack property: if x is pushed before y is pushed, then x will be popped after y is popped LIFO: Last In First Out push F F 19

Stack operations create destroy push pop top is_empty Stack ADT Stack property: if x is pushed before y is pushed, then x will be popped after y is popped LIFO: Last In First Out push E E F 20

Stack operations create destroy push pop top is_empty Stack ADT Stack property: if x is pushed before y is pushed, then x will be popped after y is popped LIFO: Last In First Out push D D E F 21

Stack operations create destroy push pop top is_empty Stack ADT Stack property: if x is pushed before y is pushed, then x will be popped after y is popped LIFO: Last In First Out push C C D E F 22

Stack operations create destroy push pop top is_empty Stack ADT Stack property: if x is pushed before y is pushed, then x will be popped after y is popped LIFO: Last In First Out pop D E F C 23

Stack operations create destroy push pop top is_empty Stack ADT Stack property: if x is pushed before y is pushed, then x will be popped after y is popped LIFO: Last In First Out pop E F D 24

Stack operations create destroy push pop top is_empty Stack ADT Stack property: if x is pushed before y is pushed, then x will be popped after y is popped LIFO: Last In First Out pop F E 25

Why use a stack? Can you think of anything in real life where you want LIFO instead of FIFO?

Why use a stack? Can you think of anything in real life where you want LIFO instead of FIFO? Handling interruptions? Reversing the order of things?

Stacks in Practice Function call stack Removing recursion Balancing symbols (parentheses) Depth first search 28

Array Stack Data Structure S 0 size - 1 f e d c b back void push(object x) { assert(!is_full()) S[back] = x back++ Object top() { assert(!is_empty()) return S[back - 1] Object pop() { assert(!is_empty()) back-- return S[back] bool is_empty() { return back == 0 bool is_full() { return back == size 29

Linked List Stack Data Structure b c d e f back void push(object x) { temp = back back = new Node(x) back->next = temp Object top() { assert(!is_empty()) return back->data Object pop() { assert(!is_empty()) return_data = back->data temp = back back = back->next delete temp return return_data bool is_empty() { return back == null 30

Data structures you should already know (a bit) Arrays Linked lists Trees Queues Stacks 31

Abstract Data Types vs. Data Structures As mentioned before, ADT tells you what operations are available, but does not say anything about how implemented. Data structure consists of algorithms and memory layout to implement the ADT. Algorithms are language-independent. How does this map onto code? 32

Theoretically ADTs vs. Data Structures in Code Implementation abstract base class (or Java interface) describes ADT inherited implementations implement data structures can change data structures transparently (to client code) Practice different implementations sometimes suggest different interfaces (generality vs. simplicity) performance of a data structure may influence form of client code (time vs. space, one operation vs. another) 33

Why so many data structures? Ideal data structure: fast, elegant, memory efficient Generates tensions: time vs. space performance vs. elegance generality vs. simplicity one operation s performance vs. another s Dictionary ADT list binary search tree AVL tree Splay tree B tree Red-Black tree hash table 34

CS 221 ADT Presentation Algorithm Present an ADT Motivate with some applications Repeat a bunch of times: develop a data structure for the ADT analyze its properties efficiency correctness limitations ease of programming Contrast data structure s strengths and weaknesses understand when to use each one 35

Coming Up Asymptotic Analysis 36