首页 > 其他 > 详细

前插法创建带头节点的单链表

时间:2018-11-21 23:19:18      阅读:234      评论:0      收藏:0      [点我收藏+]
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 typedef struct{
 4     int info;
 5     struct node *next;
 6 }node;
 7 node* creat(){
 8     int n,i;
 9     node *head;
10     head=(node*)malloc(sizeof(node));
11     head->next=NULL;
12     printf("input creat node‘s num\n ");
13     scanf("%d",&n);
14     for(i=0;i<n;i++){
15         node *p;
16         p=(node*)malloc(sizeof(node));
17         printf("input node value\n");
18         scanf("%d",&p->info);
19         p->next=head->next;
20         head->next=p;
21     }
22     return head;
23     
24 }
25 print(node* head)
26 {
27     node* q;
28     q=head->next;
29     while(q)
30     {
31         if(q->next==NULL){printf("%d",q->info);}
32         else{printf("%d->",q->info);}
33         q=q->next; 
34     }
35 }
36 
37 int main()
38 {
39     node *head;
40     head=creat();
41     print(head);
42     return 0;
43 }

思路:

创建头结点head:head的指针域附为空

创建节点:p

P->next=head->next // p->next域附为空

head->next=p // 把p节点连接到头指针后

前插法创建带头节点的单链表

原文:https://www.cnblogs.com/Thegonedays/p/9998052.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!