Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <inttypes.h>
  3. #include <stdlib.h>
  4.  
  5. int main() {
  6.     struct S {
  7.         int num;
  8.         int repeats;
  9.         struct S *prev;
  10.         struct S *next;
  11.     };
  12.     int x;
  13.     struct S *head = NULL;
  14.     while (scanf("%d", &x) == 1) {
  15.         int isNew = 1;
  16.         struct S *n = calloc(1, sizeof(*n));
  17.         n->num = x;
  18.         struct S* cur = head;
  19.         while (cur) {
  20.             if (cur->num == x) {
  21.                 if (cur->num == head->num) {
  22.                     head->repeats += 1;
  23.                     isNew = 0;
  24.                     break;
  25.                 }
  26.                 if (cur->next != NULL) {
  27.                     (cur->next)->prev = cur->prev;
  28.                 }
  29.                 if (cur->prev != NULL) {
  30.                     (cur->prev)->next = cur->next;
  31.                 }
  32.                 cur->next = head;
  33.                 cur->prev = NULL;
  34.                 if (head != NULL)
  35.                     head->prev = cur;
  36.                 cur->repeats += 1;
  37.                 head = cur;
  38.                 isNew = 0;
  39.                 break;
  40.             }
  41.             cur = cur->next;
  42.         }
  43.         if (isNew) {
  44.             n->next = head;
  45.             if (head != NULL)
  46.                 head->prev = n;
  47.             head = n;
  48.             n->repeats = 1;
  49.         }
  50.         //printf("headVal: %d, prev: %d, next: %d", head->num, (head->prev) == NULL, (head->next) == NULL);
  51.         free(n);
  52.     }
  53.     struct S* cur = head;
  54.     while (cur->next) {
  55. //        struct S* curNew = cur->next;
  56. //        free(cur);
  57.         cur = cur->next;
  58.     }
  59.  //   free(cur->next);
  60.     while (cur) {
  61.         printf("%d %d\n", cur->num, cur->repeats);
  62.         struct S* curNew = cur->prev;
  63.         free(cur);
  64.         cur = curNew;
  65.     }
  66.     free(head);
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement