Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Node
- {
- int value;
- struct Node *next;
- };
- void swap(int *x, int *y)
- {
- int t;
- t = *x;
- *x = *y;
- *y = t;
- // *x = *x + *y;
- // *y = *x - *y;
- // *x = *x - *y;
- };
- void reverse(int *array, size_t size)
- {
- for (int i = 0; i < size / 2; ++i)
- {
- swap(&array[i], &array[size - i - 1]);
- }
- };
- void printArray(int *array, size_t size)
- {
- for (int i = 0; i < size; ++i)
- {
- printf("%d ", array[i]);
- }
- };
- void printMatrix(int **matrix, size_t r, size_t c)
- {
- for (int i = 0; i < r; ++i)
- {
- for (int j = 0; j < c; ++j)
- {
- printf("%d\t", matrix[i][j]);
- }
- printf("\n");
- }
- };
- void transpose(int **matrix, int **t, size_t r, size_t c)
- {
- for (int i = 0; i < c; ++i)
- {
- for (int j = 0; j < r; ++j)
- {
- t[i][j] = matrix[j][i];
- }
- }
- };
- void listAppend(struct Node **head, int value)
- {
- struct Node *newNode = (struct Node *) malloc(sizeof(struct Node));
- struct Node *last = *head;
- newNode->value = value;
- newNode->next = NULL;
- // if empty
- if (*head == NULL)
- {
- *head = newNode;
- return;
- }
- while (last->next != NULL)
- last = last->next;
- last->next = newNode;
- }
- void listPrint(struct Node *list)
- {
- while (list != NULL)
- {
- printf("%d ", list->value);
- list = list->next;
- }
- }
- void listReverse(struct Node **list)
- {
- struct Node *previous = NULL;
- struct Node *current = *list;
- struct Node *next = NULL;
- while (current != NULL)
- {
- next = current->next;
- current->next = previous;
- previous = current;
- current = next;
- }
- *list = previous;
- }
- void task1()
- {
- int a, b;
- scanf("%d %d", &a, &b);
- swap(&a, &b);
- printf("x: %d y: %d", a, b);
- };
- void task2()
- {
- int n;
- int *arr;
- scanf("%d", &n);
- arr = malloc(n * sizeof(int));
- for (int i = 0; i < n; ++i)
- {
- int input;
- scanf("%d", &input);
- arr[i] = input;
- }
- reverse(arr, n);
- printArray(arr, n);
- free(arr);
- }
- void task3()
- {
- int r, c;
- scanf("%d %d", &r, &c);
- int **matrix = malloc(r * sizeof(int *));
- for (int i = 0; i < r; ++i)
- {
- matrix[i] = malloc(c * sizeof(int));
- }
- for (int i = 0; i < r; ++i)
- {
- for (int j = 0; j < c; ++j)
- {
- int input;
- scanf("%d", &input);
- matrix[i][j] = input;
- }
- }
- int **t = malloc(c * sizeof(int *));
- for (int i = 0; i < c; ++i)
- {
- t[i] = malloc(r * sizeof(int));
- }
- transpose(matrix, t, r, c);
- printMatrix(t, c, r);
- free(matrix);
- free(t);
- }
- void task4()
- {
- int input;
- struct Node *head = NULL;
- scanf("%d", &input);
- while (input != 0)
- {
- listAppend(&head, input);
- scanf("%d", &input);
- }
- listReverse(&head);
- listPrint(head);
- }
- int main()
- {
- //task1();
- //task2();
- //task3();
- //task4();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement