Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <inttypes.h>
- #include <stdlib.h>
- int main() {
- struct S {
- int num;
- int repeats;
- struct S *prev;
- struct S *next;
- };
- int x;
- struct S *head = NULL;
- while (scanf("%d", &x) == 1) {
- int isNew = 1;
- struct S *n = calloc(1, sizeof(*n));
- n->num = x;
- struct S* cur = head;
- while (cur) {
- if (cur->num == x) {
- if (cur->num == head->num) {
- head->repeats += 1;
- isNew = 0;
- break;
- }
- if (cur->next != NULL) {
- (cur->next)->prev = cur->prev;
- }
- if (cur->prev != NULL) {
- (cur->prev)->next = cur->next;
- }
- cur->next = head;
- cur->prev = NULL;
- if (head != NULL)
- head->prev = cur;
- cur->repeats += 1;
- head = cur;
- isNew = 0;
- break;
- }
- cur = cur->next;
- }
- if (isNew) {
- n->next = head;
- if (head != NULL)
- head->prev = n;
- head = n;
- n->repeats = 1;
- }
- //printf("headVal: %d, prev: %d, next: %d", head->num, (head->prev) == NULL, (head->next) == NULL);
- free(n);
- }
- struct S* cur = head;
- while (cur->next) {
- // struct S* curNew = cur->next;
- // free(cur);
- cur = cur->next;
- }
- // free(cur->next);
- while (cur) {
- printf("%d %d\n", cur->num, cur->repeats);
- struct S* curNew = cur->prev;
- free(cur);
- cur = curNew;
- }
- free(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement