1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
 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.
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.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
Jerry Lebowitz.  Stacks  Queues 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition.
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. 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,
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++
Data Structures Using C++
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
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.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
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)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
CSCE 210 Data Structures and Algorithms
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Chapter 7 Queues.
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Queues.
Queues Rem Collier Room A1.02
CENG 213 Data Structure Queue 7/2/2018.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Stacks Stack: restricted variant of list
Queues.
Chapter 16-2 Linked Structures
Queues.
Chapter 19: Stacks and Queues.
DATA STRUCTURE QUEUE.
CSC 143 Queues [Chapter 7].
Pointers & Dynamic Data Structures
Queues.
Queues Definition of a Queue Examples of Queues
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

1 Data Structures and Algorithms Queue

2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues An Application of Queues

3 1. introduction to the Queue Data Structure  A simple data container consisting of a linear list of elements  Access is by position (order of insertion)  Insertions at one end (rear)(back), deletions at another end (front).  You cannot add/extract entry in the middle of the queue.  A queue is open at two ends.  First In First Out (FIFO) structure  Two basic operations: enqueue: add to rear dequeue: remove from front

4 An Illustration

5

6 Simulation of waiting lines Simulation of serviceable events Job scheduling Input/Output Buffering Some Queue Applications

7 Array Implementation of Queue

8

9

10 Array Implementation of Queue oWhen last array element is reached, we move back to start oThe queue is viewed as a circular array oTo enqueue: rear = (rear + 1) % size oTo dequeue: front = (front + 1) % size oBoth rear and front advance clockwise

11 Array Implementation of Queue

12 Array Implementation of Queue

13 Array Implementation of Queue construct: construct an empty queue queueIsEmpty  bool : return True if queue is empty queueIsFull  bool : return True if queue is full enqueue(el) : add element (el) at the rear dequeue(el): retrieve and remove the front element queueFront(el): retrieve front without removing it queueLength  int : return the current queue length

14 The queue may be implemented as a dynamic array. The capacity (MaxSize) will be input as a parameter to the constructor (default is 128) The queue ADT will be implemented as a template class to allow for different element types. Array Implementation of Queue

15 // File: Queuet.h // Queue template class definition // Dynamic array implementation #ifndef QUEUET_H #define QUEUET_H template class Queuet { public: Queuet (int s = 128);// Constructor ~Queuet ();// Destructor Array Implementation of Queue

16 // Member Functions void enqueue(Type );// Add to rear void dequeue();// Remove from front Type queueFront() const;// Retrieve front bool queueIsEmpty() const;// Test for Empty queue bool queueIsFull() const;// Test for Full queue int queueLength() const;// Queue Length private: Type *queue;// pointer to dynamic array int front, rear, count, MaxSize; }; #endif // QUEUET_H #include "Queuet.cpp" Array Implementation of Queue

17 Array Implementation of Queue //Queue.cpp #include Using names pace std; #include " QUEUET_H.h“ Queuet: : Queuet (int s) : Front(0), rear(0), count(0) { MaxSize=s ; queue=new int[s] ; } ~ Queuet() {delete [] queue ;} //queueIsEmpty() bool Queue: :queueIsEmpty() const { return (count==0);// if(front == rear) }

18 Array Implementation of Queue // enqueue() void Queuet::enqueue( Type alue) { Type newBack = (rear+ 1) % MaxSize;// circular if (newBack != front) // queue Isn't full { queue[rear] = value; rear = newBack; count++;} } // Type queueFront() Type Queuet:: queueFront() const { if ( ! queueIsEmpty() ) return (queue[front]); } //dequeue void Queuet::dequeue() { i f ( ! queueIsEmpty() ) front = (front + 1) % MaxSize; count--;}

19 Array Implementation of Queue // queueIsfull() bool Queuet:: queueIsfull ( ) const { return (count==MaxSize);} // queueLength() const; Int Queuet:: queueLength() const { return count; }

20 A Queue can be implemented as a linked structure. Requires more space than array implementations, but more flexible in size. Two pointers are needed: front for dequeue and rear for enqueue 3. Linked Queues

21 Node Specification // The linked structure for a node can be // specified as a Class in the private part of // the main queue class. class node// Hidden from user { public: Type e;// stack element node *next;// pointer to next node }; // end of class node declaration typedef node * NodePointer; NodePointer front, rear;// pointers

22 Enqueue Operation enqueue(v): NodePointer pnew = new node; pnew->e = v; pnew->next = NULL; rear->next = pnew; rear = pnew; rear New pnew 1 2 front

23 Dequeue Operation 2 3 cursor front dequeue(v): v = front->e; cursor = front; front = front->next; delete cursor; 1 rear

24 // File: QueueL.h // Linked List Queue class definition #ifndef QUEUEL_H #define QUEUEL_H template class QueueL { public: QueueL(); // Constructor ~QueueL(); // Destructor void enqueue(Type ); // Add to rear Linked Queue Class

25 void dequeue(); // Remove from front void queueFront() const;// retrieve front bool queueIsEmpty() const;// Test for Empty queue int queueLength() const;// Queue Length private: // Node Class class node { public: Type e;// queue element node *next;// pointer to next node }; // end of class node declaration Linked Queue Class

26 typedef node * NodePointer; NodePointer front, rear;// Pointers int count;// length }; #endif // QUEUEL_H #include "QueueL.cpp" Linked Queue Class

27 Part of Implementation file template void QueueL :: enqueue (Type v) { NodePointer pnew = new node; pnew->e = v; pnew->next = NULL; if(queueIsEmpty()) { front = pnew; rear = pnew;} else {rear->next = pnew; rear = pnew;} count++; }

28 Part of Implementation file template void QueueL :: dequeue () { NodePointer cursor; if(queueIsEmpty()) cout<<” Queue is empty “<<endl; else { cursor = front; front =front->next; delete cursor; count--; }}

29 Part of Implementation file template void QueueL :: queueFront () const { NodePointer cursor; if(queueIsEmpty()) cout << "Queue is Empty" << endl; else {return front->e; } }

Implement Queue using ADT of a circular linked List Exercise