Guest User

Untitled

a guest
Jan 7th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4.  
  5. struct Item
  6. {
  7.     struct Item * next, * prev;
  8.     int data;
  9.     int count;
  10. };
  11.  
  12. typedef struct Item Item;
  13.  
  14. int main() {
  15.     Item * head = NULL, * tail = NULL;
  16.     Item * p, * q;
  17.     int x;
  18.     while (scanf("%d", &x) == 1) {
  19.         for (p = head; p && ((p -> data) != x); p = p -> next) {}
  20.         if (!p) {
  21.             q = calloc(1, sizeof(Item));
  22.             q -> data = x;
  23.             q -> count = 1;
  24.             q -> next = head;
  25.             if (!head) {
  26.                 head = tail = q;
  27.                 q -> next = NULL;
  28.                 q -> prev = NULL;
  29.             } else {
  30.                 head -> prev = q;
  31.                 head = q;
  32.             }
  33.         } else if (!(p -> prev)) {
  34.             ++(p -> count);
  35.         } else if (!(p -> next)) {
  36.             tail = p -> prev;
  37.             p -> next = head;
  38.             head -> prev = p;
  39.             p -> prev -> next = NULL;
  40.             p -> prev = NULL;
  41.             head = p;
  42.             ++(head -> count);
  43.         } else {
  44.             p -> prev -> next = (p -> next);
  45.             p -> next -> prev = (p -> prev);
  46.             p -> prev = NULL;
  47.             p -> next = head;
  48.             head -> prev = p;
  49.             head = p;
  50.             ++(head -> count);
  51.         }
  52.     }
  53.     if (head) {
  54.         for (p = tail; p; p = p -> prev) {
  55.             printf("%d %d\n", p -> data, p -> count);
  56.         }
  57.     } else {
  58.         printf("\n");
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment