fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef int data_type ;
  5.  
  6. struct node_type
  7. {
  8. struct node_type* next ;
  9. data_type data ;
  10. };
  11.  
  12. typedef struct list_type
  13. {
  14. struct node_type* head ;
  15. } List ;
  16.  
  17. List List_Create()
  18. {
  19. List list = { NULL } ;
  20. return list ;
  21. }
  22.  
  23. int List_AddItem(List* list, data_type item)
  24. {
  25. struct node_type* newNode = malloc(sizeof(struct node_type)) ;
  26.  
  27. if ( newNode )
  28. {
  29. newNode->data = item ;
  30. newNode->next = list->head ;
  31. list->head = newNode ;
  32. return 1 ;
  33. }
  34.  
  35. return 0 ;
  36. }
  37.  
  38. void List_Destroy(List* list)
  39. {
  40. struct node_type* curNode = list->head ;
  41.  
  42. while ( curNode )
  43. {
  44. list->head = curNode->next ;
  45. free(curNode) ;
  46. curNode = list->head ;
  47. }
  48. }
  49.  
  50. void List_Print(const List* list)
  51. {
  52. const struct node_type* curNode = list->head ;
  53.  
  54. while ( curNode )
  55. {
  56. printf( "%d\n", curNode->data) ;
  57. curNode = curNode->next ;
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. List list = List_Create() ;
  64.  
  65. for ( int i=0; i<10; ++i )
  66. List_AddItem(&list, i);
  67.  
  68. List_Print(&list) ;
  69. List_Destroy(&list) ;
  70. }
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
9
8
7
6
5
4
3
2
1
0