September 29 – Stacks and queues

Slides:



Advertisements
Similar presentations
Data Structure HKOI training /4/2010 So Pak Yeung.
Advertisements

 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
Data Structures & Algorithms
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Stack ADT (Abstract Data Type) N …
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
Elementary data structures
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Understanding Algorithms and Data Structures
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Queue ADT (Abstract Data Type) N …
Chapter 4 The easy stuff.
COSC160: Data Structures: Lists and Queues
March 27 – Course introductions; Adts; Stacks and Queues
Chapter 15 Lists Objectives
CS 1114: Implementing Search
March 29 – Testing and Priority QUeues
Stacks and Queues.
Queues Queues Queues.
Cinda Heeren / Geoffrey Tien
October 30th – Priority QUeues
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Introduction to Data Structure
CST230: Applied Data Structures
Data Structures and Database Applications Queues in C#
i206: Lecture 11: Stacks, Queues
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Principles of Computing – UFCFA3-30-1
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Lecture 21 Stacks and Queues Richard Gesick.
Data Structures and Algorithms
Lecture 3: Queues, Testing, and Working with Code
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks, Queues, and Deques
Further Data Structures
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues CLRS, Section 10.1.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks and Queues 1.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Stacks and Queues.
Introduction to Data Structure
Lecture 16 Stacks and Queues CSE /26/2018.
Abstract Data Type (ADT)
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Lecture 16 Stacks and Queues CSE /26/2018.
structures and their relationships." - Linus Torvalds
Presentation transcript:

September 29 – Stacks and queues Cse 373 September 29 – Stacks and queues

Design Decisions Shopping list?

Design Decisions Shopping list? What sorts of behavior do shoppers exhibit? What constraints are there on a shopper? What improvements would make a better shopping list?

Design Decisions Shopping list? Stack?

Design Decisions Shopping list? Stack? What sorts of behavior does the ‘stack’ support? What constraints are there on a stack user? (Is there a change in certainty?) What improvements would make a better stack? (What problems might arise in a stack?)

Stack ADT Important to know exactly what we expect from a stack.

Stack ADT Important to know exactly what we expect from a stack. Push(Object a) returns null; (other options?) Pop() returns Object a: where a is the element on ‘top’ of the stack; also removes a from the stack Top() returns Object a: where a is the element on ‘top’ of the stack without removing that element from the stack

Stack ADT Important to know exactly what we expect from a stack. Push(Object a) returns null; (other options?) Pop() returns Object a: where a is the element on ‘top’ of the stack; also removes a from the stack Top() returns Object a: where a is the element on ‘top’ of the stack without removing that element from the stack How long will these operations take?

Stack ADT Important to know exactly what we expect from a stack. Push(Object a) returns null; (other options?) Pop() returns Object a: where a is the element on ‘top’ of the stack; also removes a from the stack Top() returns Object a: where a is the element on ‘top’ of the stack without removing that element from the stack How long will these operations take? That depends on the Data Structure and Implementation

queue ADT What behavior do we expect from the queue?

queue ADT What behavior do we expect from the queue? enqueue(Object toInsert): dequeue(): front():

queue ADT What behavior do we expect from the queue? enqueue(Object toInsert): adds to the queue dequeue(): removes the ‘next’ element from the queue front(): peeks at the ‘next’ element

queue ADT What behavior do we expect from the queue? enqueue(Object toInsert): adds to the queue dequeue(): removes the ‘next’ element from the queue front(): peeks at the ‘next’ element Which element is ‘next’?

queue ADT What behavior do we expect from the queue? enqueue(Object toInsert): adds to the queue dequeue(): removes the ‘next’ element from the queue front(): peeks at the ‘next’ element Which element is ‘next’? FIFO – ‘first in, first out’ ordering

Stack and Queue ADT Stacks and Queues both support the same functions insert: push() and enqueue() remove: pop() and dequeue() peek: top() and front()

Stack and Queue ADT Stacks and Queues both support the same functions insert: push() and enqueue() remove: pop() and dequeue() peek: top() and front() This isn’t sufficient to distinguish them, their behavior is also a critical part of their ADT. Which element do we expect to be ‘removed’? FIFO v LIFO

Stack and Queue ADT The ADT describes the methods provided and the behavior we expect from them The Data Structure is a theoretical arrangement of the data that supports the functionality of the ADT

Stack and Queue ADT What Data Structures might we use for Stacks and Queues?

Stack and Queue ADT What Data Structures might we use for Stacks and Queues? Arrays

Stack and Queue ADT What Data Structures might we use for Stacks and Queues? Arrays How many ways can we use arrays?

Stack and Queue ADT What Data Structures might we use for Stacks and Queues? Arrays How many ways can we use arrays? Which ways are efficient?

queue ADT Array implementation Unique problems?

queue ADT Array implementation Unique problems? What if the array is full?

queue ADT Array implementation Unique problems? What if the array is full? What if we alternate enqueue() and dequeue()?

queue ADT Array implementation Unique problems? End of Array Unique solutions?

queue ADT Array implementation Unique problems? End of Array Unique solutions? Resizing (costly!) Circular Array (?)

Circular Queues

Circular Queues Front Back

Circular Queues Front Back

Circular Queues Why this way? What function to front and back serve?

Circular Queues Front Back enqueue(4)

Circular Queues Front Back 4 Which operations will move what pointers?

Circular Queues Front Back 4 Let’s do several enqueues

Circular Queues What happens now, on enqueue(7)? Front Back 4 5 9 2 3 1 6 What happens now, on enqueue(7)?

Circular Queues Problems here? How to implement? Front Back 4 5 9 2 3 1 6 7 Problems here? How to implement?

Circular Queues Front Back 4 5 9 2 3 1 6 7 The queue is full, but it is the same situation (front == back) as when the queue is empty. This is a boundary condition.

Circular Queues Front Back 4 5 9 2 3 1 6 7 We have to resize the list (or deny the add) if we get another enqueue.

Circular Queues What if we dequeue some items? Front Back 4 5 9 2 3 1 6 7 What if we dequeue some items?

Circular Queues Front Back 5 9 2 3 1 6 7 Dequeue() outputs 4

Circular Queues Dequeue() outputs 4 Is the 4 really “deleted”? Front Back 5 9 2 3 1 6 7 Dequeue() outputs 4 Is the 4 really “deleted”?

Circular Queues Front Back 9 2 3 1 6 7 Output 5

Circular Queues Now we’ve freed up some space and can enqueue more Front Back 9 2 3 1 6 7 Now we’ve freed up some space and can enqueue more

Circular Queues Front Back 5 9 2 3 1 6 7 enqueue(5)

Circular Queues By moving the front and back pointers, we can utilize all of the space in the array Advantages over a linked list?

Circular Queues By moving the front and back pointers, we can utilize all of the space in the array Advantages over a linked list? Fixed number of items Small data (Memory efficiency) From Wednesday: What is the memory overhead of the linked list?

Testing Implementation is great if it works on the first try

Testing Implementation is great if it works on the first try In a large implementation, what is causing the problem? Data structure? Client? Wrapper?

Testing Implementation is great if it works on the first try In a large implementation, what is causing the problem? Object oriented programming allows modularity – good testing can pinpoint bugs to particular modules

Testing Two primary types of testing

Testing Two primary types of testing Black box Behavior only, no peeking into the code This usually tests ADT behavior Can test performance/efficiency by using a timer

Testing Two primary types of testing White box (or clear box) Where there is an understanding of the implementation that can be leveraged for testing If you’re writing your own DS, you can peek into attributes that you would normally refuse access to the client

Testing Isolate the problem

Testing Isolate the problem Write specific tests Running the whole program doesn’t help narrow down problems

Testing Isolate the problem Write specific tests Running the whole program doesn’t help narrow down problems What are expected test cases?

Testing Isolate the problem Write specific tests Running the whole program doesn’t help narrow down problems

Testing Many test cases (and large ones) You can prove that an algorithm is correct, but you cannot necessarily prove an arbitrary implementation is correct

Testing Many test cases (and large ones) You can prove that an algorithm is correct, but you cannot necessarily prove an arbitrary implementation is correct More inputs can increase certainty Adversarial testing The client is not your friend

Testing Good things to test Expected behavior (at multiple sizes) Forbidden input Empty/Null Side effects Boundary/Edge Cases

New ADT Stacks and Queues are great, but they’re very simple. Data structures is about storing and managing data, but S/Q restrict access to that data What sort of behavior would be more general?

Dictionary ADT Operates on two data types a key, our lookup data type a value, the related data stored in the structure Supports three main functions insert(K key, V value) delete(K key) find(K key)

Dictionary ADT Example English Language Dictionary

Dictionary ADT Example English Language Dictionary What are keys and values?

Dictionary ADT Example English Language Dictionary Keys here are words (Strings) Values are definitions (Strings)

Dictionary ADT Example English Language Dictionary Keys here are words (Strings) Values are definitions (Strings) Keys and Values can be the same data type

Dictionary ADT Example English Language Dictionary Keys here are words (Strings) Values are definitions (Strings) Keys and Values can be the same data type find(String word) will return the definition of the word – provided that the <word,definition> pair was added to the dictionary

Next week Dictionary/Map behavior and ADT Simple Implementations Analyzing behavior, what do we mean when we say an algorithm is efficient?