Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
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.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
Huffman code uses a different number of bits used to encode characters: it uses fewer bits to represent common characters and more bits to represent rare.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Stacks and Queues Introduction to Computing Science and Programming I.
Introduction to Data Structures Fall 2008 Dr. David A. Gaitros
Generic Positional Containers and Double-Ended Queues.
Suppose you have a problem involving N data points. Recursive solution of such problem is a follows: If the problem can be solved directly for N points.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
C++ STL CSCI 3110.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
Lecture 7 : Intro. to STL (Standard Template Library)
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)
C++ Review STL CONTAINERS.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
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.
Mark Redekopp David Kempe
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Arrays.
Objectives In this lesson, you will learn to: Define stacks
Algorithms and Data Structures
Stack and Queue.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CMSC 341 Lecture 5 Stacks, Queues
Chapter 19: Stacks and Queues.
Lecture 21 Stacks and Queues Richard Gesick.
Data Structures and Algorithms for Information Processing
Container classes, ADTs
Lecture 8 : Intro. to STL (Standard Template Library)
C++ STL Stack, Queue, and Deque
Lab4 problems More about templates Some STL
Lecture 9: Stack and Queue
Chapter 4 Stacks and Queues
Data Structures & Programming
Data Structures & Programming
Presentation transcript:

Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing new element at the front of the queue

Queue: Push top Max Max top Lex Lex Nika Nika Push Vlad Vlad Joe Joe

Queue: Pop pop Max Max top Lex Lex Nika Nika Vlad Vlad Joe Joe

Generic queue Class (STL) #include <queue> queue<string> myQueue; size() returns queue size; front() returns the front queue element; push () adds new element to (the end of) the queue; pop() removes element from (the front of) the queue;

Applications Various lists: print queues, message queues, service request queues, process / thread queues; O(1) push & pop operations. Round robin: circular queue (front element linked to last.

A queue can be implemented with the help of a linked list: Implementation A queue can be implemented with the help of a linked list: front Max Lex Nika end Vlad

Deque DEQueue – Double-Ended Queue = stack + queue hybrid, supports adding and removing elements at the front and at the end. push for adding new element at the end of the queue pop for removing new element at the front of the queue

Deque: Push Joe Joe Max Max Lex Lex Nika Nika Vlad Vlad Jane Jane top push_front Joe top Max Max Lex Lex Nika Nika push_end Vlad end Vlad Jane Jane end

Generic deque Class (STL) #include <deque> deque<string> myDeque; * supports all of the methods of vector and list (including indexes and iterators) size() returns deque size; front() returns the front deque element; back() returns the back deque element; push_front() - adds new element to the front of the deque; push_end() - adds new element to the end of the deque; pop_front() - removes element from the front of the deque; pop_end() - removes element from the back of the deque;

Exercise: Operation Priority Figure out the priority of operations in an algebraic expression, e.g. Expression: a*(b+c)-d/e+f Priority: 2 3 1 3 1 Using string and queue Read expression from cin into a string Look at each character and if it is +,-,*,/ determine the priority of arithmetic operation populate the queue with operation in its priority, e.g. (*,2) (+,3) (-,1) (/,3) (+,1) Print the queue contents

Operation Priority Solution Declare user-defined type: struct OperationPriority { char Operation; int Priority; }; Your queue must store OperationPriority objects Assign priority levels to operations: 2 for *,/; 1 for +,- Read expression into string Set nestingLevel=0 Look at each character If the character is ‘(‘ then nestingLevel++ If the character is ‘)‘ then nestingLevel-- If the character is ’*’, ’/’ then priority = nestingLevel*2 + 2 If the character is ’+’, ’-’ then priority = nestingLevel*2 + If the character is ’+’, ’-’, ‘*’, ‘/’ then add it to queue including the priority value cout the queue

Assignment Read chapter 6, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.