473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

priority queue

Hi everyone .I am trying to make a priority queue .I have written the code,complier is not giving any error but I am not getting the right answer.
My program print only the last value that i have entered,and rest of the garbage value
Code is:

Expand|Select|Wrap|Line Numbers
  1. struct node *getnode(struct node *,struct node *,int,int);
  2. void display(struct node *,int n) ;
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #include<alloc.h>
  6. struct node
  7. {
  8. int data;
  9. int pri;
  10. struct node *link;
  11. };
  12.  
  13. void main()
  14. {
  15. struct node *front,*rear;
  16.  
  17. int a,n,pr,i;
  18. clrscr();
  19. front=NULL;
  20. rear=NULL;
  21. printf("\n\nEnter the no of the nodes you want to enter :");
  22. scanf("%d",&n);
  23. for(i=0;i<n;i++)
  24. {
  25. printf("\n\nEnter the %d node element & its priority :",i+1);
  26. scanf("%d%d",&a,&pr);
  27. front=getnode(front,rear,a,pr);
  28. }
  29. display(front,n);
  30. getch();
  31. }
  32.  
  33. struct node *getnode(struct node *front,struct node *rear,int a, int pr)
  34. {
  35. struct node *temp,*temp1;
  36. temp=(struct node*)malloc(sizeof(struct node));
  37. temp->data=a;
  38. temp->pri=pr;
  39. temp->link=NULL;
  40. if(rear==NULL)
  41. {
  42. rear=temp;
  43. front=rear;
  44. }
  45. else
  46. {
  47.  
  48.   if(front->pri<pr)
  49.   {
  50.     temp->link=front;
  51.     front=temp;
  52.  }
  53.   else
  54.  {
  55.     if(rear->pri>pr)
  56.     {
  57.     rear->link=temp;
  58.     rear=temp;
  59.     }
  60.     else
  61.     {
  62.     temp1=front;
  63.     while(temp->link->pri>=pr)
  64.     temp1=temp1->link;
  65.     temp->link=temp1->link;
  66.     temp1->link=temp;
  67.     }
  68.   }
  69. }
  70.     return(front);
  71. }
  72. void display(struct node *front,int n)
  73. {
  74. struct node *temp  ;
  75. int i;
  76. temp=front;
  77. printf("\n\nElements and its priority :\n\n");
  78. for(i=0;i<n;i++)
  79. {
  80. printf("\n\n[%d,%d]",temp->data,temp->pri);
  81. temp=temp->link;
  82. }
  83. }
Sep 28 '06 #1
1 6544
Banfa
9,065 Expert Mod 8TB
The problem lies in your definition of

struct node *getnode(struct node *,struct node *,int,int);


You pass front and rear as the first 2 parameters, these are of type struct node *.

You are passing the pointers by value, this means that you can not alter their values in the main function. If you want to be able to alster the values they have in the main function then you need to pass the pointers by reference.

i.e. struct node *getnode(struct node **,struct node **,int,int);
Sep 28 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
5
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
3
by: Erik | last post by:
Hi Everyone, I was thinking of how to do this for a while now and I cant get my head round it so I though I'd ask the experts! :-) What I am doing is reading in large amounts of data over TCP...
16
by: Crirus | last post by:
hello I read somewhere about priority queue...what is that and Vb net have such class? -- Ceers, Crirus ------------------------------ If work were a good thing, the boss would take it...
4
by: vfunc | last post by:
Is the STL priority queue a proper implementation of a heap with siftup algorithm etc ? How do you implement an STL priority queue (link to an example) ? Is there a resort method, why ? Thanks
3
by: Chris Lasher | last post by:
Hello, I am working with large graphs (~150,000 to 500,000 nodes) which I need decompose node-by-node, in order of a node's value. A node's value is determined by the sum of its edge weights....
3
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top (...
1
by: operatingsystem | last post by:
I need to generate such scheduler in operating system subject which satisfy below conditions...may i request u if anybody knows abt this plz help me out in this..topics.. Scheduler...
4
by: jjh5030 | last post by:
This is a programming assignment. You are asked to work with pointers. Be aware that error messages are often not very helpful when your pointers point to bad locations. Therefore, reserve...
14
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.