CSC 143 Queues [Chapter 7].

Slides:



Advertisements
Similar presentations
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Advertisements

Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
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.
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.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,
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.”
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.
Data Structure Dr. Mohamed Khafagy.
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.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
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.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
131 3/30/98 CSE 143 More Collection ADTs [Sections ]
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
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.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
(c) , University of Washington19a-1 CSC 143 Stacks and Queues: Concepts and Implementations.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
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++
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Review Array Array Elements Accessing array elements
Data Abstraction & Problem Solving with C++
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
UNIT II Queue.
CC 215 Data Structures Queue ADT
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
CSE 373: Data Structures and Algorithms
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues.
Queues.
Chapter 19: Stacks and Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Programming Abstractions
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CSC 143 Stacks [Chapter 6].
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.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues Jyh-Shing Roger Jang (張智星)
Stacks and Queues: Concepts and Implementations
Queues Definition of a Queue Examples of Queues
CSCS-200 Data Structure and Algorithms
Getting queues right … finally (?)
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Data Structures & Programming
Presentation transcript:

CSC 143 Queues [Chapter 7]

A Classification of ADTs Collections Ordered Collections Unordered Collections Direct Sequential Heterogeneous Homogeneous Vector Struct List Stack Table Set Queue Bag

A Stack Definition (Review) Stack: “Homogeneous, ordered collection, accessed only at the front for removal and insertion” Top: last value in, first value out top ... aStack:

When A Stack Is Not Quite Right People line up a use a pay phone There’s only one place you leave the line (at the front, when the phone is free) There’s only one place you enter the line (at the end -- everyone assumed to be polite!) The two places are different! So it's not a stack First in, first out: a “queue”

Queue Definition Queue: “Homogeneous, ordered collection, accessed only at the front (removal) and rear (insertion)” Front: First element in queue Rear: Last element of queue FIFO: First In, First Out front rear ... aQueue:

Abstract Queue Operations insert(item) : Adds an element to rear of queue succeeds unless the queue is full often called “enqueue” item remove( ) : Removes and returns element at the front of queue succeeds unless the queue is empty often called “dequeue” item getFront( ) : Returns a copy of the front element of queue No cursor, no iteration

Queue Example Queue q; int v1, v2; q.insert(4); q.insert(7); Draw a picture and show the changes to the queue in the following example: Queue q; int v1, v2; q.insert(4); q.insert(7); q.insert(5); v1 = q.remove(); v2 = q.getFront(); q.insert(9); q.insert(2);

What is the result of: Queue q; int v1,v2,v3,v4,v5,v6 q.insert(1); v1 = q.remove(); v2 = q.getFront(); q.insert(4); v3 = q.remove(); v4 = q.getFront(); q.insert(5); v5 = q.remove(); v6 = q.getFront();

Queue vs. Stack If I put a bunch of stuff in a queue or stack and then take it all out again... Queue: get it back in the original order Stack: get it back in reverse order Does this suggest an algorithm for checking palindromes?

Common Uses of Queues Internal to a computer Processes in an operating system, waiting for service (CPU, I/O, etc.) Buffering input/output When printing to screen with cout, characters don’t appear right away: they are buffered and sent out in groups. Print jobs at the printer Mouse events needed to be handled Simulations People or things waiting in line for service, …

Public Queue Interface class IntQueue { //see p.310 public: IntQueue( ); //should define a copy // constructor, too bool isEmpty( ); void insert(int item); int remove( ); int getFront( ); private: // more than one way to do it. //Should the client care? };

Queue Implementations Choices similar to Stack Existing Vector ADT implementation Linked list Array-based Queues are a little more complicated than Stacks More complexity in the implementation More ways to do it

Queue Implementations Use existing Vector ADT Convention: Rear is last position of vector Convention: Front is always position 0 Quite straightforward, very few lines of code

Another Implementation Linked list With L.L. we could follow Vector ADT pattern Insert at end of linked list Remove from front of list A separate “rear” pointer would increase efficiency Another linked-list approach: a circular list Let rear node point back to head node

Conventional vs. Circular (private, local) (on heap) head 4 8 16 size 3 rear 4 8 16 size 3

Array Implementation Array-based Stack: nItems was enough Queue: need to keep track of front and rear separately still need to recognize empty queue, full queue

First Attempt int front, rear; Start at index 0, add new items left to right Insert: rear++ Delete: front ++ 1 ... This is a good start, but over time... what goes wrong?

Wrap Around int size, front, rear; rear = (rear+1) % MAX; //on enqueue queueEmpty = (size == 0) queueFull = (size == MAX) Textbook p.317-318 1 2