Advertisement
Guest User

#als#lab1#kolejka_priorytetowa

a guest
Mar 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. typedef struct kolejka{
  9. int wartosc;
  10. int priorytet;
  11. struct kolejka *next;
  12. }LIFO;
  13.  
  14. kolejka *head;
  15.  
  16. void dodajElement(int value, kolejka *head, int priority) {
  17. kolejka *current;
  18. if (head->next == NULL) {
  19. current = (kolejka *) malloc(sizeof(struct kolejka));
  20. current->wartosc = value;
  21. current->next = NULL;
  22. current->priorytet = priority;
  23. head->next = current;
  24. } else {
  25. current = (kolejka *) malloc(sizeof(struct kolejka));
  26. current->priorytet = priority;
  27. current->wartosc = value;
  28.  
  29. if (head->next->priorytet < priority) {//dodawanie na poczatku
  30. current->next = head->next;
  31. head->next = current;
  32. } else {
  33. kolejka *temp;
  34. temp = head;
  35. while ( temp->next!=NULL && temp->next->priorytet >= priority ) temp = temp->next;
  36. current->next = temp->next;
  37. temp->next = current;
  38.  
  39. }
  40. }
  41. }
  42.  
  43.  
  44. void wykonanie(kolejka *head, int priority){
  45. kolejka *current;
  46. if(head->next==NULL);
  47. else{
  48. current=head;
  49. current=head->next;
  50. cout << current->wartosc;
  51. head->next = current->next;
  52.  
  53. }
  54. }
  55.  
  56. int main() {
  57.  
  58. head = (kolejka*)malloc(sizeof(struct kolejka));
  59. head->next=NULL;
  60. head->wartosc=-1;
  61. short int sterowanieNowy;
  62. int wartoscNowy, priority;
  63.  
  64. while(sterowanieNowy!=-1){
  65. cin >> sterowanieNowy;
  66. if(sterowanieNowy==1){
  67. wykonanie(head, priority);
  68. }
  69. if(sterowanieNowy==0){
  70. cin >> wartoscNowy;
  71. cin >> priority;
  72. dodajElement(wartoscNowy, head, priority);
  73. }
  74. }
  75.  
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement