New Member FAQ | Forums | Earn Revenue


Resources Entrance Ask Experts Exam Papers Jobs English Projects Universities Colleges Courses Schools Training My India



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



Resources » Articles/Knowledge Sharing » Education »

C program for priority queue


Posted Date: 23 Oct 2009    Resource Type: Articles/Knowledge Sharing    Category: Education
Author: AkhilaMember Level: Silver    
Rating: 3 out of 53 out of 53 out of 5Points: 2



Programs:

#include
#define HEAPSIZE 50
#define MAX 5
#define SHOWPASS
struct heap
{
int a[HEAPSIZE];
int index;
};
typedef struct heap heap;
void print(heap *p)
{
int i;
for(i=0;i<=p->index;i++)
printf("%d\t",p->a[i]);
}
void insertpq(heap *p,int val)
{
int i;
if(p->index==HEAPSIZE-1)
printf("\nHeap is full");
else
{
for(i=++p->index;i>0 && p->a[(i-1)/2]>val;i=(i-1)/2)
p->a[i]=p->a[(i-1)/2];
p->a[i]=val;
}
}
int removeq(heap *p)
{
int value,temp,i;
if(p->index==-1)
{
printf("\nHeap is empty");
return -1;
}
else
{
value=p->a[0];
temp=p->a[p->index--];
for(i=1;i<=p->index;i=2*i+1)
{
if(((i+1)<=p->index) && (p->a[i]>p->a[i+1]))
i++;
if(p->a[i]p->a[(i-1)/2]=p->a[i];
else
break;
}
p->a[(i-1)/2]=temp;
return value;
}
}
int main()
{
int arr[MAX];
int i,n;
heap s;
s.index=-1;
clrscr();
printf("\nEnter total elements(n< %d):",MAX);
scanf("%d",&n);
printf("\nEnter %d elements:",n);
for(i=0;i{
scanf("%d",&arr[i]);
insertpq(&s,arr[i]);
#ifdef SHOWPASS
printf("\n HEAP+%d:",arr[i]);
print(&s);
#endif
}
printf("\n");
i=0;
while(s.index!=-1)
{
arr[i]=removeq(&s);
#ifdef SHOWPASS
printf("\nHEAP-%d:",arr[i]);
print(&s);
#endif
i++;
}
printf("\n Sorted:");
printf("\n");
for(i=0;iprintf("%d\t",arr[i]);
printf("\n");
return 0;
}

Output:

Enter total elements(n<5):5

Enter 5 elements:33

HEAP +33: 33 44

HEAP +44: 33 44 22

HEAP +22: 22 44 33 77

HEAP +77: 22 44 33 77 11

HEAP +11: 11 22 33 77 44

HEAP -11: 22 44 33 77

HEAP -22: 33 44 77

HEAP -33: 44 77

HEAP -44: 77

HEAP -77:

Sorted:
11 22 33 44 77




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Program for priority queue using c  .  Priority queue using heaps  .  C program for priority queue  .  Application of heaps  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Tamilnadu state board 12th board exam blueprint
Previous Resource: Chemist And Chemical Engineering - A PROCESS CALCULATION
Return to Discussion Resource Index
Post New Resource
Category: Education


Post resources and earn money!
 
More Resources



Advertise Here





Contact Us   Advertise   Editors    Privacy Policy    Terms Of Use   

ISC Technologies.
2006 - 2009 All Rights Reserved.