Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <inttypes.h>
  5. #include <ctype.h>
  6. #include <stdbool.h>
  7.  
  8. struct Elem
  9. {
  10. struct Elem *next;
  11. char *str;
  12. };
  13.  
  14. struct Elem *process_3(struct Elem *head, char *str, int precount) {
  15. struct Elem *h = head;
  16. struct Elem *last = NULL;
  17. bool flag = false;
  18. int cnt = 0;
  19. int id = 0;
  20. struct Elem *cur;
  21. while (h != NULL) {
  22. if (strcmp(h->str, str) == 0) {
  23. return head;
  24. } else if (strcmp(h->str, str) > 0) {
  25. id = cnt;
  26. flag = true;
  27. cur = malloc(sizeof(struct Elem));
  28. cur->str = calloc((strlen(str) + 1), sizeof(char));
  29. strcpy(cur->str, str);
  30. cur->next = h;
  31. if (last != NULL) {
  32. last->next = cur;
  33. }
  34. break;
  35. }
  36. last = h;
  37. h = h->next;
  38. ++cnt;
  39. }
  40. if (!flag) {
  41. id = cnt;
  42. cur = malloc(sizeof(struct Elem));
  43. cur->str = calloc((strlen(str) + 1), sizeof(char));
  44. strcpy(cur->str, str);
  45. cur->next = NULL;
  46. if (last != NULL) {
  47. last->next = cur;
  48. }
  49. }
  50. if (id < precount) {
  51. precount = id;
  52. }
  53. // printf("precount = %d\n", precount);
  54. // printf("id = %d\n", id);
  55. if (id == 0) {
  56. head = cur;
  57. }
  58. if (precount == 0) {
  59. return head; // ничего не надо удалять
  60. }
  61. struct Elem *g = head;
  62. int i = 0;
  63.  
  64. struct Elem *from = NULL;
  65. while (g != NULL) {
  66. if (i == id - precount - 1) {
  67. from = g;
  68. // printf("from string = %s\n", from->str);
  69. g = g->next;
  70. ++i;
  71. while (i < id) {
  72. struct Elem *nxt = g->next;
  73. // printf("nxt string = %s\n", nxt->str);
  74. free(g->str);
  75. free(g);
  76. g = nxt;
  77. ++i;
  78. }
  79. from->next = cur;
  80. break;
  81. }
  82. ++i;
  83. g = g->next;
  84. }
  85. if (precount == id) {
  86. return cur;
  87. } else {
  88. return head;
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement