CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

Similar documents
Data Structures and Algorithms

8. Fundamental Data Structures

Apply to be a Meiklejohn! tinyurl.com/meikapply

1 P age DS & OOPS / UNIT II

Linked Structures. See Section 3.2 of the text.

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

CS24 Week 4 Lecture 2

.:: UNIT 4 ::. STACK AND QUEUE

Chapter 9 STACK, QUEUE

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Stack and Queue. Stack:

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

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

Data Structures and Algorithms

CS171 Midterm Exam. October 29, Name:

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

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

Computer Science 501 Data Structures & Algorithms The College of Saint Rose Fall Topic Notes: Linear Structures

Motivation for Queues

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

LIFO : Last In First Out

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

CMPT 125: Practice Midterm Answer Key

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Linked List. April 2, 2007 Programming and Data Structure 1


CS350 - Exam 1 (100 Points)

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

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

3. Fundamental Data Structures

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

csci 210: Data Structures Stacks and Queues

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

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

Stacks and queues (chapters 6.6, 15.1, 15.5)

Data Structure. Recitation VII

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

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

Data structure and algorithm in Python

Data Structures and Algorithms

CS2012 Programming Techniques II

Introduction to Data Structures

Stacks and Queues

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Sequence Abstract Data Type

CS 206 Introduction to Computer Science II

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

Data Structures -- Introduction

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

Computer Science 1 Study Union Practice Problems. What are the arguments to malloc? calloc? realloc? What do they do?

DATA STRUCTURE UNIT I

Queue. COMP 250 Fall queues Sept. 23, 2016

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1)

== isn t always equal?

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Week 4 Stacks & Queues

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

Data Structure Advanced

CSE 100: GRAPH ALGORITHMS

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

Chapter 18: Stacks And Queues

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

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Standard ADTs. Lecture 19 CS2110 Summer 2009

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CSE 373: Data Structures and Algorithms

table1 = [ [1, 2, 3, 4], [4, 5, 6, 7], [8, 9,10,11] ] we get: >>> sumcol(table1,0) 13 >>> sumcol(table1,1) 16

Abstract Data Types. Abstract Data Types

CSC148 Week 2. Larry Zhang

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

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

Basic Data Structures (Version 7) Name:

Information Science 2

PA3 Design Specification

STACKS AND QUEUES. Problem Solving with Computers-II

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

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

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Stacks and Queues. CSE 373 Data Structures Lecture 6

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

Chapter 18: Stacks And Queues

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

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

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Round 1: Basics of algorithm analysis and data structures

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Lecture 2: Stacks and Queues

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Linked Lists

Non-blocking Array-based Algorithms for Stacks and Queues!

DS L9: Queues

Transcription:

CS245-2017S-04 Stacks and Queues 1 04-0: Abstract Data Types An Abstract Data Type is a definition of a type based on the operations that can be performed on it. An ADT is an interface Data in an ADT cannot be manipulated directly only through operations defined in the interface 04-1: Abstract Data Types To define an ADT, give the operations that can be performed on it The ADT says nothing about how the operations are performed Could have different implementations of the same ADT First ADT for thie class: Stack 04-2: Stack A Stack is a Last-In, First-Out (LIFO) data structure. Stack Operations: Add an element to the top of the stack Remove the top element Check if the stack is empty 04-3: Stack Implementation 04-4: Stack Implementation Stack elements are stored in an array Top of the stack is the end of the array If the top of the stack was the beginning of the array, a push or pop would require moving all elements in the array Push: data[top++] = elem Pop: elem = data[--top] 04-5: Stack Implementation See code & Visualizaion 04-6: Θ() For Stack Operations Array Implementation: push pop empty() 04-7: Θ() For Stack Operations Array Implementation:

CS245-2017S-04 Stacks and Queues 2 push Θ(1) pop Θ(1) empty() Θ(1) 04-8: Stack Implementation 04-9: Stack Implementation Stack elements are stored in a linked list Top of the stack is the front of the linked list push: top = new Link(elem, top) pop: elem = top.element(); top = top.next() 04-10: Stack Implementation See code & Visualization 04-11: Θ() For Stack Operations Linked List Implementation: push pop empty() 04-12: Θ() For Stack Operations Linked List Implementation: push Θ(1) pop Θ(1) empty() Θ(1) 04-13: Queue A Queue is a First-In, First-Out (FIFO) data structure. Queue Operations: Add an element to the end (tail) of the Queue Remove an element from the front (head) of the Queue Check if the Queue is empty 04-14: Queue Implementation 04-15: Queue Implementation Maintain a pointer to the first and last element in the Linked List Add elements to the back of the Linked List Remove elements from the front of the linked list Enqueue: tail.setnext(new link(elem,null)); tail = tail.next() Dequeue: elem = head.element(); head = head.next();

CS245-2017S-04 Stacks and Queues 3 04-16: Queue Implementation See code & visualization 04-17: Queue Implementation 04-18: Queue Implementation Store queue elements in a circular array Maintain the index of the first element (head) and the next location to be inserted (tail) Enqueue: Dequeue: data[tail] = elem; tail = (tail + 1) % size elem = data[head]; head = (head + 1) % size 04-19: Queue Implementation Se code & visualization 04-20: Modifying Stacks Minimum Stacks have one additional operation: minimum: return the minimum value stored in the stack Can you implement a O(n) minimum? 04-21: Modifying Stacks Minimum Stacks have one additional operation: minimum: return the minimum value stored in the stack Can you implement a O(n) minimum? Can you implement a Θ(1) minimum? push, pop must remain Θ(1) as well! 04-22: Modifying Queues We d like our array-based queues and our linked list-based queues to behave in the same way How do they behave differently, given the implementation we ve seen so far? 04-23: Modifying Queues We d like our array-based queues and our linked list-based queues to behave in the same way How do they behave differently, given the implementation we ve seen so far? Array-based queues can get full How can we fix this? 04-24: Modifying Queues Growing queues

CS245-2017S-04 Stacks and Queues 4 If we do a Enqueue on a full queue, we can: Create a new array, that is twice as big as the old array Copy all of the data across to the new array Replace the old array with a new array Why is this a little tricky? 04-25: Modifying Queues Growing queues If we do a Enqueue on a full queue, we can: Create a new array, that is twice as big as the old array Copy all of the data across to the new array Replace the old array with a new array Why is this a little tricky? 04-26: Modifying Queues Queue could wrap around the end of the array (examples!) Why do we double the size of the queue when it gets full, instead of just increasing the size by a constant amount Hint think about running times 04-27: Modifying Queues What is the running time for a single enqueue/push, if we allow the stack to grow? (by doubling the size of the stack/queue when it is full) What is the running time fornenqueues/pushes? 04-28: Modifying Queues What is the running time for a single enqueue/push, if we allow the stack to grow? (by doubling the size of the stack/queue when it is full) O(n), for a stack size ofn What is the running time fornenqueues/pushes? 04-29: Modifying Queues O(n) so each push/enqueue takeso(1) on average What is the running time for a single enqueue/push, if we allow the stack to grow? (by addingk elements when the stack/queue is full) What is the running time fornenqueues/pushes?

CS245-2017S-04 Stacks and Queues 5 04-30: Modifying Queues What is the running time for a single enqueue/push, if we allow the stack to grow? (by addingk elements when the stack/queue is full) O(n), for a sack size ofn What is the running time fornenqueues/pushes? O(n n/k) = O(n 2 ), ifk is a constant Each enqueue/dequeue takes timeo(n) on average! 04-31: Amortized Analysis Figuring out how long an algorithm takes to run on average, by adding up how long a sequence of operations takes, is called Amortized Analysis Washing Machine example Cost of a washing machine Amortized cost per wash We ll take quite a bit about amortized analysis, complete with some more formal mathematics, later in the semester.