Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct Elem
  6. {
  7. struct Elem *next;
  8. char *str;
  9. };
  10.  
  11. struct Elem *process_3(struct Elem *head, char *str, int precount) {
  12. if (head == NULL) {
  13. head = malloc(sizeof(struct Elem));
  14. head->next = NULL;
  15. head->str = (char*)malloc(strlen(str)+1);
  16. strcpy(head->str, str);
  17. return head;
  18. }//gotovo
  19. struct Elem *tmp = head;
  20. struct Elem *last = NULL;
  21. int count = 0;
  22. while (tmp && strcmp(tmp->str, str) < 0) {
  23. ++count;
  24. last = tmp;
  25. tmp = tmp->next;
  26. }
  27. if (tmp && strcmp(tmp->str, str) == 0)
  28. return head;
  29. struct Elem *new = malloc(sizeof(struct Elem));
  30. new->str = malloc(sizeof(char*) * (strlen(str) + 1));
  31. new->next = tmp;
  32. strcpy(new->str, str);
  33. if (last) {
  34. last->next = new;
  35. }
  36. else {
  37. head = new;
  38. }
  39. int i = 0;
  40. tmp = head;
  41. last = NULL;
  42. while(i < count - precount) {
  43. last = tmp;
  44. tmp = tmp->next;
  45. ++i;
  46. }
  47. while(i < count) {
  48. struct Elem* next = tmp->next;
  49. free(tmp->str);
  50. free(tmp);
  51. if (last) {
  52. last->next = next;
  53. }
  54. else {
  55. head = next;
  56. }
  57. tmp = next;
  58. ++i;
  59. }
  60. return head;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement