Friday 11 October 2013

Priority queue IN array

 a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, 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.(wiki/Priority_queue).

For better understanding . we provide you power point presentation.
PPT. presentation for queue

concept :-

 make a globle array with all data = -1   
 each row represent priority i.e 1st row contain element with priority 1  
 & empty node have volue = -1
 




priority queue representation in array   C code :

 // NOTE:::::::::::::::::::::::::::::::::::::::::::::::::::::::  
 /*  
 make a globle array with all data = -1   
 each row represent priority i.e 1st row contain element with priority 1  
 so we can take only priority in between 1 to 10  
 & max element in with 0ne priority =10   
 NOTE :: for this program only  
 */  
 // NOTE::::::::::::::::::::::::::::::::::::::::::::::::::::::;  
 #include<stdio.h>  
 int a[10][10],f[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},r[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};  
 void insert(int e,int p)  
 { if(f[p]==-1)  
  {f[p]=r[p]=0;  
   a[p][0]=e;  
  }  
  else{  
       r[p]++;  
       a[p][r[p]]=e;  
  }  
 }  
 void display()  
 { int i,j;  
  for(i=0;i<10;i++)  
  {if(f[i]!=-1)  
   { printf("\n %d :",i);  
   for(j=f[i];j<=r[i];j++)  
    printf(" %d ",a[i][j]);  
    }  
  }   
 }  
 void main()  
 { int n,i,e,p;  
  printf("\n Enter number of values to insert ");  
  scanf("%d",&n);  
  for(i=0;i<n;i++)  
  { printf("\n Enter priority and number to insert ");  
   scanf("%d%d",&p,&e);  
  insert(e,p);  
  display();  
 }}  

OUTPUT:-

No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets