Advertisement
100rads

red s prioritetom (nedovršeno)

Dec 4th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. typedef struct _cvor* Position;
  7. typedef struct _cvor
  8. {
  9.     int broj;
  10.     int prioritet;
  11.     Position next;
  12. }Cvor;
  13.  
  14. int Push(Position);
  15. int Pop(Position);
  16. void Ispis(Position);
  17. Position GenerirajClan(int, int);
  18. int DodajNaPocetak(Position, Position);
  19. int DodajSortiranoPoPrioritetu(Position, Position);
  20.  
  21. int main()
  22. {
  23.     Cvor niz;
  24.     int unos = 0;
  25.     srand((unsigned)time(NULL));
  26.  
  27.     niz.next = NULL;
  28.  
  29.     while (unos != 4)
  30.     {
  31.         printf("Unesite 1 za push, 2 za pop, 3 za ispis liste, 4 za prekid: ");
  32.         scanf(" %d", &unos);
  33.  
  34.         if (unos == 1)
  35.             Push(&niz);
  36.         else if (unos == 2)
  37.             Pop(&niz);
  38.         else if (unos == 3)
  39.             Ispis(niz.next);
  40.         else if (unos != 4)
  41.             printf("Pogresan unos!\n");
  42.     }
  43.  
  44.     return 0;
  45. }
  46.  
  47. int Push(Position head)
  48. {
  49.     int temp, tempPrioritet;
  50.     temp = rand() % (100 - 10 + 1) + 10;
  51.     tempPrioritet = rand() % (5 - 1 + 1) + 1;
  52.     DodajSortiranoPoPrioritetu(head, GenerirajClan(temp, tempPrioritet));
  53.     return 0;
  54. }
  55.  
  56. int Pop(Position head)
  57. {
  58.     head->next = head->next->next;
  59.     return 0;
  60. }
  61.  
  62. Position GenerirajClan(int broj, int prioritet)
  63. {
  64.     Position noviCvor;
  65.     noviCvor = (Position)malloc(sizeof(Cvor));
  66.     noviCvor->broj = broj;
  67.     noviCvor->prioritet = prioritet;
  68.     noviCvor->next = NULL;
  69.  
  70.     return noviCvor;
  71. }
  72.  
  73. int DodajSortiranoPoPrioritetu(Position head, Position novi)
  74. {
  75.     while (head->next != NULL && head->prioritet < head->next->prioritet)
  76.         head = head->next;
  77.  
  78.     novi->next = head->next;
  79.     head->next = novi;
  80.     return 0;
  81. }
  82.  
  83. void Ispis(Position position)
  84. {
  85.     while (position->next != NULL)
  86.     {
  87.         printf("Broj: %d ", position->broj);
  88.         printf("Prioritet: %d \n", position->prioritet);
  89.         position = position->next;
  90.     }
  91.     printf("\n");
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement