Advertisement
Adam_mz_

OS_HW1

Apr 16th, 2022
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct Node
  6. {
  7.     int value;
  8.     struct Node *next;
  9. };
  10.  
  11.  
  12. void swap(int *x, int *y)
  13. {
  14.     int t;
  15.     t = *x;
  16.     *x = *y;
  17.     *y = t;
  18. //    *x = *x + *y;
  19. //    *y = *x - *y;
  20. //    *x = *x - *y;
  21. };
  22.  
  23. void reverse(int *array, size_t size)
  24. {
  25.     for (int i = 0; i < size / 2; ++i)
  26.     {
  27.         swap(&array[i], &array[size - i - 1]);
  28.     }
  29. };
  30.  
  31. void printArray(int *array, size_t size)
  32. {
  33.  
  34.     for (int i = 0; i < size; ++i)
  35.     {
  36.         printf("%d ", array[i]);
  37.     }
  38. };
  39.  
  40.  
  41. void printMatrix(int **matrix, size_t r, size_t c)
  42. {
  43.     for (int i = 0; i < r; ++i)
  44.     {
  45.         for (int j = 0; j < c; ++j)
  46.         {
  47.             printf("%d\t", matrix[i][j]);
  48.         }
  49.         printf("\n");
  50.     }
  51.  
  52. };
  53.  
  54.  
  55. void transpose(int **matrix, int **t, size_t r, size_t c)
  56. {
  57.     for (int i = 0; i < c; ++i)
  58.     {
  59.         for (int j = 0; j < r; ++j)
  60.         {
  61.             t[i][j] = matrix[j][i];
  62.         }
  63.     }
  64. };
  65.  
  66. void listAppend(struct Node **head, int value)
  67. {
  68.     struct Node *newNode = (struct Node *) malloc(sizeof(struct Node));
  69.     struct Node *last = *head;
  70.  
  71.     newNode->value = value;
  72.     newNode->next = NULL;
  73.  
  74. //    if empty
  75.     if (*head == NULL)
  76.     {
  77.         *head = newNode;
  78.         return;
  79.     }
  80.  
  81.     while (last->next != NULL)
  82.         last = last->next;
  83.  
  84.     last->next = newNode;
  85.  
  86.  
  87. }
  88.  
  89. void listPrint(struct Node *list)
  90. {
  91.     while (list != NULL)
  92.     {
  93.         printf("%d ", list->value);
  94.         list = list->next;
  95.     }
  96. }
  97.  
  98. void listReverse(struct Node **list)
  99. {
  100.     struct Node *previous = NULL;
  101.     struct Node *current = *list;
  102.     struct Node *next = NULL;
  103.  
  104.     while (current != NULL)
  105.     {
  106.         next = current->next;
  107.         current->next = previous;
  108.         previous = current;
  109.         current = next;
  110.     }
  111.     *list = previous;
  112. }
  113.  
  114.  
  115. void task1()
  116. {
  117.     int a, b;
  118.     scanf("%d %d", &a, &b);
  119.     swap(&a, &b);
  120.     printf("x: %d y: %d", a, b);
  121.  
  122. };
  123.  
  124. void task2()
  125. {
  126.     int n;
  127.     int *arr;
  128.     scanf("%d", &n);
  129.     arr = malloc(n * sizeof(int));
  130.     for (int i = 0; i < n; ++i)
  131.     {
  132.         int input;
  133.         scanf("%d", &input);
  134.         arr[i] = input;
  135.     }
  136.  
  137.     reverse(arr, n);
  138.     printArray(arr, n);
  139.  
  140.     free(arr);
  141.  
  142.  
  143. }
  144.  
  145. void task3()
  146. {
  147.     int r, c;
  148.     scanf("%d %d", &r, &c);
  149.  
  150.     int **matrix = malloc(r * sizeof(int *));
  151.     for (int i = 0; i < r; ++i)
  152.     {
  153.         matrix[i] = malloc(c * sizeof(int));
  154.     }
  155.     for (int i = 0; i < r; ++i)
  156.     {
  157.         for (int j = 0; j < c; ++j)
  158.         {
  159.             int input;
  160.             scanf("%d", &input);
  161.             matrix[i][j] = input;
  162.  
  163.         }
  164.     }
  165.  
  166.     int **t = malloc(c * sizeof(int *));
  167.     for (int i = 0; i < c; ++i)
  168.     {
  169.         t[i] = malloc(r * sizeof(int));
  170.     }
  171.  
  172.  
  173.     transpose(matrix, t, r, c);
  174.  
  175.     printMatrix(t, c, r);
  176.  
  177.     free(matrix);
  178.     free(t);
  179. }
  180.  
  181. void task4()
  182. {
  183.     int input;
  184.     struct Node *head = NULL;
  185.     scanf("%d", &input);
  186.  
  187.     while (input != 0)
  188.     {
  189.         listAppend(&head, input);
  190.         scanf("%d", &input);
  191.     }
  192.     listReverse(&head);
  193.     listPrint(head);
  194. }
  195.  
  196. int main()
  197. {
  198.     //task1();
  199.     //task2();
  200.     //task3();
  201.     //task4();
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement