Advertisement
LilChicha174

Untitled

Mar 23rd, 2022
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define N 1000000000
  5.  
  6. struct Node {
  7.     int n;
  8.     struct Node *next;
  9. };
  10.  
  11. struct Node *createNode(int n) {
  12.     struct Node *elem = (struct Node *) malloc(sizeof(struct Node));
  13.     elem->n = n;
  14.     elem->next = NULL;
  15.     return elem;
  16. };
  17.  
  18. int main() {
  19.     char end = ' ';
  20.     int n;
  21.     struct Node *head;
  22.     struct Node *elem;
  23.     struct Node *prev;
  24.     struct Node *max = (struct Node *) malloc(sizeof(struct Node));
  25.     struct Node *min = (struct Node *) malloc(sizeof(struct Node));
  26.     struct Node *prev_max = (struct Node *) malloc(sizeof(struct Node));
  27.     struct Node *prev_min = (struct Node *) malloc(sizeof(struct Node));
  28.     struct Node *next_max;
  29.     struct Node *next_min;
  30.     max->n = -N;
  31.     min->n = N;
  32.     for (int i = 0; i < 10; i++) {
  33.         scanf("%d%c", &n, &end);
  34.         elem = createNode(n);
  35.         if((elem->n>max->n) && (i!=0)){
  36.             max = elem;
  37.             prev_max = prev;
  38.         }
  39.         if(elem->n<min->n){
  40.             min = elem;
  41.             prev_min = prev;
  42.         }
  43.  
  44.         if (i == 0) {
  45.             head = elem;
  46.             prev = head;
  47.             continue;
  48.         }
  49.         prev->next = elem;
  50.         prev = elem;
  51.         if (end == '\n')
  52.             break;
  53.     }
  54.     next_max = max->next;
  55.     next_min = min->next;
  56.     prev_max->next = min;
  57.     prev_min->next = max;
  58.     max->next = next_min;
  59.     min->next = next_max;
  60.     while (head != NULL){
  61.         printf("%d ", head->n);
  62.         head = head->next;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement