1. #include<stdio.h>
    2. #include<malloc.h>
    3. typedef struct node
    4. {
    5. int data;
    6. struct node *next;
    7. }NODE;
    8. NODE *create() /*此函数采用后插入方式建立单链表,并返回一个指向链表表头的指针*/
    9. {
    10. NODE *head,*q,*p; /*定义指针变量*/
    11. char ch;
    12. int a;
    13. head=(NODE*)malloc(sizeof(NODE)); /*申请新的存储空间,建立表头结点*/
    14. q=head;
    15. ch='*';
    16. printf("\nInput the list :");
    17. while(ch!='?') /*"ch"为是否建立新结点的标志,若"ch"为"?"则输入结束*/
    18. {
    19. scanf("%d",&a); /*输入新元素*/
    20. p=(NODE*)malloc(sizeof(NODE));
    21. p->data=a;
    22. q->next=p;
    23. q=p;
    24. ch=getchar(); /*读入输入与否的标志*/
    25. }
    26. q->next=NULL;
    27. return(head); /*返回表头指针head*/
    28. }
    29. main()
    30. {
    31. int i;
    32. NODE *a;
    33. a=create();
    34. printf("output the list:");
    35. a=a->next;
    36. while(a!=NULL)
    37. {
    38. printf("%d ",a->data); /*输出链表各元素*/
    39. a=a->next;
    40. }
    41. }