Advertisement
100rads

cirkularni stog

Dec 4th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 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.     Position next;
  11. }Cvor;
  12.  
  13. int Push(Position, int);
  14. int Pop(Position);
  15. int DodajNaPocetak(Position, Position);
  16. Position GenerirajClan(int);
  17. void Ispis(Position);
  18. int VelicinaNiza(Position);
  19. void IzbrisiZadnjeg(Position);
  20.  
  21. int main()
  22. {
  23.     Cvor stog;
  24.     int maxElement;
  25.     int unos = 0;
  26.     srand((unsigned)time(NULL));
  27.  
  28.     stog.next = NULL;
  29.  
  30.     printf("Unesite maksimalni broj elemenata cirkularnog stoga:");
  31.     scanf(" %d", &maxElement);
  32.  
  33.     while (unos != 4)
  34.     {
  35.         printf("Unesite 1 za push, 2 za pop, 3 za ispis liste, 4 za prekid: ");
  36.         scanf(" %d", &unos);
  37.  
  38.         if (unos == 1)
  39.             Push(&stog, maxElement);
  40.         else if (unos == 2)
  41.             Pop(&stog);
  42.         else if (unos == 3)
  43.             Ispis(stog.next);
  44.         else if (unos != 4)
  45.             printf("Pogresan unos!\n");
  46.     }
  47.  
  48.     return 0;
  49. }
  50.  
  51. int Push(Position head, int max)
  52. {
  53.     int temp;
  54.     temp = rand() % (100 - 10 + 1) + 10;
  55.     if(VelicinaNiza(head) < max)
  56.         {
  57.             DodajNaPocetak(head, GenerirajClan(temp));
  58.         }
  59.     else
  60.         {
  61.             DodajNaPocetak(head, GenerirajClan(temp));
  62.             IzbrisiZadnjeg(head);
  63.         }
  64.     return 0;
  65. }
  66.  
  67. int Pop(Position head)
  68. {
  69.     head->next = head->next->next;
  70.     return 0;
  71. }
  72.  
  73. int DodajNaPocetak(Position head, Position novi)
  74. {
  75.     novi->next = head->next;
  76.     head->next = novi;
  77.  
  78.     return 0;
  79. }
  80.  
  81. Position GenerirajClan(int broj)
  82. {
  83.     Position noviCvor;
  84.     noviCvor = (Position)malloc(sizeof(Cvor));
  85.     noviCvor->broj = broj;
  86.     noviCvor->next = NULL;
  87.  
  88.     return noviCvor;
  89. }
  90.  
  91. void Ispis(Position position)
  92. {
  93.     while (position != NULL)
  94.     {
  95.         printf("%d ", position->broj);
  96.         position = position->next;
  97.     }
  98.     printf("\n");
  99. }
  100.  
  101. int VelicinaNiza(Position head)
  102. {
  103.     int counter = 0;
  104.     while (head->next != NULL)
  105.     {
  106.         counter++;
  107.         head = head->next;
  108.     }
  109.     return counter;
  110. }
  111.  
  112. void IzbrisiZadnjeg(Position head)
  113. {
  114.     while (head->next->next != NULL)
  115.     {
  116.         head = head->next;
  117.     }
  118.     head->next = NULL;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement