Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define N 1000000000
- #define K 10
- struct Node {
- int n;
- struct Node *next;
- };
- struct Node *createNode(int n) {
- struct Node *elem = (struct Node *) malloc(sizeof(struct Node));
- elem->n = n;
- elem->next = NULL;
- return elem;
- }
- int main() {
- char end = ' ';
- int n;
- struct Node *head;
- struct Node *elem;
- struct Node *prev;
- struct Node *max = (struct Node *) malloc(sizeof(struct Node));
- struct Node *min = (struct Node *) malloc(sizeof(struct Node));
- struct Node *prev_max = (struct Node *) malloc(sizeof(struct Node));
- struct Node *prev_min = (struct Node *) malloc(sizeof(struct Node));
- struct Node *next_max;
- struct Node *next_min;
- max->n = -N;
- min->n = N;
- for (int i = 0; i < K; i++) {
- scanf("%d%c", &n, &end);
- elem = createNode(n);
- if ((elem->n > max->n) && (i != 0)) {
- max = elem;
- prev_max = prev;
- }
- if (elem->n < min->n){
- min = elem;
- prev_min = prev;
- }
- if (i == 0) {
- head = elem;
- prev = head;
- continue;
- }
- prev->next = elem;
- prev = elem;
- if (end == '\n')
- break;
- }
- // printf("%d %d\n", max->n, min->n);
- next_max = max->next;
- next_min = min->next;
- //-60 45 79 98 25 -59 97 34 37 -98
- if(max->next!=NULL && max->next->n==min->n){
- printf("****\n");
- max->next = max->next->next;
- min->next = max;
- prev_max->next = min;
- }
- else{
- if(min->next!=NULL && min->next->n==max->n){
- min->next = min->next->next;
- max->next = min;
- prev_min->next = max;
- }
- else{
- prev_max->next = min;
- prev_min->next = max;
- max->next = next_min;
- min->next = next_max;
- }
- }
- while (head != NULL) {
- printf("%d ", head->n);
- head = head->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement