单链表 之c代码

我们知道数据结构就是数据及其相互关系,包括逻辑结构和物理结构。单链表的逻辑结构是一种一对一的线性关系,物理结构是利用节点把数据结合起来,在计算机中体现这种一对一的数据关系。单链表节点包括包含数据本身信息的数据域和体现数据一对一关系的指针域。因为单链表只有一个指向后一节点的单一指针域next 所以单链表只能从前往后遍历,而不能从后向前遍历,这就意味着一旦单链表的某一节点丢失 ,后面所有的数据信息都会丢失,并且单链表有头指针唯一确定,要想查找单链表的
某一数据只能从头开始遍历,最坏时间复杂度为O(n),但因为数据间是通过指针来确定数据的前后关系的,所以单链表在内存中是不一定要连续分配的内存,可以更有效的利用内存空间,同时可以快速的在一个节点后插入和删除一个数据元素。在某一节点后插入和删除一个节点的最坏时间复杂度O(1).在编写程序时,特别是面向过程的c语言程序一定牢记 程序 =数据结构+ 算法。数据在内存中有两种存储方式,一种就是连续存储的数组,另一种就是结构体。算法就是对数据的操作。同时 数据结构 和定义在数据结构上的算法 组成了抽象数据类型,注意这里是类型,类型不只是数据,还有定义在该数据的相关算法。

单链表 是n个相同节点组成的,每一个节点又包含了两个信息 ,包含数据信息的数据域,包含数据间关系的指针域所以要声明一个单链表结构体之前我们必须声明一个节点结构体单链表节点结构体ListNode。

1.头文件list.h

[html]  view plain copy
  1. //单链表List.h  
  2. #ifndef LIST_H  
  3. #define LIST_H  
  4.   
  5. //单链表节点的数据结构  
  6. typedef struct ListNode{  
  7.     void * data;//因为 不知道单链表中存储的是何种数据 ,为了抽象所以用一个泛型指针 void* 指向数据  
  8.     ListNode *next;//指向下一节点的指针 next  
  9. }ListNode;  
  10.   
  11. //可针对单链表节点的相关操作(1)用一个数据初始化一个节点 (2)读取节点的数据信息 (3)销毁一个节点  
  12. //init_Node  
  13. ListNode* init_node(void *data); //初始化一个节点 最后生成的是一个节点  
  14. void *get_data(const ListNode * list_node);  
  15. void destroy_node(ListNode * list_node);  
  16.   
  17. //单链表抽象类型  
  18. //单链表数据结构 (1)单链表的大小size (2)头节点 head (3)尾节点 tail (4)添加一个新节点 (5)销毁节点  
  19. typedef struct List{  
  20.     int size;//大小  
  21.     ListNode *head;//头节点指针  
  22.     ListNode *tail;//尾节点指针  
  23.     ListNode* (*init_node)(void *data);//添加一个节点  
  24.     int (*destroy_node)(ListNode *list_node);//销毁一个节点  
  25. }List;  
  26.   
  27. //单链表的相关操作:(1)初始化单链表 init_list (2)在一个节点后插入一个新的节点 ins_next_list   
  28. //(3)在一个节点后删除一个节点rem_next_list (4)销毁单链表 dstroy_list  
  29. //(5)单链表大小 size_list (6) 获取头节点 head_list (7)获取尾节点 tail_list (8)判断是否为头节点 is_head   
  30. //判断是否为尾节点 is_tail  
  31. void init_list(List *list);  
  32. int ins_next_list(List *list,ListNode *now_node,int(*init_node)(void *data));  
  33. int rem_next_list(List *list,ListNode *now_node,int(*destroy_node)(ListNode *list_node));  
  34. void destroy_list(List *list);  
  35. int size_list(const List *list);  
  36. ListNode* head_list(const List *list);  
  37. ListNode* tail_list(const List *list);  
  38. int is_head(const List *list);  
  39. int is_tail(const List *list);  
  40.   
  41. #endif  

2.单链表实现文件list.cpp

[html]  view plain copy
  1. //list.cpp  
  2. #include<stdlib.h>  
  3. #include<stdio.h>  
  4.   
  5. #include"list.h"  
  6.   
  7. //生成一个单链表节点:init_node  
  8. //功能:动态分配内存,生成一个单链表节点,数据域中的数据为 data, 指针域为NULL 为单链表的插入做准备  
  9. //返回值:内存中动态分配的单链表节点指针 ListNode  
  10. //时间复杂度:O(1)  
  11. ListNode* init_node(void *data){   
  12.   
  13.     ListNode* new_node ;//声明一个单链表节点类型的指针  
  14.   
  15.         //在堆中为新的节点分配内存空间  
  16.     if( ( new_node = (ListNode*)malloc( sizeof(ListNode) ) ) == NULL )  
  17.         return NULL;//分配失败  
  18.   
  19.     //分配成功  
  20.     //将数据封装为节点  
  21.     new_node->data = data;  
  22.     new_node->next = NULL;  
  23.   
  24.     //将指向该节点的指针返回  
  25.     return new_node;  
  26.   
  27. }  
  28.   
  29. //destroy_node:释放节点list_node 持有的内存空间  
  30. //返回值 :void  
  31. //参数; list_node 为单链表节点  
  32. //时间复杂度:O(1)  
  33. void destroy_node(ListNode * list_node){  
  34.     free(list_node);  
  35.     return;  
  36. }  
  37.   
  38. //初始化单链表 init_list  
  39. // 功能:对链表的其余操作都是在此初始化之后,如果不调用init_list,编译器会报错:list 未被初始化。  
  40. //返回值 void  
  41. //时间复杂度O(1)  
  42. void init_list(List *list){  
  43.     list->size = 0;  
  44.     list->head = NULL;  
  45.     list->tail = NULL;  
  46. }  
  47.   
  48. //在链表中插入一个新的节点:ins_next_list  
  49. //功能:在一个节点now_node后插入一个数据项为data的新的节点,如果now_node为 NULL 则在链表的头部插入  
  50. //返回值:当插入成功返回0,插入失败 返回-1  
  51. //时间复杂度:O(1)  
  52. //参数:list 为空, list 只有一个节点,list有多个节点 , now_node 为空, now_node 为中间节点  
  53. //now_node 为尾部节点  
  54. //测试用例 {list == NULL , now_node == NULL , [1,2,3,4,5],now_node->next == NULL}  
  55. int ins_next_list(List *list,ListNode *now_node, void *data){  
  56.     //把数据封装为一个新的节点,这里需要动态分配内存空间,已经在 int(*init_node)(void *data)   
  57.     //中封装好了  
  58.     if( list == NULL )  
  59.         return -1;  
  60.       
  61.     else  
  62.     {  
  63.     ListNode *new_node = (*init_node)(data);//封装好的节点类型  
  64.       
  65.     //将节点插入到相应位置  
  66.     //查看链表list 中有哪些元素发生了改变  
  67.     //size自增  
  68.     if(now_node == NULL)//新节点 作为头节点进行插入  
  69.     {  
  70.         if(list->size == 0)//当链表为空时  
  71.         {   
  72.   
  73.             list->head = new_node;  
  74.             list->tail = new_node;  
  75.             ++list->size;  
  76.         }  
  77.         else//链表非空时,  
  78.         {  
  79.             new_node->next = list->head;  
  80.             list->head = new_node;  
  81.             ++list->size;  
  82.         }  
  83.         //这里可以把共有的代码提取出来  
  84.         //list->head = new_node;  
  85.         //++list->size;  
  86.     }  
  87.   
  88.     //新节点 非头节点(now_node 为中间节点,now_node 为链表中最后第二个节点,now_node 为最后的一个节点)  
  89.     else  
  90.     {  
  91.         if(now_node == list->tail)//当前节点为尾节点  
  92.         {  
  93.             new_node->next = now_node->next;  
  94.             now_node->next = new_node;  
  95.             list->tail = new_node;  
  96.             ++list->size;  
  97.         }  
  98.         else//当前节点为中间节点  
  99.         {  
  100.             new_node->next = now_node->next;  
  101.             now_node->next = new_node;  
  102.             ++list->size;  
  103.         }  
  104.         //这里可以把插入节点的共有代码提取出来:  
  105.         // new_node->next = now_node->next;  
  106.         // now_node->next = new_node;  
  107.   
  108.           
  109.     }  
  110.     //插入都有链表长度加1  
  111.     // ++list->szie  
  112.   
  113.     return 0;  
  114.   
  115. }  
  116.   
  117. }  
  118.   
  119.   
  120.   
  121. //rem_next_list:从单链表list当前节点 now_node  后删除一个节点,list必须已经初始化,当传入实参now_node   
  122. //为NULL时 ,删除头节点  
  123. //函数返回值: 当删除成功返回0,当删除失败 返回 -1  
  124. //参数说明:list 必须已经初始化,list 不能为空,now_node == NULL 删除头节点,now_node 为尾节点时,返回-1,  
  125. //int(*destroy_node)(ListNode *list_node);为函数指针,指向节点的析构函数,也就是说在堆中动态释放该节点所占有的内存空间。  
  126. //时间复杂度:O(1)  
  127. //测试用例说明:1.list 未初始化(return -1),2.list 为空2.list 只有一个节点(now_node == NULL(删除头节点,head =tail = NULL,size =0) ,now_node == head (删除失败,return-1))  
  128. //3.list 有多个节点(now_node == NULL(删除头节点,head =head->next), now_node == head, now_node == 中间节点(pnode),(正常删除,1.保存原有信息2.释放节点内存空间,3.size-1)  
  129. //now_node =倒数第二个节点(删除尾节点, 正常删除外, tail ,size 需要改变),now_node =tail(删除失败 ,return -1))  
  130.   
  131. int rem_next_list(List *list,ListNode *now_node,void (*destroy_node) (ListNode *list_node)){  
  132.     ListNode * old_node;//将要删除的节点  
  133.     ListNode *pnode ;//保存的中间节点  
  134.   
  135.     if(list == NULL)//链表不存在  
  136.         return -1;  
  137.   
  138.     if(list->size == 0)//链表为空  
  139.         return -1;  
  140.   
  141.     if(list->size == 1)//链表只有一个节点  
  142.     {  
  143.         if(now_node == NULL)//删除头节点  
  144.         {  
  145.             old_node = list->head;  
  146.             destroy_node(old_node);//销毁节点,释放节点所占的内存空间  
  147.               
  148.             list->head = list->tail = NULL;//链表为空  
  149.             --list->size ;  
  150.   
  151.             return 0;  
  152.   
  153.         }  
  154.   
  155.         else //删除失败  
  156.             return -1;  
  157.     }  
  158.   
  159.     else if (list->size == 2)//链表由两个节点  
  160.     {  
  161.   
  162.         if(now_node == NULL)//删除头节点  
  163.         {    
  164.             pnode = list->head->next;  
  165.             old_node = list->head;  
  166.             destroy_node( old_node );//销毁节点  
  167.             list->head = list->tail = pnode;//删除后剩下的唯一节点,即是头节点,又是尾节点  
  168.             --list->size;  
  169.             return 0;  
  170.         }  
  171.   
  172.         /*else if( now_node->next = list->tail )*/  
  173.         else if( now_node->next == list->tail )//删除尾节点  
  174.         {   
  175.             now_node -> next = list->tail->next;//删除尾节点,必须使尾节点的前一节点 的next 指向 NULL;  
  176.             old_node = now_node->next;//要删除的节点,也就是尾节点  
  177.             //pnode = old_node->next;  
  178.             destroy_node( old_node );  
  179.             list->tail = list->head;//删除后只剩下一个节点了,尾节点和头节点是同一个节点,都是原来的头节点  
  180.             --list->size;  
  181.             return 0;  
  182.         }  
  183.   
  184.         else//删除不存在的节点,失败  
  185.         {  
  186.   
  187.             return -1;  
  188.         }  
  189.     }  
  190.     else //链表有n(n>2)个节点  
  191.     {  
  192.         if(now_node == NULL)//删除头节点  
  193.         {  
  194.             old_node = list->head;  
  195.             pnode = old_node->next;  
  196.             destroy_node( old_node );//销毁节点  
  197.             list->head = pnode;  
  198.             --list->size;  
  199.             return (0);  
  200.         }  
  201.         else//删除其他节点  
  202.         {  
  203.             old_node = now_node->next;  
  204.   
  205.             if( old_node == NULL )//删除不存在的节点  
  206.             {  
  207.                 return -1;  
  208.             }  
  209.   
  210.             else if( old_node == list->tail )//删除尾节点  
  211.             {  
  212.                 //now_node -> next = NULL;//如果删除的是尾节点 注意一定要将尾节点的前一节点的指针域 next 指向NULL  
  213.                 now_node -> next = old_node -> next;  
  214.                 //pnode = old_node->next;  
  215.                 pnode = now_node;  
  216.                 destroy_node( old_node );  
  217.                 list->tail = pnode;  
  218.                 --list->size;  
  219.   
  220.                 return 0;  
  221.             }  
  222.   
  223.             else//删除中间节点  
  224.             {  
  225.                 pnode = old_node->next;  
  226.                 now_node->next = pnode;//将要删除的节点从链表中移除  
  227.                 destroy_node(old_node);//释放节点所占的内存  
  228.                 --list->size;//链表的节点数 -1  
  229.                 return 0;  
  230.   
  231.             }  
  232.   
  233.   
  234.         }  
  235.     }  
  236.   
  237. }  
  238.   
  239.   
  240. //destroy_list:销毁单链表,从头到尾 将链表的节点销毁  
  241. //返回值:销毁成功 返回 0,销毁失败 返回-1;  
  242. //参数:已经初始化了的单链表  
  243. //时间复杂度:O(n)相当于遍历了整个单链表  
  244. void destroy_list(List *list){  
  245.     while (list->size > 0 )//如果链表不为空,调用已经定义好的函数从头到尾删除链表的节点  
  246.         rem_next_list(list,NULL,destroy_node);  
  247.       
  248. }  
  249.   
  250.   
  251. int main(void){  
  252.     List list;//声明一个单链表  
  253.     List *plist = &list;  
  254.   
  255.     //测试初始化链表:init_list(&list)  
  256.     init_list(plist);//初始化单链表 此时   
  257.     printf("list_size:%d\n",plist->size);  
  258.     printf("list_head:%p\n",plist->head);  
  259.     printf("list_tail:%p\n",plist->tail);  
  260.   
  261.     //测试在链表中插入一个节点:int ins_next_list(List *list,ListNode *now_node, void *data)  
  262.     //测试数据集data{1,2,3,4,5}  
  263.     //测试用例[list,now_node,data]=[NULL,NULL,1];[list,NULL,1];[list,head,2];[list,中间节点,3]  
  264.     //[list,NULL,0];[list,tail,5]  
  265.     int data[5] ={1,2,3,4,5};  
  266.       
  267.     int True = ins_next_list(NULL,NULL,&data[1]);//返回值为 -1插入失败  
  268.     printf("ins_next_list:%d\n",True);  
  269.   
  270.     True = ins_next_list(plist,NULL,&data[1]);//添加头节点 数据项 data =2 ,list_size = 1链表中的数据元素:{2}  
  271.     printf("ins_next_list:%d\n",True);  
  272.     printf("List_size:%d\n",plist->size);  
  273.     printf("*data:%d\n",*(int*)(plist->head->data));  
  274.   
  275.     True = ins_next_list(plist,plist->head,&data[2]);//在头节点后添加一个节点 数据项 data=3 ,list_size =2 链表中数据元素{2,3}  
  276.     printf("ins_next_list:%d\n",True);  
  277.     printf("List_size:%d\n",plist->size);  
  278.     printf("*data:%d\n",*(int*)(plist->head->next->data));  
  279.   
  280.     True = ins_next_list(plist,plist->head->next,&data[3]);//在第二个节点后添加一个节点 数据项 data =4 ,list_size = 3 ,链表中的数据元素{2,3,4}  
  281.     printf("ins_next_list:%d\n",True);  
  282.     printf("List_size:%d\n",plist->size);  
  283.     printf("*data:%d\n",*(int*)(plist->head->next->next->data));  
  284.   
  285.     True = ins_next_list(plist,NULL,&data[0]);//在链表头节点前插入一个节点 作为新的头节点,数据项 data = 1,链表中的数据元素 {1,2,3,4}  
  286.     printf("ins_next_list:%d\n",True);  
  287.     printf("List_size:%d\n",plist->size);  
  288.     printf("*data:%d\n",*(int*)(plist->head->data));  
  289.   
  290.     True = ins_next_list(plist,plist->tail,&data[4]);//在尾节点后添加一个节点 作为新的尾节点,数据项 data = 5, 链表中的数据元素{1,2,3,4,5}  
  291.     printf("ins_next_list:%d\n",True);  
  292.     printf("List_size:%d\n",plist->size);  
  293.     printf("*data:%d\n",*(int*)(plist->tail->data));  
  294.   
  295.     //输出链表中的所有数据元素(遍历){1,2,3,4,5}  
  296.     ListNode* pNode = plist->head;  
  297.     while( pNode != NULL ){  
  298.           
  299.         printf("%d ",*(int*)pNode->data );  
  300.         pNode = pNode->next;  
  301.     }  
  302.     printf("\n");  
  303.   
  304.     测试销毁链表 现在链表的数据元素为{1,2,3,4,5},  
  305.   
  306.  //   destroy_list(plist);  
  307.     //printf("plist->size:%d\n",plist->size);//如果为 0 链表成功销毁  
  308.     //  
  309.   
  310.     //测试在一个链表中删除节点  
  311. //现在链表中总共有5个数据 从头至尾遍历 结果为{1,2,3,4,5}  
  312. //测试用例为{链表未初始化,链表为空, 链表有n(n=5)个节点(删除头节点,删除尾节点,删除中间节点)链表只有两个节点,链表只有一个节点}  
  313.   
  314.     ListNode* qnode;//测试用的节点  
  315.     List qlist;  
  316.     List *pqlist = &qlist;//测试用的链表  
  317.     int result; //用来判断是否删除节点失败的结果,result = 0,删除成功,result = -1,删除失败  
  318.   
  319.     //1.链表未初始化,通不过编译器的语法检查  
  320.     //qnode = NULL;  
  321.     //  result = rem_next_list(pqlist,qnode,destroy_node);//  
  322.     //printf("rem_next_list: %d",result);  
  323.   
  324.     //1.链表为空,删除失败返回-1  
  325.     init_list(pqlist);  
  326.     qnode = NULL;  
  327.         result = rem_next_list(pqlist,qnode,destroy_node);//  
  328.     printf("rem_next_list: %d\n",result);  
  329.   
  330.     //2.链表有5个节点,{1,2,3,4,5}删除头节点{2,3,4,5}  
  331.     pqlist = plist;  
  332.     qnode = NULL;  
  333.     result = rem_next_list(pqlist,qnode,destroy_node );  
  334.     printf("rem_next_list: %d\n",result);  
  335.   
  336.     pNode = pqlist->head;  
  337.     while( pNode != NULL ){  
  338.           
  339.         printf("%d ",*(int*)pNode->data );  
  340.         pNode = pNode->next;  
  341.     }  
  342.     printf("\n");  
  343.   
  344.   
  345.     //删除链表中的尾节点,删除后链表中元素为{2,3,4};  
  346.   
  347.     pqlist = plist;  
  348.   
  349.     //找到尾节点前一节点  
  350.     qnode = pqlist->head;  
  351.     while(qnode->next != pqlist->tail )  
  352.         qnode = qnode->next;  
  353.     //此时删除的是尾节点5  
  354.     result = rem_next_list(pqlist,qnode,destroy_node );  
  355.     printf("rem_next_list: %d\n",result);  
  356.   
  357.     pNode = pqlist->head;  
  358.     while( pNode != NULL ){  
  359.         printf("%d ",*(int*)pNode->data );  
  360.         pNode = pNode->next;  
  361.     }  
  362.     printf("\n");  
  363.   
  364.     //此时链表数据{2,3,4},删除中间节点3后链表数据域{2,4}  
  365.     pqlist = plist;  
  366.   
  367.    //找到尾节点前一节点  
  368.     qnode = pqlist->head;  
  369.       
  370.     //此时删除的是尾节点3  
  371.     result = rem_next_list(pqlist,qnode,destroy_node );  
  372.     printf("rem_next_list: %d\n",result);  
  373.   
  374.     pNode = pqlist->head;  
  375.     while( pNode != NULL ){  
  376.         printf("%d ",*(int*)pNode->data );  
  377.         pNode = pNode->next;  
  378.     }  
  379.     printf("\n");  
  380.   
  381.     //只有两个节点{2,4}删除头节点2 后,链表数据为{4}  
  382.     pqlist = plist;  
  383.   
  384.     //要删除的节点  
  385.     qnode = NULL;  
  386.     //此时删除的是2各数据的头节点;  
  387.     result = rem_next_list(pqlist,qnode,destroy_node );  
  388.     printf("rem_next_list: %d\n",result);  
  389.   
  390.     pNode = pqlist->head;  
  391.     while( pNode != NULL ){  
  392.         printf("%d ",*(int*)pNode->data );  
  393.         pNode = pNode->next;  
  394.     }  
  395.     printf("\n");  
  396.   
  397.     只有两个节点{2,4}删除尾节点4 后,链表数据为{2}  
  398.     //pqlist = plist;  
  399.   
  400.     要删除的节点前一节点  
  401.     //qnode = pqlist->head;  
  402.     此时删除的是2各数据的头节点;  
  403.     //result = rem_next_list(pqlist,qnode,destroy_node );  
  404.     //printf("rem_next_list: %d\n",result);  
  405.   
  406.  //   pNode = pqlist->head;  
  407.     //while( pNode != NULL ){  
  408.     //  printf("%d ",*(int*)pNode->data );  
  409.     //  pNode = pNode->next;  
  410.     //}  
  411.     //printf("\n");  
  412.   
  413.     //只有两个节点{2,4},当前节点 为 尾节点时  
  414.     //pqlist = plist;  
  415.     //printf("size: %d\n",pqlist->size);  
  416.   
  417.     当前的节点 为尾节点 删除失败 返回-1;删除后数据不变还是{2,4}  
  418.     //qnode = pqlist->tail;  
  419.     //  
  420.   
  421.     //result = rem_next_list(pqlist,qnode,destroy_node );  
  422.     //printf("rem_next_list: %d\n",result);  
  423.   
  424.  //   pNode = pqlist->head;  
  425.     //while( pNode != NULL ){  
  426.     //  printf("%d ",*(int*)pNode->data );  
  427.     //  pNode = pNode->next;  
  428.     //}  
  429.     //printf("\n");  
  430.   
  431.   
  432.       
  433.   
  434.     //此时 链表中只剩一个节点数据{4},  
  435.     pqlist = plist;  
  436.   
  437.     //要删除的前一节点 为head,删除失败 返回-1; 操作结束后链表中还是{4}  
  438.     qnode = pqlist->head;  
  439.     result = rem_next_list(pqlist,qnode,destroy_node );  
  440.   
  441.   
  442.     printf("rem_next_list: %d\n",result);  
  443.   
  444.     pNode = pqlist->head;  
  445.     while( pNode != NULL ){  
  446.         printf("%d ",*(int*)pNode->data );  
  447.         pNode = pNode->next;  
  448.     }  
  449.   
  450.     printf("\n");  
  451.   
  452.   
  453.     //此时 链表中只剩一个节点数据{4},  
  454.     pqlist = plist;  
  455.   
  456.     //传入参数 为 NULL,删除成功返回 0; 操作结束后链表为 空;  
  457.     qnode = NULL;  
  458.     result = rem_next_list(pqlist,qnode,destroy_node );  
  459.     printf("rem_next_list: %d\n",result);  
  460.     printf("链表已经为 空啦,啦啦啦");  
  461.   
  462.     pNode = pqlist->head;  
  463.     while( pNode != NULL ){  
  464.         printf("%d ",*(int*)pNode->data );  
  465.         pNode = pNode->next;  
  466.     }  
  467.     printf("\n");  
  468.       
  469.       
  470.       
  471.   
  472.     return 0;  
  473. }  
3.测试结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值