Download presentation
Presentation is loading. Please wait.
1
Algorithms and Data Structures
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Course No.: Fall 2014 Queues
2
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.
3
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 ()
4
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.
5
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;
6
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);
7
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;
8
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;
9
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 };
10
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( ){
11
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++;
12
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] ;
13
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;
14
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.
15
Linked list representation of queues
16
Cont.
17
Cont.
18
Cont.
19
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
20
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
21
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.