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.”

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Stack & Queues COP 3502.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Stacks, Queues, and Deques. 2 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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
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.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
 Balancing Symbols 3. Applications
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Stacks, Queues & Deques CSC212.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
TCSS 342, Winter 2005 Lecture Notes
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
Chapter 19: Stacks and Queues.
Queues.
Stacks and Queues.
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Stacks and Queues CSE 373 Data Structures.
CSCS-200 Data Structure and Algorithms
Stacks and Queues.
Lecture 9: Stack and Queue
Data Structures & Programming
Presentation transcript:

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.” A stack is also called a Last In First Out (LIFO) data structure.

Stack: Logical Level Stack Operations: void clear() void push (E it) E pop () E topValue () int length();

Stack: Application Level A runtime stack of activation records (ar) is maintained as a program executes to track function calls and scopes. Each activation record contains – space for local variables and parameters – ptr to dynamic parent – ptr to static parent – return address

Consider this code outline: Public class SomeMethods () { // public static void B ( ) { } // public static void A ( ) { B (); } // public static void main (String[] args ) { A (); }

Consider the following: main begins executing main calls method A method A calls method B method B returns method A returns main returns Push (main’s ar) Push (A’s ar) Push (B’s ar) Use info in Top ar to return control to A Pop Use info in Top ar to return control to main Pop Use info in Top ar to return control to OS Pop main A B Runtime Stack

Stack: Application Level Stacks can be used to analyze nested expressions with grouping symbols to determine if they are well-formed (all grouping symbols occur in matching pairs and are nested properly.) ( ( {xxx} ) x [ ] xx) is well-formed ( ( {xxx} x [ ] ) is ill-formed

General Algorithm get next symbol set balanced flag to true while (there are more input symbols and expression still balanced) if (next symbol is opening symbol) Push symbol onto stack else if (next symbol is closing symbol) if (stack is empty) // check that length is 0 set balanced to false else pop the stack to get top opening symbol if (opening symbol does not match closing symbol) set balanced to false else ignore symbol get next symbol if (balanced and stack is empty) well-formed else ill-formed ( ( { x x x } ) x [ ] x x ) ( ( { [ Stack { ( [( Popped

Stack: Implementation Level Using an array: listArray [0] [maxSize - 1] 0 top * 10 maxSize * Note that top indicates position where next pushed item will go

Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 1 top push ( 70 ) 10 maxSize

Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 2 top push ( 70 ) push ( 28) 10 maxSize

Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 3 top push ( 70 ) push ( 28) push ( 88) 10 maxSize

Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 2 top push ( 70 ) push ( 28) push ( 88) pop () 10 maxSize

Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 3 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) 10 maxSize

Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 2 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () 10 maxSize

Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 1 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () 10 maxSize

Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 0 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () 10 maxSize

Stack: Implementation Level Using a linked list: top NULL 0 size

Stack: Implementation Level Using a linked list: top NULL push ( 70 ) 70 1 size

Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) size

Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) push ( 88 ) size

Stack: Implementation Level Using a linked list: top NULL 2870 push ( 70 ) push ( 28 ) push ( 88 ) pop () 2 size

Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 )

Stack: Implementation Level Using a linked list: top NULL 2870 push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 2 size

Stack: Implementation Level Using a linked list: top NULL 70 push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 1 size

Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 0 size

Queue: Logical Level “An ordered group of homogeneous items or elements in which items are added at one end (the rear) and removed from the other end (the front.)” A queue is also called a First In First Out (FIFO) data structure.

Queue: Logical Level Queue Operations: void clear () void enqueue (E it) E dequeue () E frontValue () int length()

Queue: Application Level Perfect for modeling a waiting line in a simulation program Key simulation parameters – # of servers – # of queues (waiting lines) – statistics for customer arrival patterns Want to minimize customer waiting time Want to minimize server idle time

Queue: Application Level Queues found all over operating system! – I/O buffers – Job queues waiting for various resources – Spool (print) queue

Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 0... rear front - fixed at [0] (similar to bottom of stack) * Note that rear indicates position where next enqueued item will go

Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 1 A... rear front - fixed at [0] (similar to bottom of stack) enqueue (A)

Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 2 AB... rear front - fixed at [0] (similar to bottom of stack) enqueue (A) enqueue (B)

Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 3 ABC... rear front - fixed at [0] (similar to bottom of stack) enqueue (A) enqueue (B) enqueue (C)

Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 3 ABC... rear front - fixed at [0] (similar to bottom of stack) enqueue (A) enqueue (B) enqueue (C) dequeue() But now front is at position[1], not [0] Need to shift remaining items down!

Queue: Implementation Level Using an array: Option 1 items [0] [maxSize - 1] 2 BC... rear front - fixed at [0] (similar to bottom of stack) enqueue (A) enqueue (B) enqueue (C) dequeue() After the shifting Is this a very efficient implementation? Θ(n)

Queue: Implementation Level Using an array: Option 2 items [0][4] 0 rear front 1 Note: Let maxSize = 5 for the example Keep track of both front and rear rear is actual rear (except when empty) Note that: length = rear – front + 1 front is actual front (except when empty)

Queue: Implementation Level Using an array: Option 2 items [0][4] 1 A rear front 1 Note: Let maxSize = 5 for the example enqueue (A) Note that: length = rear – front + 1

Queue: Implementation Level Using an array: Option 2 items [0][4] 2 AB rear front 1 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) Note that: length = rear – front + 1

Queue: Implementation Level Using an array: Option 2 items [0][4] 3 ABC rear front 1 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) Note that: length = rear – front + 1

Queue: Implementation Level Using an array: Option 2 items [0][4] 4 ABCD rear front 1 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) Note that: length = rear – front + 1 Hmm... Queue now appears full... but

Queue: Implementation Level Using an array: Option 2 items [0][4] 4 ABCD rear front 2 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) dequeue () Note that: length = rear – front + 1

Queue: Implementation Level Using an array: Option 2 items [0][4] 4 A B CD rear front 3 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) dequeue () Note that: length = rear – front + 1 What if we want to Enqueue a couple of more items? Why not let Queue elements “wrap around” in array?

Queue: Implementation Level Using an array: Option 2 items [0][4] 0 EABCD rear front 3 Note: Let maxSize = 5 for the example Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue () Enqueue (E) Note that: length = rear – front + 1 ??? Note: to advance the rear indicator : rear = (rear + 1) % maxSize correction: length = ((rear+maxSize) – front + 1) % maxSize

Queue: Implementation Level Using an array: Option 2 items [0][4] 1 EFBCD rear front 3 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) dequeue () enqueue (E) enqueue (F) remember: length = ((rear+maxSize) – front + 1) % maxSize

Queue: Implementation Level Using an array: Option 2 items [0][4] 2 EFGCD rear front 3 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) dequeue () enqueue (E) enqueue (F) enqueue (G) remember: length = ((rear+maxSize) – front + 1) % maxSize

Queue: Implementation Level Using an array: Option 2 items [0][4] 2 EFGCD rear front 3 Note: Let maxSize = 5 for the example Also Note: to advance the rear or front indicators: rear = (rear + 1) % maxSize front = (front+1) % maxSize Now queue REALLY IS full! But look at values of front and rear and... Oops! This is supposed to mean queue is empty !!! ??? remember: length = ((rear+maxSize) – front + 1) % maxSize length = (( 2 + 5) – 3 + 1) % 5 = 0 Solution: Don’t let this happen; “waste” an array position!

Queue: Implementation Level Using a linked list: front NULL rear 0 size

Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) A 1 size

Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) AB 2 size

Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) ABC 3 size

Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) dequeue () BC 2 size

Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) dequeue () C 0 size

Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) dequeue () 0 size