fork(2) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define LEN 30
  6.  
  7. char * sgets(char *, int);
  8.  
  9. struct book {
  10. char title[LEN];
  11. char author[LEN];
  12. char id;
  13. };
  14.  
  15. typedef struct book Item;
  16.  
  17. typedef struct node {
  18. Item item;
  19. struct node * next;
  20. } Node;
  21.  
  22. int main(void)
  23. {
  24. Node * head;
  25. Node * prev, * current;
  26.  
  27.  
  28. while (1)
  29. {
  30. current = malloc(sizeof *current);
  31.  
  32. if (head == NULL)
  33. head = current;
  34. else
  35. prev->next = current;
  36.  
  37. if (sgets(current->item.author, LEN) == NULL || current->item.author[0] == '\0')
  38. {
  39. current = NULL;
  40. break;
  41. }
  42.  
  43. if (sgets(current->item.title, LEN) == NULL || current->item.title[0] == '\0')
  44. {
  45. current = NULL;
  46. break;
  47. }
  48.  
  49. if (scanf("%hhd", &current->item.id) != 1)
  50. {
  51. current = NULL;
  52. break;
  53. }
  54. while (getchar() != '\n')
  55. continue;
  56.  
  57. current->next = NULL;
  58.  
  59. prev = current;
  60. }
  61.  
  62. current = head;
  63.  
  64. while (current)
  65. {
  66. printf("%d: %s - %s\n", current->item.id, current->item.title, current->item.author);
  67. current = current->next;
  68. }
  69.  
  70. current = head;
  71.  
  72. while (current != NULL)
  73. {
  74. free(current);
  75. current = current->next;
  76. }
  77.  
  78. return 0;
  79. }
  80.  
  81. char * sgets(char * str, int len)
  82. {
  83. char * temp;
  84.  
  85. fgets(str, len, stdin);
  86.  
  87. temp = strchr(str, '\n');
  88.  
  89. if (temp)
  90. *temp = '\0';
  91. else
  92. while (getchar() != '\n')
  93. continue;
  94.  
  95. return str;
  96. }
Time limit exceeded #stdin #stdout 5s 5548KB
stdin
Standard input is empty
stdout
Standard output is empty