Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define bool int
  5. #define true 1
  6. #define false 0
  7.  
  8. /*stos definiowanie*/
  9. /*#####################################################################*/
  10. typedef struct stos{
  11.     int value;
  12.     struct stos *next;
  13. } stos;
  14.  
  15. stos *stos_create(int value)
  16. {
  17.     stos *s = malloc(sizeof(stos));
  18.     s-> value = value;
  19.     s-> next = NULL;
  20. }
  21.  
  22. bool stos_is_empty(stos *s)
  23. {
  24.     if(s == NULL) return true;
  25.     return false;
  26. }
  27.  
  28. int stos_last(stos *s)
  29. {
  30.     int output = -1;
  31.  
  32.     while(s){
  33.  
  34.         output = s-> value;
  35.         s = s -> next;
  36.     }
  37.  
  38.     return output;
  39. }
  40.  
  41. void stos_add_value(stos *s, int value)
  42. {
  43.     stos *start = s;
  44.     stos *end = stos_create(value);
  45.  
  46.     while(s-> next != NULL){
  47.         s = s -> next;
  48.     }
  49.     s->next = end;
  50.     s = start;
  51. }
  52.  
  53. void stos_withdraw(stos *s)
  54. {
  55.  
  56.     if(stos_is_empty(s) == false){
  57.         while(s->next != NULL) s = s->next;
  58.         free(s);
  59.     }
  60. }
  61.  
  62. void stos_release(stos *s)
  63. {
  64.     stos *help_stos = s;
  65.     if(stos_is_empty(s)){
  66.         while(s){
  67.             help_stos = s->next;
  68.             free(s);
  69.             s = help_stos;
  70.         }
  71.     }
  72. }
  73.  
  74. void show_stos(stos *s)
  75. {
  76.     printf("stos starts:\n");
  77.     while(s){
  78.         printf("%d ",s->value);
  79.         s = s->next;
  80.     }
  81.     printf("\nstos ends\n");
  82. }
  83. /*#######################################################################*/
  84.  
  85. /*stos_testowanie*/
  86. int main(void)
  87. {
  88.     stos *s = stos_create(7);
  89.     stos_add_value(s, 8);
  90.     show_stos(s);
  91.     stos_add_value(s, 8989);
  92.     printf("ostatni: %d\n", stos_last(s));
  93.     show_stos(s);
  94.  
  95.     stos_withdraw(s);
  96.     printf("ostatni: %d\n", stos_last(s));
  97.  
  98.     show_stos(s);
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement