2017-05-01 //创建一个链表 #include <stdio.h> #include <malloc.h> typedef struct Link{ int data; struct Link *next; }Node; Node *createLinkList(int n)//创建新链表长度为n { int i, d; Node *head=NULL, *p=NULL, *last=NULL; for(i=0; i<n; i++) { printf("数据:"); scanf("%d", &d); p = (Node *)malloc(sizeof(Node)); p->data=d; p->next=NULL; if(head==NULL) head = p; else{ last->next=p; } last=p; } return head;//返回头指针 } //遍历链表 void traverse(Node *head) { Node *p=NULL; p=head;//p 指向头节点 do{ printf("%-5d", p->data); p=p->next;//p 指向下一个节点 }while(p!=NULL); printf("\n"); } //驱动测试函数 void main() { int n; Node *head=NULL; printf("链表长度:"); scanf("%d", &n); head = createLinkList(n); traverse(head);//遍历链表 }