第四周项目1 建立单链表

来源:互联网 发布:阿里云主机和万网主机 编辑:程序博客网 时间:2024/05/21 17:07
[cpp] view plain copy
  1. /*  
  2. *Copyright(c)2017,烟台大学计算机学院  
  3. *All right reserved.  
  4. *文件名:main.cpp list.h list.cpp  
  5. *作者:盛凯  
  6. *完成日期:2017年9月28日  
  7. *版本号:v1.0  
  8.  
  9. *问题描述:建立单链表  
  10. *输入描述:无  
  11. *程序输出:见窗口  
  12. */    
  13. #include <stdio.h>  
  14. #include <malloc.h>  
  15. #include "list.h"  
  16.   
  17.   
  18. int main()  
  19. {  
  20.     LinkList *L1, *L2;  
  21.     ElemType a[8]= {7, 9, 8, 2, 0, 4, 6, 3};  
  22.     CreateListF(L1, a, 8);  
  23.     printf("头插法建表结果:");  
  24.     DispList(L1);  
  25.     CreateListR(L2, a, 6);  
  26.     printf("尾插法建表结果:");  
  27.     DispList(L2);  
  28.     DestroyList(L1);  
  29.     DestroyList(L2);  
  30.     return 0;  
  31. }  

list.cpp

[cpp] view plain copy
  1. #include "stdio.h"  
  2. #include <malloc.h>  
  3. #include "list.h"  
  4.   
  5. void CreateListF(LinkList *&L,ElemType a[],int n)//头插法建立单链表  
  6. {  
  7.     LinkList *s;  
  8.     int i;  
  9.     L=(LinkList *)malloc(sizeof(LinkList));     //创建头结点  
  10.     L->next=NULL;  
  11.     for (i=0; i<n; i++)  
  12.     {  
  13.         s=(LinkList *)malloc(sizeof(LinkList));//创建新结点  
  14.         s->data=a[i];  
  15.         s->next=L->next;            //将*s插在原开始结点之前,头结点之后  
  16.         L->next=s;  
  17.     }  
  18. }  
  19. void CreateListR(LinkList *&L,ElemType a[],int n)//尾插法建立单链表  
  20. {  
  21.     LinkList *s,*r;  
  22.     int i;  
  23.     L=(LinkList *)malloc(sizeof(LinkList));     //创建头结点  
  24.     L->next=NULL;  
  25.     r=L;                    //r始终指向终端结点,开始时指向头结点  
  26.     for (i=0; i<n; i++)  
  27.     {  
  28.         s=(LinkList *)malloc(sizeof(LinkList));//创建新结点  
  29.         s->data=a[i];  
  30.         r->next=s;          //将*s插入*r之后  
  31.         r=s;  
  32.     }  
  33.     r->next=NULL;           //终端结点next域置为NULL  
  34. }  
  35.   
  36. void DestroyList(LinkList *&L)  //销毁单链表  
  37. {  
  38.     LinkList *p=L,*q=p->next;  
  39.     while (q!=NULL)  
  40.     {  
  41.         free(p);  
  42.         p=q;  
  43.         q=p->next;  
  44.     }  
  45.     free(p);    //此时q为NULL,p指向尾结点,释放它  
  46. }  
  47.   
  48. void DispList(LinkList *L)  //输出单链表  
  49. {  
  50.     LinkList *p=L->next;  
  51.     while (p!=NULL)  
  52.     {  
  53.         printf("%d ",p->data);  
  54.         p=p->next;  
  55.     }  
  56.     printf("\n");  
  57. }  

list.h

[cpp] view plain copy
  1. #ifndef LIST_H_INCLUDED  
  2. #define LIST_H_INCLUDED  
  3. typedef int ElemType;  
  4. typedef struct LNode        //定义单链表结点类型  
  5. {  
  6.     ElemType data;  
  7.     struct LNode *next;     //指向后继结点  
  8. } LinkList;  
  9.   
  10. void CreateListF(LinkList *&L,ElemType a[],int n);//头插法建立单链表  
  11. void CreateListR(LinkList *&L,ElemType a[],int n);//尾插法建立单链表  
  12. void DestroyList(LinkList *&L); //销毁单链表  
  13. void DispList(LinkList *L); //输出单链表  
  14.   
  15.   
  16. #endif // LIST_H_INCLUDED  
程序运行如图:


知识点总结

练习了头插法和尾插法两种方式,头插法是在头指针之后插入,尾指针是在最后插入,多多练习两种算法才能真正熟悉并运用到实例中。

原创粉丝点击