Computer Science Department Data Structures and Algorithms Queues Lecture 5.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
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.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
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.
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.
CHAPTER 7 Queues.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Fundamentals of Python: From First Programs Through Data Structures
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.”
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
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.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
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.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
Lecture 9 Feb 26 Announcement/discussion: mid-term # 1 (March 10?) Goals: Queue – implementation using array Application to BFS (breadth-first search)
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.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
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,
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
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.
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.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
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.
Review Array Array Elements Accessing array elements
Queues Chapter 4.
CC 215 Data Structures Queue ADT
Stacks and Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
CSCE 3110 Data Structures & Algorithm Analysis
Stacks Stack: restricted variant of list
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Queues.
Queues FIFO Enqueue Dequeue Peek.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CSI 1340 Introduction to Computer Science II
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Presentation transcript:

Computer Science Department Data Structures and Algorithms Queues Lecture 5

Computer Science Department Queues A Queue is a special kind of list, where items are inserted at one end (the rear) And deleted at the other end (the front). Accessing the elements of queues follows a First In,First Out (FIFO) order. Example –Like customers standing in a check-out line in a store, the first customer in is the first customer served.

Computer Science Department Common Operations on Queues MAKENULL: FRONT(Q): Returns the first element on Queue Q. ENQUEUE(x,Q): Inserts element x at the end of Queue Q. DEQUEUE(Q): Deletes the first element of Q. ISEMPTY(Q): Returns true if and only if Q is an empty queue. ISFULL(Q): Returns true if and only if Q is full.

Computer Science Department Enqueue and Dequeue Primary queue operations: Enqueue and Dequeue Enqueue – insert an element at the rear of the queue. Dequeue – remove an element from the front of the queue.

Computer Science Department Queues Implementations Static –Queue is implemented by an array, and size of queue remains fix Dynamic –A queue can be implemented as a linked list, and expand or shrink with each enqueue or dequeue operation.

Computer Science Department Static Implementation of Queues

Computer Science Department Dynamic Implementation of Queues Dynamic implementation is done using pointers. –FRONT: A pointer to the first element of the queue. –REAR: A pointer to the last element of the queue. xy. z Front Rear

Computer Science Department Dynamic Implemenatation Enqueue (X) Enqueue (Y) Q.front Q.Rear. x Q.front Q.Rear x. y

Computer Science Department Dynamic Implementation Dequeue MakeNULL Q.front Q.Rear. y Q.front Q.Rear NULL

Computer Science Department Dynamic implementation of Queue class DynQueue{ private: struct queueNode { int num; queueNode *next; }; queueNode *front; queueNode *rear; public: DynQueue(); ~DynQueue(); void enqueue(); void dequeue(); bool isEmpty(); void displayQueue(); void makeNull(); };

Computer Science Department Constructor DynQueue::DynQueue() { front = NULL; rear = NULL; }

Computer Science Department Enqueue( ) Function void DynQueue::enqueue() { queueNode *ptr; ptr = new queueNode; cout<<"Enter Data"; cin>>ptr->num; ptr->next= NULL; if (front == NULL) { front = ptr; rear = front; } else{ rear->next=ptr; rear = ptr; }

Computer Science Department Dequeue( ) Function void DynQueue::dequeue() { queueNode *temp; temp = front; if(isEmpty()) cout<<"Queue is Empty"; else { cout num; front = front->next; delete temp; }

Computer Science Department Static Implementation of Queue Static implementation is done using arrays In this implementation, we should know the exact number of elements to be stored in the queue. When enqueuing, the front index is always fixed and the rear index moves forward in the array.

Computer Science Department Static Implementation of Queue When dequeuing, the front index is fixed, and the element at the front of the queue is removed. Move all the elements after it by one position. (Inefficient!!!)

Computer Science Department Static Implementation of Queue A better way –When an item is enqueued, the rear index moves forward. –When an item is dequeued, the front index also moves forward by one element Example: X = occupied, and O = empty (front) XXXXOOOOO (rear) OXXXXOOOO (after 1 dequeue, and 1 enqueue) OOXXXXXOO (after another dequeue, and 2 enqueues) OOOOXXXXX (after 2 more dequeues, and 2 enqueues) The problem here is that the rear index cannot move beyond the last element in the array.

Computer Science Department Static Implementation of Queue To overcome the above limitation, we can use circular array implementation of queues. In this implementation, first position follows the last. When an element moves past the end of a circular array, it wraps around to the beginning, e.g – OOOOO7963 ->4OOOO7963 (after Enqueue(4)) –After Enqueue(4), the rear index moves from 3 to 4. How to detect an empty or full queue, using a circular array algorithm? –Use a counter of the number of elements in the queue.

Computer Science Department Circular Queue 18 Q.rearQ.frontQ.rearQ.frontQ.rear a b d c i e h g f i A Completely Filled Queue A Queue with Only 1 Element

Computer Science Department Circular Queue Implementation class CirQueue { private: int queue[5]; int rear; int front; int maxSize; int counter; public: CirQueue(); void enqueue(); void dequeue(); bool isEmpty(); bool isFull(); void display(); };

Computer Science Department Constructor CirQueue::CirQueue() { front = 0; rear = -1; maxSize = 5; counter =0; }

Computer Science Department Enqueue( ) Function void CirQueue::enqueue() { if ( isFull()) cout<<"queue is full"; else { rear = (rear + 1) % maxSize; cout<<"Enter Data="; cin>> queue[rear]; counter ++; }

Computer Science Department Dequeue( ) Function void CirQueue::dequeue() { if ( isEmpty()) cout<<"Queue is empty"; else { cout<< "Element deleted="<<queue[front]; front = (front +1)% maxSize; counter --; }

Computer Science Department Display( ) Function void CirQueue::display() { if(isEmpty()) cout<<"Queue is empty"; else { for (int i=0; i<counter; i++) cout<< queue[(front+ i)% maxSize]<<endl;; } }

Computer Science Department isEmpty( ) and isFull( ) bool CirQueue::isEmpty() { if (counter == 0) return true; else return false; } bool CirQueue::isFull() { if (counter < maxSize) return false; else return true; }

Computer Science Department Priority Queues

Computer Science Department Introduction Stack and Queue are data structures whose elements are ordered based on a sequence in which they have been inserted E.g. pop() function removes the item pushed last in the stack Intrinsic order among the elements themselves (e.g. numeric or alphabetic order etc.) is ignored in a stack or a queue

Computer Science Department Definition A priority queue is a data structure in which prioritized insertion and deletion operations on elements can be performed according to their priority values. There are two types of priority queues: –Ascending Priority queue, and a –Descending Priority queue

Computer Science Department Types of Priority Queue Ascending Priority queue: a collection of items into which items can be inserted randomly but only the smallest item can be removed If “A-Priority-Q” is an ascending priority queue then –Enqueue() will insert item ‘x’ into A-Priority-Q, –minDequeue() will remove the minimum item from A-Priority-Q and return its value

Computer Science Department Descending Priority queue: a collection of items into which items can be inserted randomly but only the largest item can be removed If “D-Priority-Q” is a descending priority queue then –Enqueue() will insert item x into D-Priority-Q, –maxDequeue( ) will remove the maximum item from D-Priority-Q and return its value Types of Priority Queue

Computer Science Department In both the above types, if elements with equal priority are present, the FIFO technique is applied. Both types of priority queues are similar in a way that both of them remove and return the element with the highest “Priority” when the function remove() is called. –For an ascending priority queue item with smallest value has maximum “priority” –For a descending priority queue item with highest value has maximum “priority” This implies that we must have criteria for a priority queue to determine the Priority of its constituent elements. the elements of a priority queue can be numbers, characters or any complex structures such as phone book entries, events in a simulation Generally

Computer Science Department In what manner should the items be inserted in a priority queue –Ordered (so that retrieval is simple, but insertion will become complex) –Arbitrary (insertion is simple but retrieval will require elaborate search mechanism) Retrieval –In case of un-ordered priority queue, what if minimum number is to be removed from an ascending queue of n elements (n number of comparisons) In what manner should the queue be maintained when an item is removed from it –Emptied location is kept blank (how to recognize a blank location ??) –Remaining items are shifted Priority Queue Issues