数据结构(一)创建并遍历线性列表

数据结构第一篇:创建线性列表并遍历

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 //定义列表元素结构
 5 typedef struct  Node
 6 {
 7     int name;
 8     struct Node  *_next;
 9 }PNode ;
10 
11 //创建线性列表
12 PNode  *createStudnetList(){
13     int i;
14     PNode *head,*temp,*tail;
15     head=tail=NULL;
16     printf("enter 3 char
");
17     for(i=0;i<3;i++)
18     {
19         temp=(PNode*)malloc(sizeof(PNode)); 
20         scanf("%d",&temp->name);
21         if(head==NULL){
22             head=tail=temp;
23         }
24         else
25         {
26             tail->_next=temp;
27             tail=temp;
28             tail->_next=NULL;
29         }
30     }
31     return head;
32 }
33 
34 //遍历链表
35 void printList(PNode *m)
36 {
37     PNode *p=m;
38     printf("the list is:
");
39     while (p->_next!=NULL)
40     {
41         printf("%d
",p->name);
42         p=p->_next;
43     }
44     printf("%d
",p->name);
45 }
46 
47 void main(){
48     PNode *list=createStudnetList();
49     printList(list);
50     system("pause");
51 }

 

原文地址:https://www.cnblogs.com/huangc/p/5700785.html