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. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
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.
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.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
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.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
TCSS 342, Winter 2005 Lecture Notes
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.”
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
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’
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
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.
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Data Abstraction & Problem Solving with C++
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
COSC160: Data Structures: Lists and Queues
CC 215 Data Structures Queue ADT
C++ Plus Data Structures
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 19: Stacks and Queues.
C++ Plus Data Structures
Queues.
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues CSE 373 Data Structures.
Queues.
CSCS-200 Data Structure and Algorithms
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: Boolean IsEmpty () Boolean IsFull () Push (ItemType newitem) void Pop () ItemType Top ()

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: // void B ( ) { } // void A ( ) { B (); } // int main ( ) { A (); return 0; }

Consider the following: main begins executing main calls function A function A calls function B function B returns function 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) set balanced to false else use Top to get copy of opening symbol on top of stack Pop the stack 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

Stack: Implementation Level Using an array: items [0] [MAX_ITEMS - 1] top

Stack: Implementation Level Using an array: items [0] [MAX_ITEMS - 1] 0 top Push ( 70 )

Stack: Implementation Level Using an array: items [0] [MAX_ITEMS - 1] 1 top Push ( 70 ) Push ( 28)

Stack: Implementation Level Using an array: items [0] [MAX_ITEMS - 1] 2 top Push ( 70 ) Push ( 28) Push ( 88)

Stack: Implementation Level Using an array: items [0] [MAX_ITEMS - 1] 1 top Push ( 70 ) Push ( 28) Push ( 88) Pop

Stack: Implementation Level Using an array: items [0] [MAX_ITEMS - 1] 2 top Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95)

Stack: Implementation Level Using an array: items [0] [MAX_ITEMS - 1] 1 top Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95) Pop

Stack: Implementation Level Using an array: items [0] [MAX_ITEMS - 1] 0 top Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95) Pop

Stack: Implementation Level Using an array: items [0] [MAX_ITEMS - 1] top Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95) Pop

Stack: Implementation Level Using a linked list: top NULL

Stack: Implementation Level Using a linked list: top NULL Push ( 70 ) 70

Stack: Implementation Level Using a linked list: top NULL Push ( 70 ) Push ( 28 ) 2870

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

Stack: Implementation Level Using a linked list: top NULL 2870 Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop

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

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

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

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: Boolean IsEmpty () Boolean IsFull () void Enqueue (ItemType newitem) void Dequeue (ItemType& newitem)

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] [MAXQUEUE - 1]... rear front - fixed at [0] (similar to bottom of stack)

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

Queue: Implementation Level Using an array: Option 1 items [0] [MAXQUEUE - 1] 1 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] [MAXQUEUE - 1] 2 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] [MAXQUEUE - 1] 2 ABC... rear front - fixed at [0] (similar to bottom of stack) Enqueue (A) Enqueue (B) Enqueue (C) Dequeue(ch) 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] [MAXQUEUE - 1] 1 BC... rear front - fixed at [0] (similar to bottom of stack) Enqueue (A) Enqueue (B) Enqueue (C) Dequeue(ch) After the shifting Is this a very efficient implementation?

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

Queue: Implementation Level Using an array: Option 2 items [0][4] 0 A rear front 0 Note: Let MAXQUEUE = 5 for the example Keep track of both front and rear Note: queue is empty when (front – 1) == rear Enqueue (A)

Queue: Implementation Level Using an array: Option 2 items [0][4] 1 AB rear front 0 Note: Let MAXQUEUE = 5 for the example Keep track of both front and rear Note: queue is empty when (front – 1) == rear Enqueue (A) Enqueue (B)

Queue: Implementation Level Using an array: Option 2 items [0][4] 2 ABC rear front 0 Note: Let MAXQUEUE = 5 for the example Keep track of both front and rear Note: queue is empty when (front – 1) == rear Enqueue (A) Enqueue (B) Enqueue (C)

Queue: Implementation Level Using an array: Option 2 items [0][4] 3 ABCD rear front 0 Note: Let MAXQUEUE = 5 for the example Keep track of both front and rear Note: queue is empty when (front – 1) == rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D)

Queue: Implementation Level Using an array: Option 2 items [0][4] 3 ABCD rear front 1 Note: Let MAXQUEUE = 5 for the example Keep track of both front and rear Note: queue is empty when (front – 1) == rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch)

Queue: Implementation Level Using an array: Option 2 items [0][4] 3 ABCD rear front 2 Note: Let MAXQUEUE = 5 for the example Keep track of both front and rear Note: queue is empty when (front – 1) == rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch)

Queue: Implementation Level Using an array: Option 2 items [0][4] 4 ABCDE rear front 2 Note: Let MAXQUEUE = 5 for the example Keep track of both front and rear Note: queue is empty when (front – 1) == rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Enqueue (E) Hmm... Queue now appears full, but there are two unused positions in array! Why not let Queue elements “wrap around” in array?

Queue: Implementation Level Using an array: Option 2 items [0][4] 0 FBCDE rear front 2 Note: Let MAXQUEUE = 5 for the example Keep track of both front and rear Note: queue is empty when (front – 1) == rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Enqueue (E) Enqueue (F) Note: to advance the rear indicator : rear = (rear + 1) % MAXQUEUE

Queue: Implementation Level Using an array: Option 2 items [0][4] 1 FGCDE rear front 2 Note: Let MAXQUEUE = 5 for the example Keep track of both front and rear Note: queue is empty when (front -1) == rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Enqueue (E) Enqueue (F) Enqueue (G) Note: to advance the rear indicator : rear = (rear + 1) % MAXQUEUE Now queue REALLY IS full! But look at values of front and rear... (front-1) == rear Yikes! This is supposed to mean queue is empty !!!

Queue: Implementation Level Using an array: Option 3 items [0][4] 4 rear front 4 Note: Let MAXQUEUE = 5 for the example Still keep track of both front and rear queue is empty when front == rear front indicates position just before actual front queue is full when (rear + 1) % MAXQUEUE == front (when next Enqueue would put item in unused position.) This position must remain unused, effectively reducing size of queue by 1

Queue: Implementation Level Using an array: Option 3 items [0][4] 0 A rear front 4 Note: Let MAXQUEUE = 5 for the example Still keep track of both front and rear Enqueue (A)

Queue: Implementation Level Using an array: Option 3 items [0][4] 1 AB rear front 4 Note: Let MAXQUEUE = 5 for the example Still keep track of both front and rear Enqueue (A) Enqueue (B)

Queue: Implementation Level Using an array: Option 3 items [0][4] 2 ABC rear front 4 Note: Let MAXQUEUE = 5 for the example Still keep track of both front and rear Enqueue (A) Enqueue (B) Enqueue (C)

Queue: Implementation Level Using an array: Option 3 items [0][4] 3 ABCD rear front 4 Note: Let MAXQUEUE = 5 for the example Still keep track of both front and rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) (Note: queue full)

Queue: Implementation Level Using an array: Option 3 items [0][4] 3 ABCD rear front 0 Note: Let MAXQUEUE = 5 for the example Still keep track of both front and rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch)

Queue: Implementation Level Using an array: Option 3 items [0][4] 3 ABCD rear front 1 Note: Let MAXQUEUE = 5 for the example Still keep track of both front and rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch)

Queue: Implementation Level Using an array: Option 3 items [0][4] 3 ABCD rear front 2 Note: Let MAXQUEUE = 5 for the example Still keep track of both front and rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch)

Queue: Implementation Level Using an array: Option 3 items [0][4] 3 ABCD rear front 3 Note: Let MAXQUEUE = 5 for the example Still keep track of both front and rear Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) (Note: queue empty)

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

Queue: Implementation Level Using a linked list: front NULL rear A Enqueue (A)

Queue: Implementation Level Using a linked list: front NULL rear A Enqueue (A) Enqueue (B) B

Queue: Implementation Level Using a linked list: front NULL rear A Enqueue (A) Enqueue (B) Enqueue (C) BC

Queue: Implementation Level Using a linked list: front NULL rear B Enqueue (A) Enqueue (B) Enqueue (C) Dequeue (ch) C

Queue: Implementation Level Using a linked list: front NULL rear C Enqueue (A) Enqueue (B) Enqueue (C) Dequeue (ch)

Queue: Implementation Level Using a linked list: front NULL rear NULL Enqueue (A) Enqueue (B) Enqueue (C) Dequeue (ch)