[关闭]
@bintou 2017-11-30T13:04:14.000000Z 字数 786 阅读 874

Linked list

CSI Source Code


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct _slist_t{
  4. int data;
  5. struct _slist_t *next;
  6. } slist_t;
  7. // This function prints contents of linked list starting from head
  8. void printList(slist_t *node)
  9. {
  10. while (node != NULL)
  11. {
  12. printf(" %d ", node->data);
  13. node = node->next;
  14. }
  15. printf("\n");
  16. }
  17. int main()
  18. {
  19. slist_t *list = NULL, *visitor; //empty list;
  20. slist_t *new_node = (slist_t*)malloc(sizeof(slist_t));
  21. new_node->data = 2017;
  22. new_node->next = NULL;
  23. //insert in list
  24. list = new_node;
  25. new_node = (slist_t*)malloc(sizeof(slist_t));
  26. new_node->data = 2018;
  27. new_node->next = NULL;
  28. //insert in head
  29. new_node->next = list;
  30. list = new_node;
  31. //Run through the list. We write this to warn you sth...
  32. visitor = list;
  33. int i = 0;
  34. while (visitor != NULL){
  35. printf("The %d th member is %d.\n", i++, visitor->data);
  36. visitor = visitor->next;
  37. }
  38. //Print the list using a function.
  39. printList(list);
  40. return 0;
  41. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注