CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

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

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

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

Abstract Data Types Spring 2018 Exam Prep 5: February 12, 2018

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

Queue. COMP 250 Fall queues Sept. 23, 2016

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

COMP 250 Midterm #2 March 11 th 2013

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

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

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

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Stacks and queues (chapters 6.6, 15.1, 15.5)

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

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

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Notes on access restrictions

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

.:: UNIT 4 ::. STACK AND QUEUE

Linked Structures. See Section 3.2 of the text.

CS24 Week 4 Lecture 2

CS/CE 2336 Computer Science II

Abstract Data Types - Lists Arrays implementation Linked-lists implementation

COS 226 Algorithms and Data Structures Fall Midterm

CMPT 125: Practice Midterm Answer Key

CSC212:Data Structure

Lecture 12: Doubly Linked Lists

Lecture 4. The Java Collections Framework

ITI Introduction to Computing II

COMP 250. Lecture 8. stack. Sept. 25, 2017

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

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on

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

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

8. Fundamental Data Structures

Implementing Lists, Stacks, Queues, and Priority Queues

Linked List Implementation of Queues

Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13

An Introduction to Data Structures

Lecture 2: Stacks and Queues


CMSC Introduction to Algorithms Spring 2012 Lecture 7

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

Generics. IRS W-9 Form

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Data Structures Lecture 5

package weiss.util; // Fig , pg // Fig 6.16,6.17, pg package weiss.util;

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

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Doubly LinkedList is Symmetrical! LinkedList Efficiency. Monday, April 8, 13. insert insert remove remove remove walk

182 review 1. Course Goals

Dynamic Data Structures

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

The Java Collections Framework. Chapters 7.5

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

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

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

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

Stacks and Queues. David Greenstein Monta Vista

Computer Science 210: Data Structures. Linked lists

csci 210: Data Structures Linked lists Summary Arrays vs. Lists Arrays vs. Linked Lists a[0] a[1] a[2]... null Today READING: Arrays

Computer Science 62. Midterm Examination

CSC 273 Data Structures

LIFO : Last In First Out

Chapter 18: Stacks And Queues

JAVA.UTIL.LINKEDLIST CLASS

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

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

COMP 103. Data Structures and Algorithms

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1

CE204 Data Structures and Algorithms Part 2

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

Summer Final Exam Review Session August 5, 2009

CSC 321: Data Structures. Fall 2012

Stack and Queue. Stack:

Array Lists Outline and Required Reading: Array Lists ( 7.1) CSE 2011, Winter 2017, Section Z Instructor: N. Vlajic

Solutions to a few of the review problems:

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists

Introduction to Data Structures

ECE 2400 Computer Systems Programming Fall 2017 Topic 9: Abstract Data Types

Lecture Data Structure Stack

C27a: Stack and Queue

ADTs, Arrays, and Linked-Lists

Linked Lists

Introduction to Software Design

Computer Science 136 Exam 2

CSC 321: Data Structures. Fall 2013

Chapter 18: Stacks And Queues

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Queues Fall 2018 Margaret Reid-Miller

Information Science 2

Week 6. Data structures

Data Structures and Algorithms Notes

ITI Introduction to Computing II

ECE 2400 Computer Systems Programming Fall 2018 Topic 9: Abstract Data Types

Transcription:

CSE 11 Computer Science 1 Module 9a: ADT Representations Stack and queue ADTs array implementations Buffer ADT and circular lists

Buffer ADT add and remove from both ends»can grow and shrink from either end»cannot access middle, e.g. find(p)»may wrap in either direction first 4 6 6 count=4 This yields buffer ADT, also sometimes called a doubleended queue (or deque, which is pronounced deck ) Operations include»getfirst(), getlast()»addfirst(x), addlast(x),»removefirst(), removelast() Again, constant time per operation»note, not true if we just used ArrayList in place of Buffer

The Stack Abstract Data Type A stack is a data structure that stores a collection of items and supports the following operations»push(x) add item x onto the top of the stack»pop() remove the item currently on top of the stack»peek() return the item that is currently at the top of the stack peek()= push() push(0) 0 pop»sequence: push(x), then i pushes, then i pops gives peek()=x Special case of the List ADT so why bother?»often easier to understand (and correctly implement) programs that use just the right data structure restrictions reduce bugs»specialized versions can be implemented more efficiently

The Queue Abstract Data Type A queue is a data structure that stores a collection of items and supports the following operations»enqueue(x) add item x to the tail of the queue»peek() return the item that is at the head of the queue»dequeue() remove the item at the head of the queue peek()= enqueue(6) dequeue() 6 6»sequence: enqueue(x), enqueue(y) is eventually followed by peek()=x and next dequeue() yields peek()=y»queues often used to link multiple stages of a computation 4

Implement Buffer, Stack, Queue using List Suppose you have a List that implements»getfirst(), getlast()»addfirst(x), addlast(x),»removefirst(), removelast() We can implement a buffer by simple passing-through addlast(object x) { list.addlast(x); We do not expose other details of the List to the buffer Stack: push addfirst(), peek getfirst(), pop removefirst() Queue: enqueue addlast(), peek getfirst(), dequeue removefirst()

Array-Based Stack Implementation Array implementation of stacks is simple and efficient»instance variable top stores index of top element»peek() returns stackarray[top]»push(x): stackarray[++top]=x»pop(): top»add tests to avoid overflow/underflow»isempty() returns (top == 1) Constant time per operation»that is, pushing an item onto a stack with a million items takes same time as for a stack with two items»true because items only added/removed at top of stack In Java, stacks of objects store objects separately from the array top 1 0 1 0 stackarray abc def xyz 6

Array-Based Queue Implementation Array implementation of queue is simple and efficient»instance variable head points to first item in queue; tail points to item»count is # of elements in the queue»head and tail wrap around at end of array»peek() returns qarray[head]»enqueue(x): tail=(tail+1)%size; qarray[tail]=x»dequeue(): head=(head+1)%size Again, constant time per operation since items are only added/removed from ends Queues of objects store objects separately from the array head 4 6 tail 6 count=4 tail head 0 1 8 1 4 8 count=4

Comparing ADT Implementations Stack Queue Buffer ArrayList LinkedList getfirst X constant constant constant constant getlast constant X constant constant constant addfirst X X constant linear constant addlast constant constant constant constant constant removefirst X constant constant linear constant removelast constant X constant constant constant add anywhere X X X linear constant remove any item X X X linear constant access by position X X X constant linear Constant means time is independent of # of elements Linear means time grows in proportion to # of elements in the worst-case 8

Circular Lists Array-based implementations require allocating space when data structure is constructed»often, don t know how much space is needed in advance»linked lists allow stacks/queues/buffers to grow and shrink as needed Circular lists are a natural choice for stacks/queues»usually implemented with pointer to item»makes addfirst(), addlast(), removefirst() easy»removelast() requires going all the way around (or does it?) 1 1 4 4 4 addlast(8) removefirst() 9

public class CircularList(){ public ListItem ; public void addlast(int x){ if( == null) { = new ListItem(x, null);.next = ; else {.next = new ListItem(x,.next); =.next; 8 addlast(8) 10

public void addfirst(int x){ if( == null) { = new ListItem(x, null);.next = ; else {.next = new ListItem(x,.next); 8 addfirst(8) 11

public int getfirst(){ if( == null) throw new EmptyException(); return.next.value; public int removefirst() { int x = getfirst();.next =.next.next; return x; 8 8 getfirst() is removefirst() 1

Implement Stack using Circular List public class Stack { private CircularList elements = new CircularList(); public void push(x) {elements.addfirst(x); public int pop() {return elements.removefirst(); public int peek() {return elements.getfirst(); 1

Implement Queue using Circular List public class Queue { private CircularList elements = new CircularList(); public void enqueue(x) {elements.addlast(x); public int dequeue) { return elements.removefirst(); public int peek() {return elements.getfirst(); 14