Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- struct Item
- {
- struct Item * next, * prev;
- int data;
- int count;
- };
- typedef struct Item Item;
- int main() {
- Item * head = NULL, * tail = NULL;
- Item * p, * q;
- int x;
- while (scanf("%d", &x) == 1) {
- for (p = head; p && ((p -> data) != x); p = p -> next) {}
- if (!p) {
- q = calloc(1, sizeof(Item));
- q -> data = x;
- q -> count = 1;
- q -> next = head;
- if (!head) {
- head = tail = q;
- q -> next = NULL;
- q -> prev = NULL;
- } else {
- head -> prev = q;
- head = q;
- }
- } else if (!(p -> prev)) {
- ++(p -> count);
- } else if (!(p -> next)) {
- tail = p -> prev;
- p -> next = head;
- head -> prev = p;
- p -> prev -> next = NULL;
- p -> prev = NULL;
- head = p;
- ++(head -> count);
- } else {
- p -> prev -> next = (p -> next);
- p -> next -> prev = (p -> prev);
- p -> prev = NULL;
- p -> next = head;
- head -> prev = p;
- head = p;
- ++(head -> count);
- }
- }
- if (head) {
- for (p = tail; p; p = p -> prev) {
- printf("%d %d\n", p -> data, p -> count);
- }
- } else {
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment