Algorithms and Data Structures

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
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.
Queue Overview Queue ADT Basic operations of queue
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
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.
CHP-4 QUEUE.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
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.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Review Array Array Elements Accessing array elements
What is a Queue? Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends. In a queue data structure,
CSCE 210 Data Structures and Algorithms
Chapter 7 Queues.
Data Abstraction & Problem Solving with C++
Data Structures Using C, 2e
Queues.
CS505 Data Structures and Algorithms
UNIT II Queue.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
More on Linked Lists, Stacks, and Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
CC 215 Data Structures Queue ADT
Queues.
Queues Rem Collier Room A1.02
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Queues Mohammad Asad Abbasi Lecture 5
Algorithms and Data Structures
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
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.
DATA STRUCTURE QUEUE.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
CSC 143 Queues [Chapter 7].
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
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.
Abstract Data Type Abstract Data Type as a design tool
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues What is a Queue? Queue Implementations: As Array
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CSCS-200 Data Structure and Algorithms
Queues.
Chapter 4 Stacks and Queues
Data Structures & Programming
Presentation transcript:

Algorithms and Data Structures AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Course No.: 0511363 Fall 2014 Queues

Queues Queue: is a linear data structure in which the elements are added at one end, called the back or rear, and deleted from the other end, called the front Example: line of customers in a bank A queue of documents is often waiting to be printed at a printer. The only difference between stack and queue is that in case of stack insertion and removable (PUSH and POP) operations are at one end (TOP) only, but in case of a queue Insertion and Deletion take place at two ends called REAR and FRONT of the queue respectively.

Queues A queue is a container that implements that the first-in-first-out (FIFO) protocol. This means that the data in the queue is processed in the same order as it was entered. The following figure represents a queue structure: Queue operations: Insert (Item) Remove ()

Representation of queues Queue may be represented in the memory in various ways. Mainly there are two ways: Using one dimensional array Using single linked list To represent queues as array we need: An array to store the queue elements Two variables to keep track of the first and last elements of the queue Variable to specify the maximum size of the queue.

Array representation of queues # include<iostream.h> #include<conio.h> #include<assert.h> int front = 0 , rear = -1; const int size = 7; char q[size]; char item; insert(char item) { assert(rear != size); q[++rear] = item; } char remove(char item) assert(rear >= front ); item = q[front++]; return item;

Array representation of queues…cont. main( ) { for(int i = 0 ; i < size ; i++) cout<<"Insert an item : "; cin>>item; insert(item); } clrscr( ); cout<<" items removed are: " ; for(int j = 0 ; j < size ; j ++) cout << remove(item);

Example: Queue to store Array of characters # include<iostream.h> #include<string.h> #include<conio.h> #include<assert.h> int front = 0 , rear = -1; const int size = 7, n = 7; char q[size][n]; char item[n]; insert(char item[]) { assert(rear != size); strcpy(q[++rear],item); } remove(char item[]) assert(rear >= front ); strcpy(item,q[front++]); cout<<"Deleted item is : "<<item<<endl;

Example ..cont. int frnt( ) { return front ;} int rer( ) { return rear ;} main( ) { for(int i = 0 ; i < size ; i++) cout<<"Insert the value of the item : "; cin>>item; insert(item); } clrscr( ); for(int j = 0 ; j < size ; j ++) remove(item); cout<<" The index at the rear is: "<<rer( )<<endl; cout<<" The index at the front is: "<<frnt( )<<endl;

Abstract Data Type (Queue) # include<iostream.h> #include<conio.h> #include<assert.h> class queue{ int front; int rear; int count; unsigned size ; char* q; public: queue (unsigned s) ; // constructor unsigned Get_s( ) ; // returns size of the queue void insert(char item) ; // inserts given item at the rear char remove() ; // remove an item from the front int Get_C( ) ; // returns the number of elements in the queue char Get_frnt( ) ; // returns the element in the front char Get_rer( ) ; // returns the element of the rear void initialize( ) ; // initialize front, rear and count int isEmpty( ) ; // check if queue is empty int isFull( ) ; // check if queue is full void Destroy( ) ; // Destroy all elements in the queue };

General program..Cont. void queue:: initialize( ){ front = 0 ; rear = -1 ; count = 0 ; } int queue::isEmpty( ){ return ( count == 0 ) ; int queue::isFull( ){ return ( count == size ) ; void queue::Destroy( ){

General program..Cont. queue::queue (unsigned s){ if(s <= 0){ cout<< " The size of the array should be positive "<<endl ; cout<< " By default it can be S. "<<endl ; size =10 ;} else size = s ; q = new char [size] ; assert (q ) ; } unsigned queue::Get_s( ){ return size; void queue::insert(char item){ assert(rear != size); q[++rear] = item; count++;

General program..Cont. char queue::remove(){ assert(rear >= front ); --count; return q[front++]; } int queue::Get_C( ) { //return number of item in queu return count; char queue::Get_frnt( ){ assert( !isEmpty( ) ) ; return q[front] ; char queue::Get_rer( ){ return q[rear] ;

Main function main(){ int i, s; char it; cout<<"Enter the size of queue "; cin>>s; queue f(s); f. initialize( ); for (i=0;i<s;i++){ cout<<"Enter the element"<<(i+1)<<"\t"; cin>>it; f.insert(it); } cout<<"get front item "<<f.Get_frnt( )<<endl; cout<< "the number of item in queue is "<<f.Get_C( )<<endl; cout<<"remove item number "<< (i+1)<<"\t" <<f.remove( )<<endl;

Linked list representation of queues Queues can be implemented using single linked-list (SLL): The operation of adding an item to queue is similar to add a node to SLL The operation of deleting an item from queue is similar to delete a node from SLL.

Linked list representation of queues

Cont.

Cont.

Cont.

Various queue structures Deque (Double Ended Queue) In this type both the insertion and deletion will be from both ends This structure is between Queue and Stack To implement this type there are two ways: 1- Using double linked list 2- Circular array

Priority Queue Priority Queue : is a data structure where element removed according to there priority and there current queue position An element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue Priority Queue Implementation: Array Double Linked List

Application of Queues Simulation: design computer models to study the behavior of real systems. Multiprogramming Schedule: Examples include CPU scheduling (Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur), Disk Scheduling.