Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Pair
  5. {
  6.     int value;
  7.     int times;
  8. } Pair;
  9.  
  10. typedef struct Node
  11. {
  12.     Pair pair;
  13.     struct Node *prev;
  14.     struct Node *next;
  15. } Node;
  16.  
  17. void print_pair(Node *ptr) {
  18.     if (ptr == NULL) {
  19.         printf("%s", "NULL");
  20.     } else {
  21.         printf("%d %d", ptr->pair.value, ptr->pair.times);
  22.     }
  23.  
  24.     // printf("");
  25. }
  26.  
  27. void add_in_start(Node **head_ref, Node **tail_ref, Pair value) {
  28.     Node *new_node = malloc(sizeof(Node));
  29.     new_node->pair = value;
  30.  
  31.     new_node->next = *head_ref;
  32.     new_node->prev = NULL;
  33.  
  34.     if (*head_ref != NULL) {
  35.         (*head_ref)->prev = new_node;
  36.     } else {  // head_ref == NULL <=> tail_ref == NULL
  37.         *tail_ref = new_node;
  38.     }
  39.  
  40.     *head_ref = new_node;
  41. }
  42.  
  43. void remove_node(Node **head_ref, Node **tail_ref, Node *node_ref) {
  44.     if (node_ref->prev != NULL) {
  45.         node_ref->prev->next = node_ref->next;
  46.     } else {
  47.         *head_ref = node_ref->next;
  48.     }
  49.  
  50.     if (node_ref->next != NULL) {
  51.         node_ref->next->prev = node_ref->prev;
  52.     } else {
  53.         *tail_ref = node_ref->prev;
  54.     }
  55.  
  56.     free(node_ref);
  57. }
  58.  
  59. void add(Node **head_ref, Node **tail_ref, int number) {
  60.     Node *ptr = *head_ref;
  61.  
  62.     while (ptr != NULL) {
  63.         if (ptr->pair.value == number) {
  64.             Pair new_pair = (Pair) { ptr->pair.value, ptr->pair.times + 1 };
  65.             remove_node(head_ref, tail_ref, ptr);
  66.             add_in_start(head_ref, tail_ref, new_pair);
  67.             return;
  68.         }
  69.  
  70.         ptr = ptr->next;
  71.     }
  72.  
  73.     add_in_start(head_ref, tail_ref, (Pair) { number, 1 });
  74. }
  75.  
  76. void print_from_head(Node **head_ref) {
  77.     Node *ptr = *head_ref;
  78.  
  79.     while (ptr != NULL) {
  80.         print_pair(ptr->prev);
  81.         print_pair(ptr);
  82.         print_pair(ptr->next);
  83.         printf("%s", "\n");
  84.  
  85.         ptr = ptr->next;
  86.     }
  87. }
  88.  
  89. void print_from_tail(Node **tail_ref) {
  90.     Node *ptr = *tail_ref;
  91.  
  92.     while (ptr != NULL) {
  93.         // print_pair(ptr->prev);
  94.         print_pair(ptr);
  95.         // print_pair(ptr->next);
  96.         printf("%s", "\n");
  97.  
  98.         ptr = ptr->prev;
  99.     }
  100. }
  101.  
  102. void free_list(Node **head_ref) {
  103.     Node *ptr = *head_ref;
  104.  
  105.     while (ptr != NULL) {
  106.         Node *tmp = ptr->next;
  107.         free(ptr);
  108.         ptr = tmp;
  109.     }
  110. }
  111.  
  112. int main() {
  113.     Node *head = NULL;
  114.     Node *tail = NULL;
  115.  
  116.     int number;
  117.     while (scanf("%d", &number) == 1) {
  118.         add(&head, &tail, number);
  119.         // print_from_head(&head);
  120.     }
  121.    
  122.     /*
  123.     add_in_start(&head, &tail, (Pair) {1, 1});
  124.     add_in_start(&head, &tail, (Pair) {2, 1});
  125.     add_in_start(&head, &tail, (Pair) {3, 1});
  126.     add_in_start(&head, &tail, (Pair) {4, 1});
  127.     remove_node(&head, &tail, head->next);
  128.     remove_node(&head, &tail, tail->prev);
  129.     remove_node(&head, &tail, tail);
  130.     remove_node(&head, &tail, head);
  131.     */
  132.  
  133.     print_from_tail(&tail);
  134.  
  135.     free_list(&head);
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement