Data Structures Chapter 3 Queues Andreas Savva. 2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
Array based Queues A circular implementation. Implementation Option 1 As with a array based stack, there are multiple ways that a queue can be implemented.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
CHAPTER 4 QUEUE CSEB324 DATA STRUCTURES & ALGORITHM.
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.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
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.
Chapter3 QUEUES.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
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.
Data Structures Chapter 4 Linked Stacks and Queues Andreas Savva.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Topic 3 Basic Data Structures continued CSE1303 Part A Data Structures and Algorithms.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
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.
Stacks and Queues.
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.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
1 Queues. 2 Queue Which of the following cases use similar structures? Cars lined up at a tollgate Cars on a 4-lane highway Customers at supermarket check-out.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Kruse/Ryba ch041 Linked Stacks and Queues Pointers and Linked Structures Linked Stacks Linked Stacks with Safeguards Linked Queues Application: Polynomials.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Class List { public: // TYPEDEF and MEMBER CONSTANTS enum { CAPACITY = 30 }; // Or: static const size_t CAPACITY = 30; typedef double Item; // CONSTRUCTOR.
1 Data Structures CSCI 132, Spring 2016 Notes13 Queues as Linked Lists, Polynomial Arithmetic.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Basic Data Structures – Continued (Queues)
Linked Stacks and Queues
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Chapter 19: Stacks and Queues.
تهیه کنندگان مهری بابائی،گیتا جوادی رضا امید ملایری ،ناصر بهمدی
Queues.
CSC 143 Queues [Chapter 7].
Queues Jyh-Shing Roger Jang (張智星)
Visit for more Learning Resources
Queues Definition of a Queue Examples of Queues
Queue.
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Data Structures & Programming
Presentation transcript:

Data Structures Chapter 3 Queues Andreas Savva

2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled after a line of people waiting to be served. The first entry which is inserted is the first one that will be removed. (First In First Out : FIFO) The first entry which is inserted is the first one that will be removed. (First In First Out : FIFO)

3 Application of Queues in our everyday life

4 Waiting for a printer Waiting for a printer Access to disk storage Access to disk storage In a time-sharing system use of the CPU In a time-sharing system use of the CPU Along with stacks, queues are one of the simplest data structures Application of Queues Most common than Stacks

5 Basic Operations for Queues Create a queue, leaving it empty. Create a queue, leaving it empty. Clear the queue leaving it empty. Clear the queue leaving it empty. Test whether the queue is Empty. Test whether the queue is Empty. Test whether the queue is Full. Test whether the queue is Full. Return the Size of the queue. Return the Size of the queue. Retrieve the first entry of the queue, provided the queue is not empty. Retrieve the first entry of the queue, provided the queue is not empty. Serve: remove the first entry from the queue, provided the queue is not empty. Serve: remove the first entry from the queue, provided the queue is not empty. Retrieve and Serve : retrieves and remove the first entry from the queue, provided the queue is not empty. Retrieve and Serve : retrieves and remove the first entry from the queue, provided the queue is not empty. Append a new entry at the end of the queue, provided that the queue is not full. Append a new entry at the end of the queue, provided that the queue is not full. Print all the entries of the queue. Print all the entries of the queue.

6 The Class Queue methods: Queue (constructor) clear empty full size retrieve serve retrieve_and_serve append print Data members class Queue

7 Stack implementation - Array typedef double Queue_entry; enum Error_code {success,overflow,underflow}; const int max = 100; class Queue { public: Queue(); void clear(); bool empty() const; bool full() const; int size() const; Error_code retrieve(Queue_entry &) const; Error_code serve(); Error_code retrieve_and_serve(Queue_entry &); Error_code append(const Queue_entry &); void print() const; private: int count; Queue_entry entry[max]; };

8 Data Structure - Array 1 *... [0][1] [2] [max-1] One item: n items: n *****... [0][1] [2] [max-1] [n-1] [n] frontrear frontrear front is at position 0 rear is at position n-1

9 Create Queue Queue::Queue() // Pre:None. // Post:Initialize Queue to be empty. { } We use a constructor to initialize a new created queue as empty [0][1] [2] [max-1] [n-1] [n]countentry

10 Clear void Queue::clear() // Pre:None. // Post:All entries in the Queue have been //removed; it is now empty. { } 0... [0][1] [2] [max-1] [n-1] [n]countentry

11 Empty bool Queue::empty() const // Pre:None. // Post:Return true if the Queue is empty, //otherwise false is returned. { } 0... [0][1] [2] [max-1] [n-1] [n]countentry

12 Full bool Queue::full() const // Pre:None. // Post:Returns true if the Queue is full, //otherwise false is returned. { } max *******... [0][1] [2] [max-1] [n-1] [n]countentry

13 Size int Queue::size() const // Pre:None. // Post:Return the number of entries in the Queue. { } n *****... [0][1] [2] [max-1] [n-1] [n]countentry

14 entry Retrieve Error_code Queue::retrieve(Queue_entry &item) const // Pre:None. // Post:If the Queue is not empty, the front of the queue is recorded //as item. Otherwise an Error_code of underflow is return. { } n *****... [0][1] [2] [max-1] [n-1] [n]count Retrieve

15 Serve or Dequeue Error_code Queue::serve() // Pre:None. // Post:If the Queue is not empty, the front of the Queue is removed //Otherwise an Error_code of underflow is returned. { } n ABCDE... [0][1] [2] [max-1] [n-1] [n]count n-1 BCDE count entry

16 Retrieve and Serve Error_code Queue::retrieve_and_serve(Queue_entry &item) // Pre:None. // Post:If the Queue is not empty, the front of the queue is removed and //recorded as item. Otherwise an Error_code of underflow is return. { } n ABCDE... [0][1] [2] [max-1] [n-1] [n]count n-1 BCDE item = A entry

17 Append or Enqueue Error_code Queue::append(const Queue_entry &item) // Pre:None. // Post:If the Queue is not full, item is added at the end of the Queue. //If the Queue is full, an Error_code of overflow is returned //and the Queue is left unchanged. { } n *****... [0][1] [2] [max-1] [n-1] [n]count * n+1 entry

18 Print void Queue::print() const // Pre:None. // Post:Display the entries of the Queue. { }

19 Data Structure – Circular Array

20 frontrear frontrear Queue containing one item Empty Queue frontrear Queue with one empty position frontrear Full Queue

21 The class Queue – Circular Array class Queue { public: Queue(); void clear(); bool empty() const; bool full() const; int size() const; Error_code retrieve(Queue_entry &) const; Error_code serve(); Error_code retrieve_and_serve(Queue_entry &); Error_code append(const Queue_entry &); void print() const; private: int next(int n) const; int front, rear; Queue_entry entry[max]; };

22 Circular Queue =max-1

23 Create Queue Queue::Queue() { } =max-1 front rear=-1

24 Clear void Queue::clear() { } =max-1 front rear=-1 rear * ** *

25 Empty bool Queue::empty() const { } =max-1 front rear=-1 rear * ** * Not Empty Empty

26 Next position int Queue::next(int n) const { } =max-1 front rear * ** * * * * * * * * front = next(front); rear = next(rear); entry[rear] = ‘*’

=max-1 rear front ** * * * * * * * * * * Full bool Queue::full() const { } * * * * NOT FULL FULL

28 Size int Queue::size() const { } =max-1 front rear * ** * * rear – front + 1 max + * * * * * * * front rear

=max-1 * * * * * * * front rearRetrieve Error_code Queue::retrieve(Queue_entry &item) const { } Retrieve

30 Serve or Dequeue Error_code Queue::serve() { } =max-1 * * * * * * * front rear *

31 Retrieve and Serve Error_code Queue::retrieve_and_serve(Queue_entry &item) { } =max-1 * * * * * * * front rear *Retrieve

32 Append or Enqueue Error_code Queue::append(const Queue_entry &item) { } rear =max-1 * * * * * * * front * *

33 Print void Queue::print() const { }