Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

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.
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Chapter 12: Data Structures
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
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.”
DS.L.1 Lists, Stacks, and Queues (Review) Chapter 3 Overview Abstract Data Types Linked Lists, Headers, Circular Links Cursor (Array) Implementation Stacks.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
Stack and Queue.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
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’
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
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)
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Queues Chapter 8 (continued)
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Lecture 7 Queues Stacks Trees.
COSC160: Data Structures: Lists and Queues
Chapter 12: Data Structures
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
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.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Presentation transcript:

Lecture 10 b Stacks b Queues

2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In, First-Out b Analogy: a stack of plates poppush

Stack Data Structure b A stack is a data structure supporting the following operations: isEmpty() : check if empty.isEmpty() : check if empty. top(): examine the top element.top(): examine the top element. push(item): add a new top element.push(item): add a new top element. pop(): remove the top element.pop(): remove the top element. clear(): clear the stack.clear(): clear the stack. b All operations should be constant-time (do not depend on size of stack)

Applications of Stacks b Managing call frames of subprograms b Expression evaluation b Elimination of recursion b “Searching” for a solution

Stack implementations Stack implementations b Array (or vector) space efficientspace efficient per operation time is constant, but significant delays at times of reallocationper operation time is constant, but significant delays at times of reallocation b Linked list space overhead may be significantspace overhead may be significant space for stack is allocated incrementally (not in “bursts”)space for stack is allocated incrementally (not in “bursts”)

Array Based Implementation tos = push(5) push(3) push(7) pop() push(8) gets overwritten

List Based Implementation push(5) push(3) push(7) pop() 5=37 tos

List Based Implementation This object is now garbage. push(5) push(3) push(7) pop() push(8) 53 7 tos 8

Analysis b It is easy to check that all operations in the linked list case are O(1). b Since arrays are fixed length, our implementation dynamically doubles the size of allocated array when needed. b This happens if we perform a push operation when the allocated array is full. b In this case, a fresh array of double the size is allocated and the contents of the old array are copied into this one.

Analysis (continued) b Thus most push are O(1) except the ones where doubling and copying happens. (the other operations are O(1) clearly.) b Since these are infrequent, we can spread the cost over a sequence of stack operations. b With this, a sequence of M stack operations is O(M). b This technique is called amortization.

11 Queues b A queue is similar to a list but adds items only to the end of the list and removes them from the front b It is called a FIFO data structure: First-In, First-Out b Analogy: a line of people at a bank teller’s window enqueue dequeue

Queue Data Structure b A queue is a data structure supporting the following operations: isEmpty() : check if empty.isEmpty() : check if empty. enqueue(item): add a new element at the rear of the queue.enqueue(item): add a new element at the rear of the queue. dequeue(): remove the front element.dequeue(): remove the front element. clear(): clear the queue.clear(): clear the queue. b All operations should be constant-time (do not depend on size of queue)

Applications of Queues b Managing system resources (eg, printer queues) b “Searching” for a solution b Simulations (eg: airplanes landing and taking off at an airport) b Graph traversals

Queue implementations Queue implementations b Vector unacceptably slow dequeue (moves up all items)unacceptably slow dequeue (moves up all items) b Array space efficientspace efficient using fixed-size circular array also very time efficientusing fixed-size circular array also very time efficient b Linked list space overhead may be significantspace overhead may be significant allows maximum flexibility (queue can grow and shrink as needed)allows maximum flexibility (queue can grow and shrink as needed)

Circular arrays Circular arrays  head and count  head and tail  head and tail and queueEmpty