当前位置:   article > 正文

【C语言】 链表 超详细解析_c语言链表

c语言链表

目录

一:静态存储和动态存储   

二:存储类别

三:malloc函数

四: free函数

五:内存初始化函数memset

六:calloc函数

七:realloc函数

八:线性表

九:链式存储结构

十:线性表的基本操作

十一:链表的创建和链接

十二:链表的遍历

十三:链表的插入

十四:链表的删除

十五:链表实际应用


一:静态存储和动态存储   

变量从变量值存在的时间(即生存期)角度分:静态存储方式和动态存储方式

静态:在编译时确定了固定的内存地址与内存大小,如:函数里的static局部变量、全局变量等

动态:由程序控制,运行时主动性的向系统申请所需大小的内存段,并且每次分配到的内存地址不固定

在动态存储区存放一下数据:

1、函数形式参数

2、自动变量(未加static声明)

3、函数调用时的现场保护和返回地址

二:存储类别

三:malloc函数

malloc函数 如下

malloc函数示例

四: free函数

free函数 如下

free函数示例

五:内存初始化函数memset

memset函数 如下

memset函数示例

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main( )
  4. {
  5. char buffer[] = "This is a test of the memset function";
  6. printf( "Before: %s\n", buffer );
  7. memset( buffer, 0, 4 );
  8. printf( "After: %s\n", buffer );
  9. return 0;
  10. }

六:calloc函数

calloc函数 如下

calloc函数示例

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void main() {
  4. float *calloc1;
  5. int i;
  6. calloc1 = (float *) calloc(3, sizeof(float));
  7. if(calloc1!=NULL) {
  8. for(i = 0 ; i < 3 ; i++)
  9. printf("\ncalloc1[%d]holds%05.5f", i,calloc1[i]);
  10. free(calloc1);
  11. }
  12. else { printf("Not enough memory \n");
  13. }
  14. }

七:realloc函数

realloc函数 如下

realloc函数示例

  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. int main(){
  4. int *ptr , i;
  5. ptr = (int *)calloc(5, sizeof(int));
  6. if(ptr ==NULL) return 1;
  7. *ptr = 1;
  8. *(ptr+1) = 2;
  9. ptr[2] = 4;
  10. ptr[3] = 8;
  11. ptr[4] = 16;
  12. ptr = (int *)realloc(ptr,7*sizeof(int));
  13. if(ptr == NULL) return 1;
  14. ptr[5] = 32;
  15. ptr[6] = 64;
  16. for(i = 0;i < 7;i++){
  17. printf(“ptr[%d]:%d\n", i, ptr[i]);
  18. }
  19. realloc(ptr,0); /* free(ptr);*/
  20. return 0;
  21. }

八:线性表

线性表 如下

顺序存储结构以及特点

九:链式存储结构

链式存储结构 如下

单链表 如下

单链表状态图 如下

单链表节点数据结构定义 如下

单链表初始化 如下

循环单链表状态图 如下

双向链表状态图 如下

双向循环链表状态图 如下

十:线性表的基本操作

初始化

插入

删除

遍历(即访问每一个元素,如打印所有信息)

查找

排序

十一:链表的创建和链接

十二:链表的遍历

十三:链表的插入

1.插入链表头和链表尾

2.插入中间的位置 

十四:链表的删除

十五:链表实际应用

普通链表设计

通用链表设计

通用链表基本操作

初始化链表

  void *List_Init(void *data)

添加链表节点

  void List_Add(struct list *head,void *data)

获取链表节点个数

  int List_Count(struct list *head)

获取链表某个节点(返回链表节点的data)

  void *List_GetNode(struct list *head,int Index)

  Index---链表节点编号,head---链表头节点

删除链表的某个节点

  int List_Del(struct list *head,int Index)

释放链表

  void List_Free(struct list *head)

通用链表初始化

  1. void *List_Init(void *data)
  2. {
  3. struct list * head;
  4. head = (struct list *)malloc(sizeof(struct list));
  5. head->data=data;
  6. head->next=NULL;
  7. return head;
  8. }

通用链表添加节点到尾部

  1. void List_Add(struct list *head,void *data)
  2. {
  3. struct list *pNode,p1=head;
  4. pNode=(struct list *)malloc(sizeof(struct list ));
  5. while(p1->next != NULL )
  6. { p1=p1->next; } //遍历链表,找到最末尾的节点
  7. p1->next=pNode;
  8. pNode->data=data;
  9. pNode->next=NULL;
  10. }

通用链表获取链表节点个数

  1. int LIST_Count(struct list * head)
  2. {
  3. struct list * p1;
  4. int nCount = 0;
  5. p1=head->next;
  6. while(p1 != NULL)
  7. {
  8. nCount++;
  9. p1=p1->next;
  10. }
  11. return nCount;
  12. }

通用链表释放链表

  1. void *List_Free(struct list *head)
  2. {
  3. struct list *ptr=head;
  4. while(ptr!=NULL)
  5. {
  6. ptr=ptr->next;
  7. free(head->data);//先释放数据存储的内存空间
  8. free(head);//再释放链表节点的内存空间
  9. head=ptr;
  10. }
  11. return head;
  12. }

通用链表示例    

  1. void test()
  2. {
  3. struct list *head;
  4. struct staff *people1,*people2;
  5. //初始化链表
  6. head=List_Init(NULL);//头节点不存储数据,参数为NULL
  7. people1=(struct staff *)malloc(sizeof(struct staff));
  8. people2=(struct staff *)malloc(sizeof(struct staff));
  9. people1->iStaffID=1001;
  10. strcpy(people1->acName,"张三");
  11. strcpy(people1->acPasswd,"123456");
  12. people2->iStaffID=1002;
  13. strcpy(people2->acName,"李四");
  14. strcpy(people2->acPasswd,"123456");
  15. //添加链表节点
  16. List_Add(head,people1);
  17. List_Add(head,people2);
  18. //员工信息打印函数
  19. Staff_Print(head);
  20. }
  1. //员工信息打印函数
  2. void Staff_Print(struct list *head)
  3. {
  4. struct list *p1=head->next;
  5. struct staff *people1;
  6. while(p1 != NULL)
  7. {
  8. people1=p2->data;
  9. printf("%5d%10s%10s\n",people1->iStaffID,
  10. people1->acName,people1->acPasswd);
  11. p1=p1->next;
  12. }
  13. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/645916
推荐阅读
相关标签
  

闽ICP备14008679号