2022年数据结构笔记归类 .pdf
《2022年数据结构笔记归类 .pdf》由会员分享,可在线阅读,更多相关《2022年数据结构笔记归类 .pdf(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、数据结构笔记1 1.线性表文件结构list.h 1./list.h 2.3.#ifndef _LIST_H 4.#define _LIST_H /条件编译语句5.6.#define LIST_INIT_SIZE 10 /线性表初始大小7.#define LIST_INCREMENT 10 /递增大小8.9.typedefstruct10. 11. ElemType * elem; / 线性表首地址12.int length; / 线性表当前使用长度13.int size; / 线性表当前最大长度14.LIST; 15.16.LIST *InitList(); / 初始化17.void Free
2、List(LIST *l); 18.int InsertList(LIST* l,int i,ElemType *e); 19.int DeleteList(LIST *l,int i); 20.21.#endiflist.c 1./list.c 2.#include 3.#include 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 14 页 - - - - - - - - - 4.#include stu.h 5.#include list.h 6.7./ 初始线性
3、表函数8.LIST * InitList() 9. 10. LIST* l=(LIST*)malloc(sizeof(LIST); 11./在堆中动态定义一个线性表结构指针12.if(l=NULL) 13. 14. exit(0); 15. 16.17. l-elem=(ElemType*)malloc(LIST_INIT_SIZE* sizeof(ElemType); 18./在堆中动态开辟数据空间19.if(l-elem=NULL) 20. 21. free(l); 22. exit(0); 23. 24. l-length=0; 25. l-size=LIST_INIT_SIZE; 26
4、.return l; 27. 28.29.void FreeList(LIST *l) 30. 31. free(l-elem); / 释放线性表的成员空间32. free(l); /释放线性表本身33. 34.35.int InsertList(LIST* l,int i,ElemType *e) 36. 37. ElemType *p=NULL,*q=NULL,*newElem=NULL; 38.if(l=NULL|e=NULL) 39.return 0; 40.if(il-length+1) 41.return 0; 42.if(l-length=l-size) 43. 44. newE
5、lem=realloc(l-elem, 45. (l-size+LIST_INCREMENT)*sizeof(ElemType); 46.if(newElem=NULL) 47.return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 14 页 - - - - - - - - - 48. l-elem=newElem; 49. l-size+=LIST_INCREMENT; 50. 51.52. q=&l-elemi-1; /要插入的位置53.for(p=&(l
6、-eleml-length-1);p=q;p-) 54. *(p+1)=*p; / 将 p 往后移一位55. *q=*e; /插入56. +l-length; 57.return 1; 58. 59.60.int DeleteList(LIST *l,int i) 61. 62. ElemType *p=NULL,*q=NULL; 63.if(l=NULL) 64.return 0; 65.if(il-length) 66.return 0; 67. p=&l-elemi-1; 68. q=&l-eleml-length-1; 69.for(;plength; 72.return 1; 73.
7、 stu.h 1./stu.h 2.3.#ifndef _STU_H 4.#define _STU_H 5.6.typedefstruct/定义一个学生的结构体7. 8.char sno5; 9.char name21; 10.char sex3; 11.int score; 12.ElemType; 13.14.#endif名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 14 页 - - - - - - - - - main.c 1./main.c 2.#include
8、 3.#include stu.h 4.#include list.h 5.6.ElemType stu3= 7. s010, 张三 , 男 ,80, 8. s011, 张四 , 男 ,81, 9. s012, 张五 , 男 ,82 10.; 11.12.void main() 13. 14./定义一个线性性表指针15.int i; 16. LIST* list=NULL; 17. list=InitList(); / 初始化线性表18.19.for(i=0;i3;i+) 20. InsertList(list,1,&stui); 21. DeleteList(list,2); 22.23.
9、FreeList(list); 24. 2.单链表文件结构list.h 1./list.h 2.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 14 页 - - - - - - - - - 3.#ifndef _LIST_H 4.#define _LIST_H 5.6.typedefstruct _node 7. /结点类型定义8.void *data; /结点的数据域9.struct _node *next; / 结点的指针域10.NODE; 11.12.typedef
10、struct13. /链表类型定义14. NODE * head;/头结点15. NODE * last;/尾结点16.int length;/ 链表长度17.LIST; 18.19.LIST* InitList(); 20.int AddNode(LIST* l,void *data,int size); 21.NODE* FindNodeByKey(LIST *l,void *key, 22.int (*compare)(void *, void*) /* 函数指针 */ ); 23.NODE* FindNode(LIST *l,void *key, 24.int (*compare)(v
11、oid *, void*) /* 函数指针 */ ,NODE *pre); 25.int DeleteListByKey(LIST* l,void * Key,int (*compare)(void*, void*); 26.27.#endiflist.c 1./list.c 2.3.#include 4.#include 5.#include 6.#include list.h 7.8.LIST* InitList() 9. 10. LIST* l=(LIST*)malloc(sizeof(LIST); / 创建一个链表11.if(l=NULL) 12. exit(0); 13. memse
12、t(l,0,sizeof(LIST);/ 将链表清零14.return l; 15. 16.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 14 页 - - - - - - - - - 17.int AddNode(LIST* l,void *data,int size) /添加结点18. 19./在调试中可以这样看结点的值(struct STU*)l-head-data,(struct STU*)l-last-data 20. NODE *n=NULL; 21.if(l
13、=NULL|data=NULL) 22.return 0; 23. n=(NODE*)malloc(sizeof(NODE); / 创建一个结点24.if(n=NULL) 25.return 0; 26. n-data=data; 27. n-data=malloc(size); 28.if(n-next=NULL) 29. 30. free(n); 31.return 0; 32. 33. memcpy(n-data,data,size);/ 把数据拷到目标结点中去34.35./*/方法一36. if(l-head=NULL) 37. 38. l-head=n; 39. l-last=n;
14、40. l-length=1; 41. 42. else 43. 44. l-last-next=n;/挂在尾结点后面45. l-last=n; /改变尾结点的指向46. l-length+; 47. 48. */49./方法二50.if(l-head=NULL) 51. l-head=n; 52.else53. l-last-next=n; 54. l-last=n; 55. l-length+; 56.return 1; 57. 58.59.NODE* FindNodeByKey(LIST *l,void *key, 60.int (*compare)(void *, void*) /*
15、函数指针 */ ) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 14 页 - - - - - - - - - 61. /查找结点62. NODE *p=NULL; 63.if(l=NULL|key=NULL|compare=NULL) 64.return NULL; 65. p=l-head;/ 让 p 指向第一个结点66.while(p) 67. 68.if(compare(p-data,key)=1)/ 如果返回为1 说明找到了69.return p; 70. p
16、=p-next;/ 否则继续往下找71. 72.return NULL;/都没找到,返回空73. 74.75.76.NODE* FindNode(LIST *l,void *key, 77.int (*compare)(void *, void*) /* 函数指针 */ ,NODE *pre) 78. /查找结点 , 并传入前一个结点的参数79. NODE *p=NULL; 80.if(l=NULL|key=NULL|compare=NULL|pre=NULL) 81.return NULL; 82. p=l-head;/ 让 p 指向第一个结点83. *pre=NULL; 84.while(
17、p) 85. 86.if(compare(p-data,key)=1)/ 如果返回为1 说明找到了87.return p; 88. *pre=p;/ 找到的结点的前一个结点89. p=p-next;/ 否则继续往下找90. 91.return NULL;/都没找到,返回空92. 93.94.int DeleteListByKey(LIST* l,void * key,int (*compare)(void*, void*) 95. /删除结点96. NODE *p=NULL,*q=NULL; 97. p=FindNode(l,key,compare,&q/* 前一个结点的地址*/ ); 98.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年数据结构笔记归类 2022 数据结构 笔记 归类
限制150内