Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #include<ctype.h>
  5.  
  6. struct _Cvor;
  7. typedef struct _Cvor * Position;
  8.  
  9. struct _Cvor
  10. {
  11.     int Element;
  12.     Position Next;
  13. };
  14.  
  15. int GetValueFromRange(int, int);
  16. int Push(Position, int, int);
  17. int Pop(Position, int);
  18. void PrintList(Position, int);
  19.  
  20. void main(void)
  21. {
  22.     struct _Cvor S;
  23.     char izbor = 0;
  24.     int n = 0;
  25.     int max = 0;
  26.  
  27.     S.Next = NULL;
  28.     S.Element = -1;
  29.  
  30.     while(max < 5)
  31.     {
  32.         printf("\r\nUnesite velicinu stoga <5-20> : ");
  33.         scanf_s(" %d", &max);
  34.         if(max < 5 || max > 20)
  35.         {
  36.             max = 0;
  37.             printf("\r\nPogresan unos!");
  38.         }
  39.     }
  40.  
  41.     S.Next = NULL;
  42.     srand((unsigned)time(NULL));
  43.  
  44.     while(izbor != 'K' && izbor != 'k')
  45.     {
  46.         printf("\r\n\t\t********************");
  47.         printf("\r\n\t\t***   IZBORNIK   ***");
  48.         printf("\r\n\t\t********************");
  49.         printf("\r\n");
  50.         printf("\r\n\t\t1.\tPush na stog");
  51.         printf("\r\n\t\t2.\tPop sa stoga");
  52.         printf("\r\n\t\tk.\tKraj programa");
  53.         printf("\r\n\r\nIzbor : ");
  54.         scanf_s(" %c", &izbor, 1);
  55.  
  56.         switch(izbor)
  57.         {
  58.         case '1':
  59.             if(n < max)
  60.                 n = Push(&S, GetValueFromRange(0, 10000), n);
  61.             else
  62.                 printf("\r\nStog je popunjen!\r\n");
  63.             PrintList(S.Next, n);
  64.             break;
  65.         case '2':
  66.             if(n > 0)
  67.                 n = Pop(&S, n);
  68.             PrintList(S.Next, n);
  69.             break;
  70.         case 'K':
  71.         case 'k':
  72.             printf("\r\nBye bye!\r\n");
  73.             break;
  74.         default:
  75.             printf("\r\nPogresan izbor!\r\npokusajte ponovno.\r\n\r\n\r\n");
  76.         }
  77.     }
  78.  
  79.  
  80. }
  81.  
  82. int GetValueFromRange(int min, int max)
  83. {
  84.     int x = 0;
  85.  
  86.     x = rand()%(max-min) + min;
  87.  
  88.     return x;
  89. }
  90.  
  91. int Push(Position P, int x, int n)
  92. {
  93.     static Position last;
  94.     Position q;
  95.  
  96.     q=(Position)malloc(sizeof(struct _Cvor));
  97.     if(q)
  98.     {
  99.         if(P->Next == NULL)
  100.             last = q;
  101.  
  102.         q->Element = x;
  103.  
  104.         printf("\r\nNa listu se sprema %d.", x);
  105.         q->Next = P->Next;
  106.         P->Next = q;
  107.        
  108.         last->Next = q;
  109.         n++;
  110.     }
  111.     return n;
  112. }
  113.  
  114. int Pop(Position P, int n)
  115. {
  116.     static Position last = NULL;
  117.     Position tmp;
  118.  
  119.     if(P->Next != NULL)
  120.     {
  121.  
  122.         tmp = P->Next;
  123.  
  124.         printf("\r\nSa liste se skida %d.", tmp->Element);
  125.  
  126.         P->Next = tmp->Next;
  127.  
  128.         if(last == NULL && n > 1)
  129.         {
  130.             while(P->Next != tmp)
  131.                 P = P->Next;
  132.             last = P;
  133.         }
  134.  
  135.         last->Next = tmp->Next;
  136.  
  137.         free(tmp);
  138.         n--;
  139.         if(n == 0)
  140.             last = NULL;
  141.     }
  142.     else
  143.         printf("\r\nLista je prazna.");
  144.  
  145.     return n;
  146. }
  147.  
  148. void PrintList(Position P, int n)
  149. {
  150.     printf("\r\n\r\nU listi se nalaze %d elemenata:\r\n", n);
  151.     while(n > 0)
  152.     {
  153.         printf(" %d", P->Element);
  154.         P = P->Next;
  155.         n--;
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement