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.
|
Creation Of Minqueue In C++
Posted Date:
Total Responses: 0
Posted By: mohit kukreja Member Level: Silver Points/Cash: 5
|
// by mohit #include #include #include
class heapsort { public: int arr[100],smallest,left,right,n,saver; void build_heap(int j); void min_heap(int i); void compare(int i); void extract(); void increase(int node,int value); void new_insert(int val); void decrease(int node,int val); void deleter(int node); };
void heapsort :: build_heap(int j) { for(int i=0;i<=j;i++) { cout<<"Enter Job "<"; cin>>arr[i]; } saver=n; }
void heapsort :: min_heap(int i) {
i=i/2;
while(i>0) { compare(i); i--; } }
void heapsort :: compare(int i) { left=2*i; right=left+1; if(left<=n && arr[left-1] < arr[i-1]) smallest=left; else smallest=i; if(right<=n && arr[right-1] < arr[smallest-1]) smallest=right; if(arr[smallest-1] { int temp=arr[smallest-1]; arr[smallest-1]=arr[i-1]; arr[i-1]=temp; } if(smallest!=i) compare(smallest); }
void heapsort :: extract() { if(n>0) { min_heap(saver); cout<<"\nJob with priority "< <<" has been extracted"; arr[0]=arr[saver-1]; n--; saver--; } }
void heapsort :: decrease(int node,int value) { min_heap(n); if(arr[node-1] < value) cout<<"\nNode allready has lower priority" <<" so it can not be decrease"; else { arr[node-1]=value; while(arr[(node)/2-1]>arr[node-1] && node/2 >0) { int temp=arr[(node)/2-1]; arr[(node)/2-1]=arr[node-1]; arr[node-1]=temp; node=node/2; } } }
void heapsort :: new_insert(int val) { n++; saver++; arr[saver-1]=32765; decrease(saver,val); }
void heapsort :: increase(int node,int val) { if(arr[node-1]>val) cout<<"\nNo need to increase"; else { arr[node-1]=val; min_heap(node); } }
void heapsort :: deleter(int node) { if(node>saver) cout<<"\nNode does not exist"; else { cout<<"node with priority "< arr[node-1]=arr[saver-1]; arr[n-1]=0; n--; saver--; min_heap(node); } }
void main() { clrscr(); int swtch; heapsort heap; heap.n=heap.saver=0; for(int m=0;m<100;m++) heap.arr[m]=0;
cout<<"Enter the Number of Jobs:->"; cin>>heap.n; heap.build_heap(heap.n-1); while(1) { clrscr(); cout<<"\n1.Find the job with minimum priority" <<"\n2.Extract the job with minimum priority" <<"\n3.Insert a new job" <<"\n4.Increase priority of any existing job" <<"\n5.Decrease priority of any existing job" <<"\n6.Delete a job" <<"\n7.Display after min heapyfying" <<"\n8.Exit" <<"\nEnter Choice:->"; cin>>swtch; switch(swtch) {
case 1: { heap.min_heap(heap.saver); if(heap.saver>0) cout<<"\nJob has minimum priority of " < cout<<"\n"; break; }
case 2: { heap.extract(); cout<<"\n"; break; }
case 3: { int temp; cout<<"Enter priority of new job :->"; cin>>temp; heap.new_insert(temp); break; }
case 4: { int temp,i; cout<<"Enter job no:->"; cin>>temp; cout<<"Enter priority of the job :->"; cin>>i; heap.increase(temp,i); break;}
case 5: { int temp,i; cout<<"Enter job no:->"; cin>>temp; cout<<"Enter priority of the job :->"; cin>>i; heap.decrease(temp,i); break; }
case 6: { int temp; cout<<"Enter job no:->"; cin>>temp; heap.deleter(temp); break; }
case 7: { heap.min_heap(heap.saver); for(int i=0;i cout<<"\nPriority of job :->"< break; }
case 8: exit(0); }
cout<<"\n";
getch(); } }
|
Project Feedbacks
|
No feedbacks found. Be the first to respond and make money from revenue sharing program.
|
|
|
| Post Feedback |
|
|
You must Sign In to post a feedback.
|
|
|
|
|
|