Chapter 16 Stack and Queues part2

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

Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
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.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
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.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
CS Data Structures II Review COSC 2006 April 14, 2017
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures & Algorithms
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,
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.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
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.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
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 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)
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
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.
Exam Review 5/28/15. Software Development (Review)  Make it work…then make it pretty can result in dangerous code  Better to take a unified, consistent.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
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,
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
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.
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.
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,
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Review Array Array Elements Accessing array elements
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Queues.
Chapter 15 Lists Objectives
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stacks and Queues.
Queues Queues Queues.
Queues Chapter 4.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Chapter 19: Stacks and Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks, Queues, and Deques
Getting queues right … finally (?)
Presentation transcript:

Chapter 16 Stack and Queues part2 Dr. Bernard Chen Ph.D. University of Central Arkansas

Introduction to Queues A queue is a waiting line It’s in daily life: A line of persons waiting to check out at a supermarket A line of persons waiting to purchase a ticket for a film A line of planes waiting to take off at an airport A line of vehicles at a toll booth

Introduction to Queues Difference between Stack and Queues: Stack exhibits last-in-first-out (LIFO) Queue exhibits first-in-first-out (FIFO)

ADT in Queues Unlike stacks in which elements are popped and pushed only at the ends of the list, Collection of data elements: items are removed from a queue at one end, called the FRONT of the queue; and elements are added at the other end, called the BACK

Queue ADT Basic operations Construct a queue Check if empty Enqueue (add element to back) Front (retrieve value of element from front) Dequeue (remove element from front)

Designing and Building a Queue Class Array-Based Consider an array in which to store a queue Note additional variables needed myFront, myBack Picture a queue object like this

Queue Operation Empty Queue Enqueue(70)

Queue Operation Enqueue(80) Enqueue(50)

Queue Operation Dequeue()

Queue Operation Enqueue(90) Enqueue(60)

Circular Queue Problems Possible solutions We quickly "walk off the end" of the array Possible solutions Shift array elements Use a circular queue Note that both empty and full queue gives myBack == myFront

Circular Queue Using a static array QUEUE_CAPACITY specified Enqueue increments myBack using mod operator, checks for full queue Dequeue increments myFront using mod operator, checks for empty queue

Circular Example Both Front and Back wraparound as needed. b c d Front g

QUEUE Only tricky part is vector doubling because the queue items are not necessarily stored in an array starting at location 0, and the contiguity of wraparound must be maintained. Therefore, mostly straightforward; maintain Front Back

Queue Full Situation If an item were stored in the last position, and an Enqueure() occurred myBack would be incremented by 1, giving it the same value as myFront However, myFront == myBack indicates the queue is empty Thus, we cannot distinguish between empty and full We may avoid this situation by maintaining one empty position, so that myFront will never equal to myBack unless the queue is empty

Queue Operation Construct: Empty: Front : Create an array, set capacity, myFront=myBack=0 Empty: test myFront==myBack Front : if not empty: print array[myFront]

Algorithm for Enqueue(value) 1. Set newBack == (myBack+1)%Queue_capacity 2. If newBack == myFront Signal “Queue Full” otherwise: Set Array[myBack] == value Set myBack == newBack

Algorithm for Dequeue() If queue is empty signal “Queue Empty” Otherwise Set myFront=(myFront+1)%Queue_capacity

Linked Queues We could also use linked list to store queue elements Can grow and shrink to fit the situation No need for upper bound (myCapacity)

Linked Queues Constructor initializes myFront, myBack Empty myFront == Null Front return myFront->data Dequeue Delete first node (watch for empty queue) Enqueue Insert node at end of list

Enqueue newptr= new Node(value) if (empty()) else { myFront=myBack=newptr; else { myBack->next=newptr; myBack=newwptr; }

Dequeue (if not empty) ptr=myFront myFront=myFront->next delete ptr;

Queue ADT implement by Vectors Basic operations Construct a queue Check if empty Enqueue (add element to back) Front (retrieve value of element from front) Dequeue (remove element from front)

Enqueue and Front Enqueue (add element to back) This is the same with push(), therefore: L.push_back(value); Front (retrieve value of element from front) L.begin();

Dequeue Dequeue (remove element from front) L.erase( L.begin() ); L.begin() is an iterator, in erase(), you cannot just gives the location

Functions related to Queue Constructor: vector<int> L; Empty(): L.size() == 0? Enqueue(): L.push_back(value); Front(): L.begin(); Dequeue(): L.erase(L.begin());

QUEUE Write a queue program

Vector Functions

Queue + Stack SCROLL: a queue-stack hybrid model Thus, you may have functions in both— Push(value)=Enqueue(value) Pop(); Top() --- top of stack Dequeue(); Front()