Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. typedef struct list
  7. {
  8. struct list *next;
  9. int value;
  10. int priority;
  11. }list;
  12. struct list *head;
  13.  
  14. int size(list *head)
  15. {
  16. int s=0;
  17. list *curr=head;
  18. while(curr!=NULL)
  19. {
  20. curr=curr->next;
  21. s++;
  22. }
  23. return s;
  24. }
  25.  
  26. int max(list *head)
  27. {
  28. list *curr, * pmax;
  29. int n;
  30. pmax = head;
  31. if(head)
  32. for(curr = head->next; curr; curr = curr->next)
  33. {
  34. if(curr->priority > pmax->priority)
  35. pmax = curr;
  36. }
  37. n=pmax->priority;
  38. return n;
  39. }
  40.  
  41. struct list *free(list *head)
  42. {
  43. list *curr=head;
  44. list *help=head;
  45. while(curr->next->priority!=max(head))
  46. {
  47. curr=curr->next;
  48. }
  49. help=curr;
  50. curr=curr->next;
  51. help->next=curr->next;
  52. delete curr;
  53. return head;
  54. }
  55.  
  56.  
  57. struct list *add(list *head,int value, int priority)
  58. {
  59. list *curr=head, *nowy_element;
  60. nowy_element = (list*) malloc(sizeof(list));
  61. if(size(head)==0)
  62. {
  63. head=nowy_element;
  64. nowy_element->value=value;
  65. nowy_element->priority=priority;
  66. nowy_element->next=NULL;
  67. return nowy_element;
  68. }
  69. while(curr->next!=NULL)
  70. {
  71. curr=curr->next;
  72. }
  73. nowy_element->next=NULL;
  74. curr->next=nowy_element;
  75. nowy_element->value=value;
  76. nowy_element->priority=priority;
  77. return head;
  78. }
  79.  
  80. void wypisywanie(list *head)
  81. {
  82. list *curr=head;
  83. while(curr->priority!=max(head))
  84. {
  85. curr=curr->next;
  86. }
  87. cout<<curr->value;
  88. }
  89.  
  90. int main ()
  91. {
  92. head=NULL;
  93. int wybor,wartosc,priorytet;
  94.  
  95. do
  96. {
  97. cin>>wybor;
  98.  
  99. if(wybor==0)
  100. {
  101. cin>>wartosc;
  102. cin>>priorytet;
  103. head=add(head,wartosc,priorytet);
  104. } else if(wybor==1)
  105. {
  106. if(size(head)!=0)
  107. {
  108. wypisywanie(head);
  109. head=free(head);
  110. }
  111. }
  112. } while (wybor!=-1);
  113.  
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement