自己写的第一个链表程序

来源:互联网 发布:360沙盒软件 编辑:程序博客网 时间:2024/06/06 04:01
# include <stdio.h># include <malloc.h> struct Node{    int data;    struct Node * next;}; struct Node * create_list(){    int len;     printf("请输入节点个数:");    scanf("%d", &len);     struct Node * pHead = (struct Node *)malloc(sizeof(struct Node));    struct Node * p = pHead;     pHead->data = NULL;     for (int i = 0; i < len; i++)    {        struct Node * pNew = (struct Node *)malloc(sizeof(struct Node));         pHead->next = pNew;        pNew->data = i;        pNew->next = NULL;        pHead = pNew;     }     return p;} void traverse_list(struct Node * pHead){    while (pHead)    {        printf("%d\n", pHead->next->data);        pHead = pHead->next;    }     return;} int main(void){    traverse_list(create_list());     return 0;}

0 0
原创粉丝点击