CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

§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.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Data Structures and Algorithms (60-254)
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
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.
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.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
CS 206 Introduction to Computer Science II 10 / 15 / 2008 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.
Transforming Infix to Postfix
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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’
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Stacks And Queues Chapter 18.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Linear Data Structures
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Stacks Chapter 5.
COMPSCI 107 Computer Science Fundamentals
Stacks and Queues.
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
CMSC 341 Lecture 5 Stacks, Queues
Stacks Chapter 5 Adapted from Pearson Education, Inc.
The List, Stack, and Queue ADTs
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues

Overview  Stacks and Queues  Data structure holding a collection of objects, ordered by when they were inserted into the structure.  Stack can access newest object.  Queue can access oldest object. 2

Stacks 3  A stack is a linear collection whose elements are added and removed from the same end  Stacks are processed in a last in, first out (LIFO) manner  Usually, stacks are depicted vertically, and we refer to the top of the stack as the end to which elements are added and removed

Stack Operations 4  push: places an element onto the top of a stack  pop: removes an element from the top of the stack  peek: which retrieves (copies) a value from the top of the stack without removing it  isempty: an operation to determine whether or not the stack is empty  empty: an operation to empty out a stack

The Stack Interface 5

 Iast-in, first-out or LIFO policy  Last item pushed on the stack is next item popped off the stack.  Example: A stack of plates. 6

The Stack Interface 7

Push 8  Push means place a new data element at the top of the stack

Push (cont.) 9  Push means place a new data element at the top of the stack

Push (cont.) 10  Push means place a new data element at the top of the stack

Push (cont.) 11  Push means place a new data element at the top of the stack

Pop 12  Pop means take a data element off the top of the stack

Pop (cont.) 13  Pop means take a data element off the top of the stack

Pop (cont.) 14  Pop means take a data element off the top of the stack

Pop (cont.) 15  Pop means take a data element off the top of the stack

Peek 16  Peek means retrieve the top of the stack without removing it

Peek (cont.) 17  Peek means retrieve the top of the stack without removing it

Peek (cont.) 18  Peek means retrieve the top of the stack without removing it

Linked-List Stack 19  Stacks can be implemented with a linked list  The front node is the top of the stack

Linked-List Stack (cont.) 20 top To pop, we remove the node at the front of the linked list, and return the element to the client…

Linked-List Stack (cont.) 21 top To pop, we remove the node at the front of the linked list, and return the element to the client…

Linked-List Stack (cont.) 22 top To push, we place the new element in a node and insert it at the front of the linked list…

The Program (Call) Stack  When a method is called  Runtime environment creates activation record  Shows method's state during execution  Activation record pushed onto the program stack (Java stack)  Top of stack belongs to currently executing method  Next method down is the one that called current method 23

The Program Stack The program stack at 3 points in time; (a) when main begins execution; (b) when methodA begins execution, (c) when methodB begins execution. 24

Evaluating Postfix Expressions 25  Before looking further at the implementation of a stack, let's first see how one might be used  Arithmetic operations are traditionally written in infix notation, meaning that the operator is placed between its operands in the form  When evaluating infix expressions, we rely on precedence rules to determine the order of operator evaluation  In a postfix expression, the operator comes after its two operands

Evaluating Postfix Expressions 26  The process of evaluating a postfix expression can be stated simply:  scan from left to right,  apply each operation to the two previous operands immediately preceding it and  replace the operator with the result  Consider the infix expression: * 2  In postfix notation, it would be written as: * +

Evaluating Postfix Expressions 27  Consider the design of a program that can compute the result of a postfix expression  The evaluation rule relies on being able to retrieve the previous two operands whenever we encounter an operator  A large postfix expression will have many operators and operands to manage  A stack is the perfect collection to use in this solution

Evaluating Postfix Expressions 28  Solution algorithm:  scan the expression from left to right, identifying each token as an operator or operand  if the scanned token is an operand, push it onto the stack  if the scanned token is an operator pop the top two elements off the stack, apply the operation to them, and push the result onto the stack  If we reach the end of the expression the remaining element on the stack is the result of the expression (otherwise the expression was not well formed)

Using a Stack to Evaluate a Postfix Expression 29 Given the expression: * / * 7 7 top * * / / * *

Infix-to-Postfix Algorithm Symbol in Infix Action OperandAppend to end of output expression Operator ^Push ^ onto stack Operator +,-, *, or / Pop operators from stack, append to output expression until stack empty or top has lower precedence than new operator. Then push new operator onto stack Open parenthesis Push ( onto stack Close parenthesis Pop operators from stack, append to output expression until we pop an open parenthesis. Discard both parentheses. 30

Transforming Infix to Postfix Steps to convert the infix expression a / b * ( c + ( d – e ) ) to postfix form. 31

Evaluating Postfix Expression Fig The stack during the evaluation of the postfix expression a b / when a is 2 and b is 4 32

Evaluating Postfix Expression Fig The stack during the evaluation of the postfix expression a b + c / when a is 2, b is 4 and c is 3 33

Evaluating Infix Expressions Two stacks during evaluation of a + b * c when a = 2, b = 3, c = 4; (a) after reaching end of expression; (b) while performing multiplication; (c) while performing the addition 34

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 Stacks35

QUEUES 36 Definition: A sequence of elements of the same type. The first stored element is first accessible. The structure is known also under the name FIFO - first in first out

Basic operations 37 EnQueue : store a data item at the end of the queue DeQueue : retrieve a data item from the beginning of the queue

The Queue Interface  Queue  Very similar to a stack.  Items are inserted in one end (the back) and removed from the other end (the front).  first-in, first-out, or FIFO 38

The Queue Interface 39

Array Implementation of a Queue 40  Similar to the linked-list queue, there are two data members called front and back, but they are indexes into an Array instead of pointers  When enqueuing, the back index is incremented, and when dequeuing, the front index is incremented

Enqueue / Dequeue frontback

Enqueue / Dequeue (cont.) 42 frontback DEQUEUE

Enqueue / Dequeue (cont.) 43 frontback DEQUEUE

Enqueue / Dequeue (cont.) 44 frontback DEQUEUE

Enqueue / Dequeue (cont.) 45 frontback DEQUEUE

Enqueue / Dequeue (cont.) 46 frontback ENQUEUE

Enqueue / Dequeue (cont.) frontback ENQUEUE

Enqueue / Dequeue (cont.) frontback ENQUEUE

Enqueue / Dequeue (cont.) frontback ENQUEUE

frontback DEQUEUE Enqueue / Dequeue (cont.)

frontback DEQUEUE Enqueue / Dequeue (cont.)

frontback DEQUEUE Enqueue / Dequeue (cont.)

frontback DEQUEUE Enqueue / Dequeue (cont.)

frontback ENQUEUE Enqueue / Dequeue (cont.)

frontback ENQUEUE Enqueue / Dequeue (cont.)

frontback DEQUEUE Enqueue / Dequeue (cont.)

frontback DEQUEUE Enqueue / Dequeue (cont.)

frontback DEQUEUE Enqueue / Dequeue (cont.)

frontback ENQUEUE Enqueue / Dequeue (cont.)

frontback ENQUEUE ? Enqueue / Dequeue (cont.)

frontback ENQUEUE We could double the size of the array here. Enqueue / Dequeue (cont.)

frontback ENQUEUE But if we keep doing this, we may have a million elements in the Array, but only a few at the end are used! Enqueue / Dequeue (cont.)

frontback ENQUEUE We handle this problem by having the back wrap around to the beginning of the array. Enqueue / Dequeue (cont.)

frontback ENQUEUE Enqueue / Dequeue (cont.)

frontback The front also wraps to the beginning when it reaches the end of the array Enqueue / Dequeue (cont.)

How Do We Know When the Array is Full? 66  We may still need to double the capacity of the array if it gets filled  An array will be full when  back + 1 == front  OR  back + 1 == capacity AND front == 0

A Full Array frontback

A Full Array frontback If the next operation is ENQUEUE, the array capacity will need to be doubled

Implementing Queues with Links 69  Because a queue is a linear collection, we can implement a queue as a linked list of LinearNode objects, as we did with stacks  One important difference, however, is that we will have to operate on both ends of the list  So we'll add an additional reference variable ( rear ) that points to the last element in the list

Dequeue Operation 70 front back header

Dequeue Operation (cont.) 71 front back header

Enqueue Operation 72 front back header

Enqueue Operation (cont.) 73 front back header

Summary  Stacks and queues are collections of objects.  Stack follows a last-in, first-out policy. Objects are pushed onto and popped off the top.  Queue follows a first-in, first-out policy. Objects are added to the back and removed from the front. 74