C中链表

来源:互联网 发布:linux java控制面板 编辑:程序博客网 时间:2024/06/05 15:43

链表中最简单的一种是单向链表,它包含两个域,一个信息域和一个指针域。这个链接指向列表中的下一个节点,而最后一个节点则指向一个空值。

Singly-linked-list.svg
一个单向链表包含两个值: 当前节点的值和一个指向下一个节点的链接

一个单向链表的节点被分成两个部分。第一个部分保存或者显示关于节点的信息,第二个部分存储下一个节点的地址。单向链表只可向一个方向遍历。

链表最基本的结构是在每个节点保存数据和到下一个节点的地址,在最后一个节点保存一个特殊的结束标记,另外在一个固定的位置保存指向第一个节点的指针,有的时候也会同时储存指向最后一个节点的指针。一般查找一个节点的时候需要从第一个节点开始每次访问下一个节点,一直访问到需要的位置。但是也可以提前把一个节点的位置另外保存起来,然后直接访问。当然如果只是访问数据就没必要了,不如在链表上储存指向实际数据的指针。这样一般是为了访问链表中的下一个或者前一个(需要储存反向的指针,见下面的双向链表)节点。

相对于下面的双向链表,这种普通的,每个节点只有一个指针的链表也叫单向链表,或者单链表,通常用在每次都只会按顺序遍历这个链表的时候(例如图的邻接表,通常都是按固定顺序访问的)。

例1,建立链表保存学生的信息,并且可以进行,插入,删除操作,并将学生的信息输出

[cpp] view plain copy
print?
  1. #include <stdio.h>  
  2. #include<malloc.h>  
  3. #define LEN sizeof(struct student )  
  4. #define NULL 0  
  5. struct student   
  6. {  
  7.     int num;  
  8.     float score;  
  9.     struct student *next;  
  10. };  
  11. int n;  
  12. //建立链表  
  13. struct student *create(void)  
  14. {  
  15.     struct student *head, *p1,*p2;  
  16.     n=0;  
  17.     p1=p2=(struct student *)malloc(LEN);  
  18.     scanf("%d %f",&p1->next,&p1->score);  
  19.     head=NULL;  
  20.     while(p1->num!=0)  
  21.     {  
  22.         n=n+1;  
  23.         if(n==1) head=p1;    //p1是指向新开辟的结点  
  24.         else p2->next=p1;  
  25.         p2=p1;    //p2指向链表中的最后一个基点  
  26.         p1=(struct student *)malloc(LEN); //开辟新的结点,并且使p1指向他  
  27.         scanf("%d %f",&p1->num,&p1->score);  
  28.     }  
  29.     p2->next=NULL;  
  30.     return (head);  
  31. }  
  32. //删除结点  
  33. struct student *del(struct student  *head,long num)  
  34. {  
  35.     struct student *p1,*p2;  
  36.       
  37.     if(head==NULL)   {printf("\nlist null!\n");goto end;}  
  38.     p1=head;  
  39.     while(num!=p1->num && p1->next!=NULL)   //p1指向不是所要找的结点,并且后面还有结点  
  40.     {  
  41.         p2=p1;  
  42.         p1=p1->next;      //p1后移一个及诶单  
  43.     }       //p1后移一个结点  
  44.     if(num==p1->num)     //找到了  
  45.     {  
  46.         if(p1==head)  head=p1->next;    //若p1指向的是首结点,把第二个结点的地址head  
  47.   
  48.         else p2->next=p1->next;      //or 将下一个结点的地址赋给前一个结点地址  
  49.         printf("delete:%d\n",num);  
  50.         n=n-1;  
  51.         free(p1);  
  52.     }  
  53.     else  printf("%dnot been found !\n",num);  
  54.     end:  
  55.     return(head);  
  56.   
  57.   
  58. }  
  59. //插入学生信息  
  60.  struct student *insert(struct student *head ,struct student *stud)  
  61. {  
  62.     struct student *p0,*p1,*p2;  
  63.     p1=head;         //使p1指向第一个结点  
  64.     p0=stud;        //p0指向要插入的结点  
  65.     if(head==NULL)     //原来的链表是空表  
  66.     {  
  67.         head=p0,p0->next=NULL; //使p0指向的结点作为头结点  
  68.     }  
  69.     else  
  70.     {  
  71.         while ((p1->num>p1->num)&&(p1->next!=NULL))  
  72.         {  
  73.             p2=p1;     //使p2指向刚才p1指向的及诶按  
  74.             p1=p1->next;    //p1后移一个结点  
  75.         }  
  76.         if(p0->num<=p1->num)  
  77.         {  
  78.             if(head==p1)  head=p0;   //插到原来第一个结点之前  
  79.             else p2->next=p0;        //插到p2指向的结点之后  
  80.             p0->next=p1;  
  81.         }  
  82.         else  
  83.         {  
  84.             p1->next=p0;p0->next=NULL;    //插到最后的结点之后  
  85.         }  
  86.     }  
  87.     n=n+1;                            //结点数加1  
  88.     return (head);  
  89. }  
  90. //输出链表  
  91.  void print(struct student *head)  
  92. {  
  93.     struct student *p;  
  94.     p=head;  
  95.     if(head!=NULL)  
  96.         do  
  97.     {  
  98.         printf("%d %f\n",p->num,p->score);  
  99.         p=p->next;  
  100.   
  101.     }  
  102.     while(p!=NULL);  
  103. }  
  104. //main函数  
  105.  void main()  
  106. {  
  107.     struct student *head,stu;  
  108.     long del_num;  
  109.     printf("Input records:\n");  
  110.     head=create();        //建立链表,返回头指针  
  111.     print(head);          //输出全部结点  
  112.   
  113.     printf("\nInput the deleted number:\n");  
  114.     scanf("%ld",&del_num);       //输入要删除的学号  
  115.     while(del_num!=0)  
  116.     {  
  117.     head=del(head,del_num);      //删除后链表的头指针  
  118.     print(head);                       //输出全部结点  
  119.     printf("Input the deleted number:\n");  
  120.     scanf("%ld",&del_num);   
  121.     }  
  122.   
  123.     printf("\nInput the inserted record:\n");  
  124.     stu=(struct student *)malloc(LEN);  
  125.     scanf("%d %f",&stu->num,&stu->score);     //输入要插入的结点  
  126.     {  
  127.     head=insert(head,&stu);                 //插入一个结点,返回头指针的  
  128.     print(head);                                     //输出全部结点  
  129.     printf("\nInput the inserted record:\n");  
  130.     stu=(struct student *)malloc(LEN);  
  131.     scanf("%d %f",&stu->num,&stu->score);  
  132.     }  
  133. }  
0 0
原创粉丝点击