CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Lecture 5 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Implementing and Using Stacks
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks.
Queues Queues Queues.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Copyright © Aiman Hanna All rights reserved
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO AND JORY DENNY 1

STACK - OUTLINE  The Stack ADT (Ch )  Array-based implementation (Ch )  Growable array-based stack  Singly list implementation (Ch 5.1.5) 2

STACKS  A stack is a container with visibility and access through one end known as top element.  LIFO – Last In First Out Principle  Practical Applications:  Web browser history of pages : Back button  Editors: Undo button  Runtime Stack: Nested Function calls and storage of parameters, variables, return address, etc.  Indirect applications  Auxiliary data structure for algorithms  Component of other data structures 3

STACK ADT  Stores arbitrary objects  Insertions, deletion, modification and access is allowed through the top element.  Main Operations:  push(e) – inserts element e at the top of the stack  pop() – removes the top element from the stack  top() – returns the top element without removing it  Auxiliary operations:  size() – Number of elements in the stack  empty() – True if the stack contains no elements  Removing or accessing an element in an empty stack returns an exception called EmptyStackException. 4

EXERCISE  Show the stack after each of these operations:  Push(5)  Push(3)  Pop()  Push(2)  Push(8)  Pop()  Push(1) 5

C++ RUNTIME STACK  The C++ run-time system keeps track of the chain of active functions with a stack  When a function is called, the system pushes on the stack a frame containing  Local variables and return value  Program counter, keeping track of the statement being executed  When the function ends, its frame is popped from the stack and control is passed to the function on top of the stack main() { int i; i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 6

ARRAY BASED STACK IMPLEMENTATION  Array based implementation is the simplest  Add elements from left to right of the array  Keep track of the index of the top element in a variable, t  Do you observe any problems? S 012 t … Algorithm pop() if empty() then throw StackEmptyException else t  t  1 return S[t + 1] Algorithm push(e) t  t + 1 S[t]  e 7

ARRAY BASED STACK IMPLEMENTATION  The array storing the stack elements may become full  A push operation will then throw a StackFullException  Limitation of the array-based implementation  Not intrinsic to the Stack ADT S 012 t … Algorithm push(e) if size() = S.length then throw StackFullException else t  t + 1 S[t]  e 8 Algorithm size() return t + 1

ALGORITHMIC COMPLEXITY ANALYSIS 9

STACK PERFORMANCE 10 Array Fixed Capacity pop() push(e) top() size(), empty() Array Fixed Capacity pop() push(e) top() size(), empty()

PERFORMANCE AND LIMITATIONS  Performance  Let n be the number of elements in the stack  The space used is O(n)  Each operation runs in time O(1)  Limitations  The maximum size of the stack must be defined a priori and cannot be changed  Trying to push a new element into a full stack causes an implementation-specific exception 11

GROWABLE ARRAY STACK 12

COMPARISON OF THE STRATEGIES 13

INCREMENTAL STRATEGY ANALYSIS 14

DOUBLING STRATEGY ANALYSIS

STACK PERFORMANCE Array Fixed CapacityGrowable Array (Doubling) pop() push(e) Worst Case Best Case Average Case top() size(), empty() 16 Array Fixed CapacityGrowable Array (Doubling) pop() push(e) top() size(), empty()

SINGLY LINKED LIST STACK IMPLEMENTATION  nodes elements top 17

EXERCISE 18

STACK SUMMARY Array Fixed CapacityGrowable Array (Doubling) Singly Linked List pop() push(e) top() size(), empty() 19

QUEUES - OUTLINE  The Queue ADT (Ch )  Implementation with a circular array (Ch )  Growable array-based queue  List-based queue 20

QUEUES  Container storing arbitrary objects such that insertions allowed at one end called back and removal from other end called front.  FIFO – First In First Out scheme  Practical applications:  Waiting lines  Scheduling – Task, shared resources (printer) access  Multiprogramming  Indirect applications  Auxiliary data structure for algorithms  Component of other data structures 21

QUEUE ADT  Insertions and deletions follow the first-in first-out scheme  Insertions are at the rear of the queue and removals are at the front of the queue  Main operations:  enqueue(e): inserts an element e at the end of the queue  dequeue(): removes the element at the front of the queue  Auxiliary queue operations:  front(): returns the element at the front without removing it  size(): returns the number of elements stored  empty(): returns a Boolean value indicating whether no elements are stored  Application of dequeue() on an empty queue throws EmptyQueueException 22

EXERCISE  Show the queue after each of the following operations:  enqueue(5)  enqueue(3)  dequeue()  enqueue(2)  enqueue(8)  dequeue()  enqueue(9) 23

ARRAY BASED QUEUE IMPLEMENTATION Q 012rf normal configuration Q 012fr wrapped-around configuration 24

QUEUE OPERATIONS  Use modulo operator (finds the remainder of a division) Q 012rf Q 012fr 25

QUEUE OPERATIONS  Operation enqueue throws an exception if the array is full  This exception is implementation-dependent Q 012rf Q 012fr 26

QUEUE OPERATIONS  Operation dequeue throws an exception if the queue is empty  This exception is specified in the queue ADT Q 012rf Q 012fr 27

PERFORMANCE AND LIMITATIONS – ARRAY BASED QUEUE 28

GROWABLE ARRAY BASED QUEUE 29

EXERCISE 30

QUEUE SUMMARY Array with fixed capacity Growable array (doubling) Singly linked list dequeue() enqueue(e) front() size(), empty() 31

DEQUE ADT (CH 5.3) 32

DOUBLY LINKED LIST BASED DEQUE back front elements 33

PERFORMANCE AND LIMITATIONS – DOUBLY LINKED LIST BASED DEQUE 34

DEQUE SUMMARY Array Fixed capacity Growable array (doubling) Singly linked listDoubly linked list 35