Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <inttypes.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6.  
  7. struct Elem
  8. {
  9. struct Elem *next;
  10. char *str;
  11. };
  12. struct Elem *dup_elem(struct Elem *head) {
  13. struct Elem *cur = head;
  14. struct Elem *pred = NULL;
  15. while (cur != NULL) {
  16. const int radix = 10;
  17. char *eptr;
  18. errno = 0;
  19. long long value = strtol(cur->str, &eptr, radix);
  20. if (!*cur->str || *eptr || errno) {
  21. //printf("%s\n", cur->str);
  22. pred = cur;
  23. cur = cur->next;
  24. } else {
  25. //printf("%s\n", cur->str);
  26. value -= 1;
  27. if (value == (int32_t)value) {
  28. struct Elem *new = malloc(8);
  29. new->str = malloc(30);
  30. new->next = cur;
  31. sprintf(new->str, "%d", (int32_t)value);
  32. if (pred == NULL) {
  33. head = new;
  34. } else {
  35. pred->next = new;
  36. }
  37. }
  38. pred = cur;
  39. cur = cur->next;
  40. }
  41. }
  42. return head;
  43. }
  44. int main() {
  45. char s1[20];
  46. char s2[20];
  47. char s3[20];
  48. scanf("%s%s%s",s1,s2,s3);
  49.  
  50. struct Elem h1;
  51. h1.str = s1;
  52. h1.next = NULL;
  53.  
  54. struct Elem h2;
  55. h2.str = s2;
  56.  
  57. h1.next = &h2;
  58.  
  59. struct Elem h3;
  60. h3.str = s3;
  61.  
  62. h2.next = &h3;
  63. h3.next = NULL;
  64.  
  65. struct Elem *head = dup_elem(&h1);
  66. while (head != NULL) {
  67. printf("%s\n", head->str);
  68. head = head->next;
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement