Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <inttypes.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <wctype.h>
  8. #include <wchar.h>
  9. #include <locale.h>
  10. #include <math.h>
  11. #include <ctype.h>
  12.  
  13. struct Elem
  14. {
  15.     struct Elem *next;
  16.     char *str;
  17. };
  18.  
  19. char *stringcopywithpointer( const char *source) {
  20.     int len = strlen(source);
  21.     char *copy = malloc(len+1);
  22.     strcpy(copy, source);
  23.     return copy;
  24. }
  25.  
  26. struct Elem *process_3(struct Elem * root, char *str, int precount) {
  27.     struct Elem* tmp = root;
  28.     if (!root) return root;
  29.     if (!str) return root;
  30.     int cmpres = strcmp(root->str, str);
  31.     if (cmpres == 0) {
  32.         return root;
  33.     }
  34.     if (cmpres > 0) {
  35.         struct Elem* begin = malloc(sizeof(struct Elem));
  36.         begin->str = stringcopywithpointer(str);
  37.         begin->next = root;
  38.         return begin;
  39.     }
  40.     while (tmp->next != NULL && strcmp(tmp->next->str, str) < 0) tmp = tmp->next;
  41.     if (tmp->next && strcmp(tmp->next->str, str) == 0) return root;
  42.     struct Elem* nxt = tmp->next;
  43.     tmp->next = malloc(sizeof(struct Elem));
  44.     tmp->next->str = stringcopywithpointer(str);
  45.     tmp->next->next = nxt;
  46.     if (precount <= 0)
  47.         return root;
  48.     struct Elem* from = root;
  49.     int met = 0;
  50.     struct Elem* to = root;
  51.     for (int i = 0; i < precount; i++) {
  52.         if (to == tmp) {
  53.             met = 1;
  54.             to = to->next;
  55.             break;
  56.         }
  57.         to = to->next;
  58.     }
  59.     struct Elem* res = (met ? tmp->next : root);
  60.     int strt = met;
  61.     while (!met) {
  62.         if (to == tmp) {
  63.             met = 1;
  64.             to = to->next;
  65.             from = from->next;
  66.             break;
  67.         }
  68.         to = to->next;
  69.         from = from->next;    
  70.     }
  71.     if (!strt) {
  72.         struct Elem* tmpfrom = root;
  73.         while (tmpfrom->next != from) {
  74.             tmpfrom = tmpfrom->next;
  75.         }
  76.         tmpfrom->next    = tmp->next;
  77.     }
  78.     while (from != to) {
  79.         free(from->str);
  80.         struct Elem* tmpn = from->next;
  81.         free(from);
  82.         from = tmpn;
  83.     }
  84.     return res;
  85. }
  86.  
  87. int main() {
  88.     struct Elem* root = malloc(sizeof(struct Elem));
  89.     root->str = malloc(3);
  90.     root->str[0] = 'a';
  91.     root->str[1] = 'a';
  92.     root->str[2] = 0;
  93.     struct Elem* tmp = root;
  94.     for (int i = 0; i < 5; i++) {
  95.         root->next  = malloc(sizeof(struct Elem));
  96.         root = root->next;
  97.         root->str = malloc(3);
  98.         root->str[0] = 'a';
  99.         root->str[1] = 'a' + 2 * (i + 1);
  100.         root->str[2] = 0;
  101.     }
  102.     root = tmp;
  103.     while(tmp != NULL) {
  104.         printf("%s\n", tmp->str);
  105.         tmp = tmp->next;
  106.     }
  107.     printf("\n");
  108.     char* a = malloc(3);
  109.     a[0] = 'a';
  110.     a[1] = 'd';
  111.     a[2] = 0;
  112.     tmp = process_3(root, a, 3);
  113.     free(a);
  114.     while(tmp != NULL) {
  115.         printf("%s\n", tmp->str);
  116.         fflush(stdout);
  117.         tmp = tmp->next;
  118.     }
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement