Chapter 4 Stacks and Queues

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

Chapter 4: Stacks and Queues
Ceng-112 Data Structures I Chapter 5 Queues.
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.
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
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.”
ALGORITHMS FOR ISNE DR. KENNETH COSH WEEK 3.
Queues and Priority Queues
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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 16 Stack and Queues part2
Queues and Priority Queues
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Stacks and Queues Introduction to Computing Science and Programming I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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’
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
Stacks And Queues Chapter 18.
Data Structures and Algorithm Analysis Dr. Ken Cosh Stacks ‘n’ 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.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Computer Engineering Rabie A. Ramadan Lecture 6.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
1 Data Organization Example 1: A simple text editor –Store the text buffer as a list of lines. –How would we implement the UNDO operation? Example 2: Parsing.
Queues Chapter 8 (continued)
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Review Array Array Elements Accessing array elements
Data Abstraction & Problem Solving with C++
Queues.
Queues Chapter 4.
Unit 4 Stacks And Queues King Fahd University of Petroleum & Minerals
G64ADS Advanced 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.
Chapter 12 – Data Structures
COSC160: Data Structures: Lists and Queues
CC 215 Data Structures Queue ADT
Chapter 15 Lists Objectives
Stacks and Queues.
Queues Queues Queues.
Queues Chapter 4.
Algorithms and Data Structures
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
ITEC 2620M Introduction to Data Structures
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
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.
Abstract Data Type Abstract Data Type as a design tool
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Visit for more Learning Resources
Lecture 2: Stacks and Queues
Abstract Data Type (ADT)
CSCS-200 Data Structure and Algorithms
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Queues.
Stack Implementations
Presentation transcript:

Chapter 4 Stacks and Queues

Objectives Discuss the following topics: Stacks Queues Priority Queues Case Study: Exiting a Maze

Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data A stack is called an LIFO structure: last in/first out

Stacks (continued) The following operations are needed to properly manage a stack: clear() — Clear the stack isEmpty() — Check to see if the stack is empty push(el) — Put the element el on the top of the stack pop() — Take the topmost element from the stack topEl() — Return the topmost element in the stack without removing it

Stacks (continued) Figure 4-1 A series of operations executed on a stack

Stacks (continued) a = b + (c – d ) * (e – f); g[10] = h[i[9]] + (j + k) * l; while (m < (n[8] + o)) { p = 7; /* initialize p */ r = 6; } These examples are statements in which mismatching occurs: a = b + (c – d) * (e – f)); g[10] = h[i[9]] + j + k) * l; while (m < (n[8] + o]) { p = 7; /* initialize p */ r = 6; } while (m < (n[8] + o))

Stacks (continued) Figure 4-2 Processing the statement s=t[5]+u/(v*(w+y)); with the algorithm delimiterMatching()

Stacks (continued) Figure 4-2 Processing the statement s=t[5]+u/(v*(w+y)); with the algorithm delimiterMatching() (continued)

Stacks (continued) Figure 4-3 An example of adding numbers 592 and 3,784 using stacks

Stacks (continued) Figure 4-4 Array list implementation of a stack

Stacks (continued) Figure 4-4 Array list implementation of a stack (continued)

Stacks (continued) Figure 4-5 Implementing a stack as a linked list

Stacks (continued) Figure 4-5 Implementing a stack as a linked list (continued)

Stacks (continued) Figure 4-6 A series of operations executed on an abstract stack (a) and the stack implemented with an array (b) and with a linked list (c)

Stacks in java.util Figure 4-7 A list of methods in java.util.Stack; all methods from Vector are inherited

Queues A queue is a waiting line that grows by adding elements to its end and shrinks by taking elements from its front A queue is a structure in which both ends are used: One for adding new elements One for removing them A queue is an FIFO structure: first in/first out

Queues (continued) The following operations are needed to properly manage a queue: clear() — Clear the queue isEmpty() — Check to see if the queue is empty enqueue(el) — Put the element el at the end of the queue dequeue() — Take the first element from the queue firstEl() — Return the first element in the queue without removing it

Queues (continued) Figure 4-8 A series of operations executed on a queue

Queues (continued) Figure 4-9 Two possible configurations in an array implementation of a queue when the queue is full

Queues (continued) Figure 4-10 Array implementation of a queue

Queues (continued) Figure 4-10 Array implementation of a queue (continued)

Queues (continued) Figure 4-10 Array implementation of a queue (continued)

Queues (continued) Figure 4-11 Linked list implementation of a queue

Queues (continued) Figure 4-11 Linked list implementation of a queue (continued)

Queues (continued) In queuing theory, various scenarios are analyzed and models are built that use queues for processing requests or other information in a predetermined sequence (order)

Queues (continued) Queuing theory is when various scenarios are analyzed and models are built that use queues Figure 4-12 A series of operations executed on an abstract queue (a) and the stack implemented with an array (b) and with a linked list (c)

Queues (continued) Figure 4-13 Bank One example: (a) data for number of arrived customers per one-minute interval and (b) transaction time in seconds per customer

Queues (continued) Figure 4-14 Bank One example: implementation code

Queues (continued) Figure 4-14 Bank One example: implementation code (continued)

Queues (continued) Figure 4-14 Bank One example: implementation code (continued)

Queues (continued) Figure 4-14 Bank One example: implementation code (continued)

Priority Queues A priority queue can be assigned to enable a particular process, or event, to be executed out of sequence without affecting overall system operation In priority queues, elements are dequeued according to their priority and their current queue position

Priority Queues (continued) Priority queues can be represented by two variations of linked lists: All elements are entry ordered Order is maintained by putting a new element in its proper position according to its priority

Case Study: Exiting a Maze Figure 4-15 (a) A mouse in a maze; (b) two-dimensional character array representing this situation

Case Study: Exiting a Maze (continued) exitMaze() initialize stack, exitCell, entryCell, currentCell = entryCell; while currentCell is not exitCell mark currentCell as visited; push onto the stack the unvisited neighbors of currentCell; if stack is empty failure; else pop off a cell from the stack and make it currentCell; success;

Case Study: Exiting a Maze (continued) Figure 4-16 An example of processing a maze

Case Study: Exiting a Maze (continued) Figure 4-17 Listing of the program for maze processing

Case Study: Exiting a Maze (continued) Figure 4-17 Listing of the program for maze processing (continued)

Case Study: Exiting a Maze (continued) Figure 4-17 Listing of the program for maze processing (continued)

Case Study: Exiting a Maze (continued) Figure 4-17 Listing of the program for maze processing (continued)

Case Study: Exiting a Maze (continued) Figure 4-17 Listing of the program for maze processing (continued)

Case Study: Exiting a Maze (continued) Figure 4-17 Listing of the program for maze processing (continued)

Case Study: Exiting a Maze (continued) Figure 4-17 Listing of the program for maze processing (continued)

Summary A stack is a linear data structure that can be accessed at only one of its ends for storing and retrieving data. A stack is called an LIFO structure: last in/first out. A queue is a waiting line that grows by adding elements to its end and shrinks by taking elements from its front. A queue is an FIFO structure: first in/first out.

Summary (continued) In queuing theory, various scenarios are analyzed and models are built that use queues for processing requests or other information in a predetermined sequence (order). A priority queue can be assigned to enable a particular process, or event, to be executed out of sequence without affecting overall system operation.

Summary (continued) In priority queues, elements are dequeued according to their priority and their current queue position.