单链表的建立与输出
来源:互联网 发布:java weka bp神经网络 编辑:程序博客网 时间:2024/06/06 18:22
1 #include<stdio.h> 2 #include<malloc.h> 3 #define LEN sizeof(struct node) 4 typedef struct node* Node; 5 struct node 6 { 7 int num; 8 struct node *next; 9 };10 11 int n = 0;12 //创建单链表13 struct node* creat()14 {15 Node head;16 Node p1,p2;17 p1=p2=(Node)malloc(LEN);18 scanf("%d",&p1->num);19 head = NULL;20 while(p1->num != NULL)21 {22 n =n + 1;23 if(n==1) head = p1;24 else p2->next = p1;25 p2 = p1;26 p1 = (Node)malloc(LEN);27 scanf("%d",&p1->num);28 }29 p2->next = NULL;30 return head;31 }32 //输出链表33 void print(Node head)34 {35 Node p;36 p = head;37 do38 {39 printf("%d",p->num);40 p = p->next;41 }while(p!=NULL);42 }43 void main()44 {45 Node pHead = creat();46 print(pHead);47 }