建立链表—头插法、尾插法—有无头结点

1.建立链表-头插法-头结点

 1 //建立链表-头插法-头结点
 2 LinkList CreatList_head()
 3 {
 4     DataType x;                                 //数据
 5     LinkList p,head;                            //结点
 6     head = (LinkList)malloc(sizeof(LNode));
 7     head->next = NULL;
 8     head->data = 0;
 9 
10     scanf("%d",&x);
11     while(x != 999)
12     {
13         p = (LinkList)malloc(sizeof(LNode));
14         p->data = x;                     //存数据
15         p->next = head->next;
16         head->next = p;
17         head->data++;                //结点计数
18         scanf("%d",&x);
19     }
20     return head;
21 }
22 
23 //输出链表-头插法-头结点
24 void PrintList_head(LinkList p)
25 {
26     LinkList a;
27     a = p->next;
28     while(a != NULL)
29     {
30         printf("%d  ",a->data);
31         a = a->next;
32     }
33     printf("\n");
34 }

 

2.建立链表-头插法-无头结点

 1 //建立链表-头插法-无头结点
 2 LinkList CreatList()
 3 {
 4     DataType x;                                
 5     LinkList p,head;                         
 6     head = NULL;
 7 
 8     scanf("%d",&x);
 9     while(x != 999)
10     {
11         p = (LinkList)malloc(sizeof(LNode));
12         p->data = x;
13         if(head == NULL)
14         {    head = p;
15             p->next = NULL;
16         }
17         else
18         {
19             p->next = head;
20             head = p;
21         }
22         scanf("%d",&x);
23     }
24     return head;
25 }
26 
27 
28 //输出链表-头插法-无头结点
29 void PrintList(LinkList p)
30 {
31     LinkList a;
32     a = p;
33     while(a != NULL)
34     {
35         printf("%d  ",a->data);
36         a = a->next;
37     }
38     printf("\n");
39 }

 

3.建立链表-尾插法-无头结点

 

 1 //建立链表-头插法-无头结点
 2 LinkList CCC()
 3 {
 4     DataType x;                                
 5     LinkList p,head;                         
 6     head = NULL;
 7 
 8     scanf("%d",&x);
 9     while(x != 999)
10     {
11         p = (LinkList)malloc(sizeof(LNode));
12         p->data = x;
13         p->next = head;
14         head = p;
15     
16         scanf("%d",&x);
17     }
18     return head;
19 }
20 
21 
22 //输出链表-头插法-无头结点
23 void PPP(LinkList p)
24 {
25     LinkList a;
26     a = p;
27     while(a != NULL)
28     {
29         printf("%d  ",a->data);
30         a = a->next;
31     }
32     printf("\n");
33 }

 

4.建立链表-尾插法-头结点

 

 1 //建立链表-尾插法-头结点
 2 LinkList Tailhead()
 3 {
 4     DataType x;                               
 5     LinkList p,head,r;                           
 6     head = (LinkList)malloc(sizeof(LNode));
 7     head->next = NULL;
 8     r = head;
 9     scanf("%d",&x);
10     while(x != 999)
11     {
12         p = (LinkList)malloc(sizeof(LNode));
13         p->data = x;                    
14         p->next = r->next;
15         r->next = p;
16         r = p;             
17         scanf("%d",&x);
18     }
19 
20     return head;
21 }
22 //输出链表-尾插法-头结点
23 void PrintTailhead(LinkList p)
24 {
25     LinkList a;
26     a = p->next;
27     while(a != NULL)
28     {
29         printf("%d  ",a->data);
30         a = a->next;
31     }
32     printf("\n");
33 }

 

posted @ 2017-04-02 20:06  空空空空白  阅读(6142)  评论(1编辑  收藏  举报