Data Structures and Algorithms Stacks and Queues



Similar documents
Linked Lists Linked Lists, Queues, and Stacks

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Chapter 3: Restricted Structures Page 1

St S a t ck a ck nd Qu Q eue 1

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

DATA STRUCTURE - QUEUE

DATA STRUCTURES USING C

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Common Data Structures

Introduction to Data Structures and Algorithms

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Application of Stacks: Postfix Expressions Calculator (cont d.)

Data Structures and Algorithms V Otávio Braga

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Module 2 Stacks and Queues: Abstract Data Types

EECE 276 Embedded Systems

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Data Structures and Algorithms Lists

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Introduction to Data Structures

Data Structures and Algorithms

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

DATA STRUCTURE - STACK

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Algorithms and Data Structures

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

I/O Management. General Computer Architecture. Goals for I/O. Levels of I/O. Naming. I/O Management. COMP755 Advanced Operating Systems 1

7.1 Our Current Model

Data Structures UNIT III. Model Question Answer

Data Structure [Question Bank]

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Data Structures Using C++ 2E. Chapter 5 Linked Lists

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

STACKS,QUEUES, AND LINKED LISTS

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Cpt S 223. School of EECS, WSU

Universidad Carlos III de Madrid

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Sequential Data Structures

Analysis of a Search Algorithm

Data Structures and Algorithms

CmpSci 187: Programming with Data Structures Spring 2015

Timing of a Disk I/O Transfer

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Data Structures and Algorithms

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Evolving Data Structures with Genetic Programming

Ordered Lists and Binary Trees

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

10CS35: Data Structures Using C

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

3 Abstract Data Types and Search

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Stacks. Data Structures and Data Types. Collections

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Basic Data Structures and Algorithms

Lecture Notes on Stacks & Queues

Lecture 2: Data Structures Steven Skiena. skiena

Abstract Data Types. Chapter 2

Data Structures and Data Manipulation

PES Institute of Technology-BSC QUESTION BANK

Introduction to Stacks

Chapter 11 I/O Management and Disk Scheduling

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Data Structures. Level 6 C Module Descriptor

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

recursion, O(n), linked lists 6/14

Java Software Structures

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

ADTs,, Arrays, Linked Lists

Conditions for Strong Synchronization In Concurrent Data Types

Data Structures/Stacks and Queues

Data Structures and Algorithms C++ Implementation

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Algorithms and Data Structures

14 Stacks, Queues, And Linked Lists

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Fast nondeterministic recognition of context-free languages using two queues

COMPUTER SCIENCE. Paper 1 (THEORY)

Transcription:

Data Structures and Algorithms Stacks and Queues Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/23

6-0: Stacks and Queues Stacks and queues are two of the most common data structures Based on the List, but with more limited access to elements. Simplicity allows for efficiency, easy implementation. Department of Computer Science University of San Francisco p.2/23

6-1: Stack A Stack is a Last-In, First-Out (LIFO) data structure. Like a stack of trays in a cafeteria. Stack Operations: Add an element to the top of the stack (push) Remove the top element (pop) Look at the top element (peek) Check if the stack is empty Department of Computer Science University of San Francisco p.3/23

6-2: Uses of a stack Postfix notation calculator 3 4 + -> 7 3 4 + 5 - -> 2 Parenthesis checker Recursion Interrupt handling Department of Computer Science University of San Francisco p.4/23

6-3: Stack Implementation Array: Like the list, we allocate memory in advance. Keep a pointer for the top of the stack. Department of Computer Science University of San Francisco p.5/23

6-4: Stack Implementation Array: 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] Department of Computer Science University of San Francisco p.6/23

6-5: Θ() For Stack Operations Array Implementation: push pop empty() Department of Computer Science University of San Francisco p.7/23

6-6: Θ() For Stack Operations Array Implementation: push Θ(1) pop Θ(1) empty() Θ(1) Pros: fast, easy to implement Cons: Resizing the stack is Θ(n) Department of Computer Science University of San Francisco p.8/23

6-7: Stack Implementation Linked List: Department of Computer Science University of San Francisco p.9/23

6-8: Stack Implementation Linked List: 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() Department of Computer Science University of San Francisco p.10/23

6-9: Θ() For Stack Operations Linked List Implementation: push pop empty() Department of Computer Science University of San Francisco p.11/23

6-10: Θ() For Stack Operations Linked List Implementation: push Θ(1) pop Θ(1) empty() Θ(1) Pros: Fast, can grow/shrink dynamically, Cons: Allocating/freeing memory may be costly. Department of Computer Science University of San Francisco p.12/23

A Queue is a Last-In, First-Out (FIFO) data structure. Like a line at the post office or grocery store. Queue Operations: 6-11: Queue Add an element to the end (tail) of the Queue (enqueue) Remove an element from the front (head) of the Queue (dequeue) Check the item at the front of the queue (peek) Check if the Queue is empty Department of Computer Science University of San Francisco p.13/23

6-12: Uses of a queue Scheduling You ll get lots of experience with queues in OS Searching (web crawling) Handling requests Print queues Department of Computer Science University of San Francisco p.14/23

6-13: Queue Implementation Linked List: Department of Computer Science University of San Francisco p.15/23

6-14: Queue Implementation Linked List: 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(); Department of Computer Science University of San Francisco p.16/23

6-15: Θ() For Queue Operations Linked List Implementation: enqueue Θ(1) dequeue Θ(1) empty() Θ(1) Pros: Fast, can grow/shrink dynamically, Cons: Allocating/freeing memory may be costly. Department of Computer Science University of San Francisco p.17/23

6-16: Queue Implementation Array: Can t just use a flat array, as with a stack. Queue will wind up sliding up the array. Department of Computer Science University of San Francisco p.18/23

6-17: Queue Implementation Array: Store queue elements in a circular array Maintain the index of the first element (head) and the next location to be inserted (tail) Enqueue: data[tail] = elem; tail = (tail + 1) % size Dequeue: elem = data[head]; head = (head + 1) % size Department of Computer Science University of San Francisco p.19/23

6-18: Queue Implementation Array Issues: What do head and tail look like when the queue is empty? What about when there is one element in the queue? What about when the queue is full? Department of Computer Science University of San Francisco p.20/23

6-19: Θ() For Queue Operations Array Implementation: enqueue Θ(1) dequeue Θ(1) empty() Θ(1) Pros: Fast Cons: Issues with full vs. empty, fixed size Department of Computer Science University of San Francisco p.21/23

6-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? Department of Computer Science University of San Francisco p.22/23

6-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! Department of Computer Science University of San Francisco p.23/23