Pp9

Zadanie1(sII)

Pp9
Feb 27th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. struct stos
  6. {
  7.     int liczba;
  8.     struct stos *n;
  9. };
  10.  
  11. void init(struct stos *(*head));
  12. void destroy(struct stos *(*head));
  13. struct stos* push (struct stos *head, int x);
  14. struct stos* pop (struct stos *head);
  15. int top (struct stos *head);
  16. int empty (struct stos *head);
  17. int full (struct stos *head);
  18.  
  19. int main()
  20. {
  21.     struct stos *HEAD;
  22.     init(&HEAD);
  23.     int ilosc, liczba;
  24.     srand(time(NULL));
  25.     ilosc=rand()%10;
  26.     for(int i=0; i<ilosc; i++)
  27.     {
  28.         liczba=rand()%10;
  29.         HEAD=push(HEAD, liczba);
  30.     }
  31.  
  32.     while(empty(HEAD))
  33.     {
  34.         printf("Liczba z wierzchu: %d\n", top(HEAD));
  35.         HEAD=pop(HEAD);
  36.     }
  37.     destroy(&HEAD);
  38.     return 0;
  39. }
  40.  
  41. void init(struct stos *(*head))
  42. {
  43.     *head=NULL;
  44. }
  45.  
  46. void destroy(struct stos *(*head))
  47. {
  48.     if((*head)!=NULL)
  49.     {
  50.         struct stos *tmp;
  51.         while(*head)
  52.         {
  53.             tmp=(*head)->n;
  54.             free(*head);
  55.             *head=tmp;
  56.         }
  57.  
  58.     }
  59. }
  60.  
  61. struct stos* push (struct stos *head, int x)
  62. {
  63.     struct stos *tmp;
  64.     tmp=malloc(sizeof(struct stos));
  65.     tmp->liczba=x;
  66.     tmp->n=head;
  67.     return tmp;
  68. }
  69.  
  70. struct stos* pop (struct stos *head)
  71. {
  72.     struct stos *tmp;
  73.     tmp=head->n;
  74.     free(head);
  75.     return tmp;
  76. }
  77.  
  78. int top (struct stos *head)
  79. {
  80.     if (head) return head->liczba;
  81.     else return NULL;
  82. }
  83.  
  84. int empty (struct stos *head)
  85. {
  86.     if(head==NULL) return 0;        //stos pusty
  87.     else return 1;                  //stos nie jest pusty
  88. }
  89.  
  90. int full (struct stos *head)
  91. {
  92.     return 0;                       //lista nie jest pe³na
  93. }
Advertisement
Add Comment
Please, Sign In to add comment