By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

priority queue

P: 5
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
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
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

Post your reply

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