fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define BUFFER_SIZE 256
  6.  
  7. struct Node
  8. {
  9. char line[BUFFER_SIZE];
  10. struct Node* next;
  11. struct Node* prev;
  12. };
  13.  
  14. struct List
  15. {
  16. struct Node* head;
  17. struct Node* tail;
  18. size_t size;
  19. };
  20.  
  21. struct Node* createNode(const char line[BUFFER_SIZE], struct Node* parentNode)
  22. {
  23. struct Node* node = malloc(sizeof(struct Node));
  24.  
  25. strcpy(node->line, line);
  26. node->next = NULL;
  27. node->prev = parentNode;
  28.  
  29. if(parentNode != NULL)
  30. {
  31. parentNode->next = node;
  32. }
  33.  
  34. return node;
  35. }
  36.  
  37. void addLineToList(struct List* list, const char line[BUFFER_SIZE])
  38. {
  39. list->tail = (list->head == NULL) ? list->head = createNode(line, NULL) : createNode(line, list->tail);
  40. ++list->size;
  41. }
  42.  
  43. void printList(struct List* list)
  44. {
  45. struct Node* node = list->head;
  46.  
  47. printf("Lines: %u\n", list->size);
  48.  
  49. while(node)
  50. {
  51. printf("Line: %s", node->line);
  52. node = node->next;
  53. }
  54. }
  55.  
  56. void deleteList(struct List* list)
  57. {
  58. struct Node* node = list->tail;
  59.  
  60. while(node)
  61. {
  62. struct Node* prevNode = node->prev;
  63.  
  64. free(node);
  65. node = prevNode;
  66. }
  67. }
  68.  
  69. void addLinesToList(struct List* list, FILE* file)
  70. {
  71. char text[BUFFER_SIZE];
  72.  
  73. while(fgets(text, BUFFER_SIZE, file) != NULL)
  74. {
  75. addLineToList(list, text);
  76. }
  77. }
  78.  
  79. int main()
  80. {
  81. struct List lineList = { NULL, NULL, 0u };
  82.  
  83. //addLinesToList(&lineList, fopen("data.txt", "r"));
  84. addLinesToList(&lineList, stdin);
  85. printList(&lineList);
  86. deleteList(&lineList);
  87.  
  88. return 0;
  89. }
Success #stdin #stdout 0s 2144KB
stdin
ala
ma
kota
a
kot
ma
ale
: )
stdout
Lines: 8
Line: ala
Line: ma
Line: kota
Line: a
Line: kot
Line: ma
Line: ale
Line: : )