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

Similar documents
Stack and Queue. Stack:

CS24 Week 4 Lecture 2

csci 210: Data Structures Stacks and Queues


1 P age DS & OOPS / UNIT II

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

Information Science 2

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

Chapter 9 STACK, QUEUE

.:: UNIT 4 ::. STACK AND QUEUE

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

PA3 Design Specification

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Foundations of Data Structures

COSC160: Data Structures Heaps. Jeremy Bolton, PhD Assistant Teaching Professor

Lecture Data Structure Stack

Stacks and Queues. David Greenstein Monta Vista

Basic Data Structures 1 / 24

CMSC Introduction to Algorithms Spring 2012 Lecture 7

15. Stacks and Queues

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

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

CSC148 Week 2. Larry Zhang

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

COMP 182 Algorithmic Thinking. Breadth-first Search. Luay Nakhleh Computer Science Rice University

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

Basic Data Structures

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

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

3. Fundamental Data Structures

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

Abstract Data Types. Abstract Data Types

Data Structure. Recitation VII

What is an algorithm?

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

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

CS 206 Introduction to Computer Science II

Data Abstraction and Specification of ADTs

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

Stacks and Queues. CSE Data Structures April 12, 2002

Ashish Gupta, Data JUET, Guna

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

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

CSE 100: GRAPH ALGORITHMS

Stacks, Queues and Hierarchical Collections

12 Abstract Data Types

Stacks and queues (chapters 6.6, 15.1, 15.5)

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

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

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

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

Some major graph problems

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

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

DATA STRUCTURE UNIT I

Stacks and Queues. Stack - Abstract Data Type. Stack Applications. Stack - Abstract Data Type - C Interface

Lecture 3: Stacks & Queues

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

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stacks and Queues. CSE 373 Data Structures Lecture 6

CP2 Revision. theme: dynamic datatypes & data structures

Data Structures and Algorithms (DSA) Course 6 Lists (Recapitulation) Iulian Năstac

Lecture 4 Stack and Queue

Chapter 18: Stacks And Queues

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Summer Final Exam Review Session August 5, 2009

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

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Rob Legrand, and Ron K Cytron Washington University in St Louis 1 Brookings Dr Saint Louis, MO

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.

Data Structure Advanced

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

Data Structures and Algorithms

Announcements Queues. Queues

infix expressions (review)

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

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

Motivation for Queues

Chapter 18: Stacks And Queues

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

Postfix (and prefix) notation

08 STACKS DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

LIFO : Last In First Out

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

Unit 4: Stacks and Queues

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

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

Arrays, Strings and Collections

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

1 Short Answer (7 Points Each)

8. Fundamental Data Structures

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

BBM 201 DATA STRUCTURES

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Transcription:

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

Outline I. Queues I. FIFO Queues I. Usage II. Implementations II. LIFO Queues (Stacks) I. Usage II. Implementations III. Applications / Examples

Queues Queue is a linear list where data is added and removed at the ends Refers to conceptual behavior (not implementation) Specifically when (where) data enters the queue, and when (where) data exits the queue Access is generally limited to the top or the bottom of the queue. Types of queues are differentiated by operations performed (behaviors) Common Queues First In First Out (FIFO) Applications: traversals, memory buffers, resource sharing, simulations, Last In First Out (LIFO) Stack Applications: parsers, Runtime environments, traversals,

FIFO Queue First In First Out Describes how data is added and removed First come first serve Examples where used: Check-out line at store When entities use a shared resource Memory Buffer Operations Insert (item) // inserts item to back of queue Remove () // removes item at front of queue Implementation Details Array or chain structure Time Complexity implications

FIFO Queue: Add and Remove Enqueue 4 (add to back) Enqueue 5 (add to back) Dequeue (remove from front) Enqueue 2 (add to back) Dequeue (remove from front) Dequeue (remove from front)

FIFO Queue: Array Implementation Considerations Front of Queue Should front of list always be the front of the array Requires shifting after every dequeue Circular, dynamic array is efficient Keep index to front (and back) Observations: Since no internal elements in the queue need to be accessed. Both insert (enqueue) and remove (dequeue) operations are efficient. Time Complexity?

FIFO Queue: Linked List Implementation Memory Requirements? Time? Enqueue Dequeue

LIFO Queue: aka stack Last In First Out Describes how data is added and removed Last to be added is first to be removed Examples where used: Push down plates at cafeteria Parsing / grammar analysis Tree or maze traversals Operations Insert (to top) PUSH( item ) Remove (from top) POP () View top PEEK() Implementation Details Array or chain structure Time Complexity implications

LIFO Queue (Stack): Push and Pop Push(5) Push(10) Push(2) Pop() Push(10) Pop()

Stack: Array Implementations Observations Must allocate size initially No need to keep a base index Maintain a top index

Stack: Linked List Implementations Observations Must maintain pointer to top Size changes dynamically

Queue Implementation Summary Both Insert and Remove operations for both Queue types are efficient Chaining Implementation permits size to change so that extra space is not necessarily allocated. This comes at the cost of one extra pointer per item stored. Array Implementation does not require an extra pointer per item stored, but may use space unnecessarily and may infrequently, require and O(n) reallocation if the Queue is full and an insert is performed.

Shared Resource Queue Application of FIFO Queue

Applications of FIFO queues Shortest Distance (from a to b) on a grid example FIFO := initialize empty fifo queue //Square a is at grid location (0,0) a.distancethusfar := 0 FIFO.enqueue(a) while (! FIFO.isEmpty() ) x := FIFO.dequeue() for each neighboring square n of square x if square was not previously encountered OR if n.distancethusfar > x.distancethusfar + 1 n.distancethusfar := x.distancethusfar + 1 if n == b // done FIFO.clear() return n.distancethusfar FIFO.enqueue(n); and mark square n as encountered a 1 2 3 4 5 1 1 2 3 4 5 2 2 2 * 4 5 3 3 * 3 * 5 5 4 4 * * 6 6 5 5 5 6 7 b

Application of Stacks Symbol matching (for syntax) EG: Are all the ( and { properly matched with ) and } function f ( arg ) { while (arg > 0) {x = arg --;} } Symbol matching algorithm Scan input code (character array) from left to right for i from 0 to inputlength - 1 if input[ i ] == ( OR input[ i ] == {, then stack.push(input[ i ]) else if input[ i ] == ) OR input[ i ] == } then c : = stack.pop() if c is ( and input[ i ] is ) OR if c is { and input[ i ] is } //The symbols are matched!, do nothing else // the symbols are unmatched throw a syntax error if traversal complete and stack.isempty() == false, then Throw error, no matching close symbol found