Advertisement
LilChicha174

Untitled

Mar 24th, 2022
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define N 1000000000
  6. #define K 10
  7. struct Node {
  8.     int n;
  9.     struct Node *next;
  10. };
  11.  
  12. struct Node *createNode(int n) {
  13.     struct Node *elem = (struct Node *) malloc(sizeof(struct Node));
  14.     elem->n = n;
  15.     elem->next = NULL;
  16.     return elem;
  17. }
  18.  
  19. int main() {
  20.     char end = ' ';
  21.     int n;
  22.     struct Node *head;
  23.     struct Node *elem;
  24.     struct Node *prev;
  25.     struct Node *max = (struct Node *) malloc(sizeof(struct Node));
  26.     struct Node *min = (struct Node *) malloc(sizeof(struct Node));
  27.     struct Node *prev_max = (struct Node *) malloc(sizeof(struct Node));
  28.     struct Node *prev_min = (struct Node *) malloc(sizeof(struct Node));
  29.     struct Node *next_max;
  30.     struct Node *next_min;
  31.     max->n = -N;
  32.     min->n = N;
  33.     for (int i = 0; i < K; i++) {
  34.         scanf("%d%c", &n, &end);
  35.         elem = createNode(n);
  36.         if ((elem->n > max->n) && (i != 0)) {
  37.             max = elem;
  38.             prev_max = prev;
  39.         }
  40.         if (elem->n < min->n){
  41.             min = elem;
  42.             prev_min = prev;
  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. //    printf("%d %d\n", max->n, min->n);
  55.     next_max = max->next;
  56.     next_min = min->next;
  57.     //-60 45 79 98 25 -59 97 34 37 -98
  58.     if(max->next!=NULL && max->next->n==min->n){
  59.         printf("****\n");
  60.         max->next = max->next->next;
  61.         min->next = max;
  62.         prev_max->next = min;
  63.     }
  64.     else{
  65.         if(min->next!=NULL && min->next->n==max->n){
  66.             min->next = min->next->next;
  67.             max->next = min;
  68.             prev_min->next = max;
  69.         }
  70.         else{
  71.  
  72.             prev_max->next = min;
  73.             prev_min->next = max;
  74.             max->next = next_min;
  75.             min->next = next_max;
  76.         }
  77.     }
  78.  
  79.     while (head != NULL) {
  80.         printf("%d ", head->n);
  81.         head = head->next;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement