Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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-infirst-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 { cout<<"Insert the cin>>item; insert(item); } clrscr( ); for(int j = 0 ; j { remove(item); } cout<<" The index cout<<" The index } < size ; i++) value of the item : "; < size ; j ++) at the rear is: "<<rer( )<<endl; 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( ){ front = 0 ; rear = -1 ; count = 0 ; } 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 assert(rear != size); q[++rear] = item; count++; } item){ 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( ){ assert( !isEmpty( ) ) ; 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; for (i=0;i<s;i++){ 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.