收藏 分享(赏)

郝斌老师__数据结构_笔记.doc

上传人:buyk185 文档编号:6213471 上传时间:2019-04-02 格式:DOC 页数:40 大小:446.50KB
下载 相关 举报
郝斌老师__数据结构_笔记.doc_第1页
第1页 / 共40页
郝斌老师__数据结构_笔记.doc_第2页
第2页 / 共40页
郝斌老师__数据结构_笔记.doc_第3页
第3页 / 共40页
郝斌老师__数据结构_笔记.doc_第4页
第4页 / 共40页
郝斌老师__数据结构_笔记.doc_第5页
第5页 / 共40页
点击查看更多>>
资源描述

1、数据结构 15 笔记Array_point 1# include int main(void)int a5 = 1,2,3,4,5;/a3 = *(3+a);printf(“%pn“, a+1);printf(“%pn“, a+2);printf(“%dn“, *a+3); /*a+3 等价于 a0+3return 0;Array_point 2# include void Show_Array(int * p, int len)int i = 0;for (i=0; iint main(void)int * p; /p 是个变量名字 , int * 表示该 p 变量只能存储 int 类型变量

2、的地址int i = 10;int j;/ p = j = *p; / 等价于 j = i;printf(“i = %d, j = %d, *p = %dn“, i, j, *p);/p = 10; /errorreturn 0;Point 2# include int main(void)int * p; /p 是个变量名字 , int * 表示该 p 变量只能存储 int 类型变量的地址int i = 10;int j;p = *p = i; / 等价于 i = i;/ j = *p; / 等价于 j = i;printf(“i = %d, j = %d, *p = %dn“, i, j,

3、 *p);return 0;Point3# include void f(int * p) /不是定义了一个名字叫做 *p 的形参, 而是定义了一个形参,该形参名字叫做p,它的类型是 int *p = 100; /int main(void)int i = 9;f(printf(“i = %dn“, i);return 0;数据结构 610 笔记# include int main(void)double * p;double x = 66.6;p = /x 占 8 个子节 1 个字节是 8 位, 1 个子节一个地址double arr3 = 1.1, 2.2, 3.3;double * q;

4、q = printf(“%pn“, q); /%p 实际就是以十六进制输出q = printf(“%pn“, q); return 0;# include void f(int * p);int main(void)int i = 10;f(printf(“i = %dn“, i);return 0;void f(int * p)*p = 99;# include void f(int * q);int main(void)int i = 9;int * p = / int *p; p = printf(“%pn“, p);f(printf(“%pn“, p);return 0;void f(

5、int * q)*q = (int *)0xFFFFFFFF;Malloc1# include # include int main(void)int a5 = 4, 10, 2, 8, 6;int len;printf(“请输入你需要分配的数组的长度: len = “);scanf(“%d“, int * pArr = (int *)malloc(sizeof(int) * len);/ *pArr = 4; /类似于 a0 = 4;/ pArr1 = 10; /类似于 a1 = 10;/ printf(“%d %dn“, *pArr, pArr1);/我们可以把 pArr 当做一个普通数组

6、来使用for (int i=0; iint f();int main(void)int i = 10;i = f();printf(“i = %dn“, i);for (i=0; i# include struct Studentint sid;int age;struct Student * CreateStudent(void);void ShowStudent(struct Student *);int main(void)struct Student * ps;ps = CreateStudent();ShowStudent(ps);return 0;void ShowStudent(

7、struct Student * pst)printf(“%d %dn“, pst-sid, pst-age);struct Student * CreateStudent(void)struct Student * p = (struct Student *)malloc(sizeof(struct Student);p-sid = 99;p-age = 88;return p;Struct1# include # include struct Studentint sid;char name200;int age; /分号不能省int main(void)struct Student st

8、 = 1000, “zhangsan“, 20;printf(“%d %s %dn“, st.sid, st.name, st.age);st.sid = 99;/st.name = “lisi“; /errorstrcpy(st.name, “lisi“);st.age = 22;printf(“%d %s %dn“, st.sid, st.name, st.age);/printf(“%d %s %dn“, st); /errorreturn 0;Struct2/*2009 年 8 月 26 日 14:18:02如何使用结构体两种方式:struct Student st = 1000, “

9、zhangsan“, 20;struct Student * pst = 1.st.sid2. pst-sidpst 所指向的结构体变量中的 sid 这个成员*/# include # include struct Studentint sid;char name200;int age; /分号不能省int main(void)struct Student st = 1000, “zhangsan“, 20;/st.sid = 99; /第一种方式struct Student * pst;pst = pst-sid = 99; /第二种方式 pst-sid 等价于 (*pst).sid 而(*

10、pst).sid 等价于 st.sid, 所以 pst-sid 等价于 st.sidreturn 0;Struct3# include # include struct Studentint sid;char name200;int age; /分号不能省void f(struct Student * pst);void g(struct Student st);void g2(struct Student *pst);int main(void)struct Student st; /已经为 st 分配好了内存f(g2(/printf(“%d %s %dn“, st.sid, st.name

11、, st.age);return 0;/这种方式耗内存 耗时间 不推荐void g(struct Student st)printf(“%d %s %dn“, st.sid, st.name, st.age);void g2(struct Student *pst)printf(“%d %s %dn“, pst-sid, pst-name, pst-age);void f(struct Student * pst)(*pst).sid = 99;strcpy(pst-name, “zhangsan“);pst-age = 22;数据结构 1013 笔记# include # include /

12、包含了 malloc 函数# include /包含了 exit 函数/定义了一个数据类型,该数据类型的名字叫做 struct Arr, 该数据类型含有三个成员,分别是 pBase, len, cntstruct Arrint * pBase; /存储的是数组第一个元素的地址int len; /数组所能容纳的最大元素的个数int cnt; /当前数组有效元素的个数;void init_arr(struct Arr * pArr, int length); /分号不能省bool append_arr(struct Arr * pArr, int val); /追加bool insert_arr(

13、struct Arr * pArr, int pos, int val); / pos 的值从 1 开始bool delete_arr(struct Arr * pArr, int pos, int * pVal);int get();bool is_empty(struct Arr * pArr);bool is_full(struct Arr * pArr);void sort_arr(struct Arr * pArr);void show_arr(struct Arr * pArr); void inversion_arr(struct Arr * pArr);int main(voi

14、d)struct Arr arr;int val;init_arr(show_arr(append_arr(append_arr(append_arr(append_arr(append_arr(append_arr(if ( delete_arr(printf(“您删除的元素是: %dn“, val);elseprintf(“删除失败!n“);/* append_arr(append_arr(append_arr(append_arr(insert_arr(append_arr(append_arr(if ( append_arr(elseprintf(“追加失败!n“);*/show_ar

15、r(inversion_arr(printf(“倒置之后的数组内容是:n“);show_arr(sort_arr(show_arr(/printf(“%dn“, arr.len);return 0;void init_arr(struct Arr * pArr, int length)pArr-pBase = (int *)malloc(sizeof(int) * length);if (NULL = pArr-pBase)printf(“动态内存分配失败!n“);exit(-1); /终止整个程序elsepArr-len = length;pArr-cnt = 0;return;bool i

16、s_empty(struct Arr * pArr)if (0 = pArr-cnt)return true;elsereturn false;bool is_full(struct Arr * pArr)if (pArr-cnt = pArr-len)return true;elsereturn false;void show_arr(struct Arr * pArr)if ( is_empty(pArr) ) printf(“数组为空!n“);elsefor (int i=0; icnt; +i)printf(“%d “, pArr-pBasei); /int *printf(“n“);

17、bool append_arr(struct Arr * pArr, int val)/满是返回 falseif ( is_full(pArr) )return false;/不满时追加pArr-pBasepArr-cnt = val; (pArr-cnt)+;return true;bool insert_arr(struct Arr * pArr, int pos, int val)int i;if (is_full(pArr)return false;if (pospArr-cnt+1) /return false;for (i=pArr-cnt-1; i=pos-1; -i)pArr-

18、pBasei+1 = pArr-pBasei;pArr-pBasepos-1 = val;(pArr-cnt)+;return true;bool delete_arr(struct Arr * pArr, int pos, int * pVal)int i;if ( is_empty(pArr) )return false;if (pospArr-cnt)return false;*pVal = pArr-pBasepos-1;for (i=pos; icnt; +i)pArr-pBasei-1 = pArr-pBasei;pArr-cnt-;return true;void inversi

19、on_arr(struct Arr * pArr)int i = 0;int j = pArr-cnt-1;int t;while (i pBasei;pArr-pBasei = pArr-pBasej;pArr-pBasej = t;+i;-j;return;void sort_arr(struct Arr * pArr)int i, j, t;for (i=0; icnt; +i)for (j=i+1; jcnt; +j)if (pArr-pBasei pArr-pBasej)t = pArr-pBasei;pArr-pBasei = pArr-pBasej;pArr-pBasej = t

20、;数据结构 1422 笔记插入节点r = p-pNext; p-pNext = q; q-pNext = r;q-pNext = p-pNext; p-pNext = q;删除一个节点r = p-pNext;p-pNext = p-pNext-pNext;free(r);数据结构 2328 笔记List 上课敲的程序# include # include # include typedef struct Nodeint data; /数据域struct Node * pNext; /指针域NODE, *PNODE; /NODE 等价于 struct Node PNODE 等价于 struct

21、Node */函数声明PNODE create_list(void);void traverse_list(PNODE pHead);bool is_empty(PNODE pHead);int length_list(PNODE);bool insert_list(PNODE, int, int); /在 pHead 所指向链表的第 pos 个节点的前面插入一个新的结点,该节点的值是 val, 并且 pos 的值是从 1 开始bool delete_list(PNODE, int, int *);void sort_list(PNODE);int main(void)PNODE pHead

22、= NULL; /等价于 struct Node * pHead = NULL;int val;pHead = create_list(); /create_list()功能:创建一个非循环单链表,并将该链表的头结点的地址付给 pHeadtraverse_list(pHead);/insert_list(pHead, -4, 33);if ( delete_list(pHead, 4, elseprintf(“删除失败!您删除的元素不存在!n“);traverse_list(pHead);/int len = length_list(pHead);/printf(“链表的长度是%dn“, le

23、n);/sort_list(pHead);/traverse_list(pHead);/* if ( is_empty(pHead) )printf(“链表为空!n“);elseprintf(“链表不空!n“);*/return 0;PNODE create_list(void)int len; /用来存放有效节点的个数int i;int val; /用来临时存放用户输入的结点的值/分配了一个不存放有效数据的头结点PNODE pHead = (PNODE)malloc(sizeof(NODE);if (NULL = pHead)printf(“分配失败, 程序终止!n“);exit(-1);P

24、NODE pTail = pHead;pTail-pNext = NULL;printf(“请输入您需要生成的链表节点的个数: len = “);scanf(“%d“, for (i=0; idata = val;pTail-pNext = pNew;pNew-pNext = NULL;pTail = pNew;return pHead;void traverse_list(PNODE pHead)PNODE p = pHead-pNext;while (NULL != p)printf(“%d “, p-data);p = p-pNext;printf(“n“);return;bool is

25、_empty(PNODE pHead)if (NULL = pHead-pNext)return true;elsereturn false;int length_list(PNODE pHead)PNODE p = pHead-pNext;int len = 0;while (NULL != p)+len;p = p-pNext;return len;void sort_list(PNODE pHead)int i, j, t;int len = length_list(pHead);PNODE p, q;for (i=0,p=pHead-pNext; ipNext)for (j=i+1,q

26、=p-pNext; jpNext)if (p-data q-data) /类似于数组中的: ai ajt = p-data;/类似于数组中的: t = ai;p-data = q-data; /类似于数组中的: ai = aj;q-data = t; /类似于数组中的: aj = t;return;/在 pHead 所指向链表的第 pos 个节点的前面插入一个新的结点,该节点的值是 val, 并且pos 的值是从 1 开始bool insert_list(PNODE pHead, int pos, int val)int i = 0;PNODE p = pHead;while (NULL!=p

27、 +i;if (ipos-1 | NULL=p)return false;PNODE pNew = (PNODE)malloc(sizeof(NODE);if (NULL = pNew)printf(“动态分配内存失败!n“);exit(-1);pNew-data = val;PNODE q = p-pNext;p-pNext = pNew;pNew-pNext = q;return true;bool delete_list(PNODE pHead, int pos, int * pVal)int i = 0;PNODE p = pHead;while (NULL!=p-pNext +i;i

28、f (ipos-1 | NULL=p-pNext)return false;PNODE q = p-pNext;*pVal = q-data;/删除 p 节点后面的结点p-pNext = p-pNext-pNext;free(q);q = NULL;return true;课下老师又加了注释的程序# include # include # include typedef struct Nodeint data; /数据域struct Node * pNext; /指针域NODE, *PNODE; /NODE 等价于 struct Node PNODE 等价于 struct Node */函数声

29、明PNODE create_list(void); /创建链表void traverse_list(PNODE pHead); /遍历链表bool is_empty(PNODE pHead); /判断链表是否为空int length_list(PNODE); /求链表长度bool insert_list(PNODE pHead, int pos, int val); /在 pHead 所指向链表的第 pos 个节点的前面插入一个新的结点,该节点的值是 val, 并且 pos 的值是从 1 开始bool delete_list(PNODE pHead, int pos, int * pVal);

30、 /删除链表第 pos 个节点,并将删除的结点的值存入 pVal 所指向的变量中, 并且 pos 的值是从 1 开始void sort_list(PNODE); /对链表进行排序int main(void)PNODE pHead = NULL; /等价于 struct Node * pHead = NULL;int val;pHead = create_list(); /create_list()功能:创建一个非循环单链表,并将该链表的头结点的地址付给 pHeadtraverse_list(pHead);/insert_list(pHead, -4, 33);if ( delete_list(

31、pHead, 4, elseprintf(“删除失败!您删除的元素不存在!n“);traverse_list(pHead);/int len = length_list(pHead);/printf(“链表的长度是%dn“, len);/sort_list(pHead);/traverse_list(pHead);/* if ( is_empty(pHead) )printf(“链表为空!n“);elseprintf(“链表不空!n“);*/return 0;PNODE create_list(void)int len; /用来存放有效节点的个数int i;int val; /用来临时存放用户

32、输入的结点的值/分配了一个不存放有效数据的头结点PNODE pHead = (PNODE)malloc(sizeof(NODE);if (NULL = pHead)printf(“分配失败, 程序终止!n“);exit(-1);PNODE pTail = pHead;pTail-pNext = NULL;printf(“请输入您需要生成的链表节点的个数: len = “);scanf(“%d“, for (i=0; idata = val;pTail-pNext = pNew;pNew-pNext = NULL;pTail = pNew;return pHead;void traverse_l

33、ist(PNODE pHead)PNODE p = pHead-pNext;while (NULL != p)printf(“%d “, p-data);p = p-pNext;printf(“n“);return;bool is_empty(PNODE pHead)if (NULL = pHead-pNext)return true;elsereturn false;int length_list(PNODE pHead)PNODE p = pHead-pNext;int len = 0;while (NULL != p)+len;p = p-pNext;return len;void so

34、rt_list(PNODE pHead)int i, j, t;int len = length_list(pHead);PNODE p, q;for (i=0,p=pHead-pNext; ipNext)for (j=i+1,q=p-pNext; jpNext)if (p-data q-data) /类似于数组中的: ai ajt = p-data;/类似于数组中的: t = ai;p-data = q-data; /类似于数组中的: ai = aj;q-data = t; /类似于数组中的: aj = t;return;/在 pHead 所指向链表的第 pos 个节点的前面插入一个新的结点

35、,该节点的值是 val, 并且pos 的值是从 1 开始bool insert_list(PNODE pHead, int pos, int val)int i = 0;PNODE p = pHead;while (NULL!=p +i;if (ipos-1 | NULL=p)return false;/如果程序能执行到这一行说明 p 已经指向了第 pos-1 个结点,但第 pos-1 个节点是否存在无所谓/分配新的结点PNODE pNew = (PNODE)malloc(sizeof(NODE);if (NULL = pNew)printf(“动态分配内存失败!n“);exit(-1);pN

36、ew-data = val;/将新的结点存入 p 节点的后面PNODE q = p-pNext;p-pNext = pNew;pNew-pNext = q;return true;bool delete_list(PNODE pHead, int pos, int * pVal)int i = 0;PNODE p = pHead;while (NULL!=p-pNext +i;if (ipos-1 | NULL=p-pNext)return false;/如果程序能执行到这一行说明 p 已经指向了第 pos-1 个结点,并且第 pos 个节点是存在的PNODE q = p-pNext; /q

37、指向待删除的结点*pVal = q-data; /删除 p 节点后面的结点p-pNext = p-pNext-pNext;/释放 q 所指向的节点所占的内存free(q);q = NULL;return true;数据结构 2934 笔记# include # include void f(int k)int m;double * q = (double *)malloc(200);int main(void)int i = 10;int * p = (int *)malloc(100);return 0;Stack# include # include # include typedef s

38、truct Nodeint data;struct Node * pNext;NODE, * PNODE;typedef struct StackPNODE pTop;PNODE pBottom;STACK, * PSTACK; /PSTACK 等价于 struct STACK *void init(PSTACK);void push(PSTACK, int );void traverse(PSTACK);bool pop(PSTACK, int *);void clear(PSTACK pS);int main(void)STACK S; /STACK 等价于 struct Stackint

39、 val;init( /目的是造出一个空栈push( /压栈push(push(push(push(push(traverse( /遍历输出clear(/traverse( /遍历输出if ( pop(elseprintf(“出栈失败!n“);traverse( /遍历输出return 0;void init(PSTACK pS)pS-pTop = (PNODE)malloc(sizeof(NODE);if (NULL = pS-pTop)printf(“动态内存分配失败!n“);exit(-1);elsepS-pBottom = pS-pTop;pS-pTop-pNext = NULL; /

40、pS-Bottom-pNext = NULL;void push(PSTACK pS, int val)PNODE pNew = (PNODE)malloc(sizeof(NODE);pNew-data = val;pNew-pNext = pS-pTop; /pS-Top 不能改成 pS-BottompS-pTop = pNew;return;void traverse(PSTACK pS)PNODE p = pS-pTop;while (p != pS-pBottom)printf(“%d “, p-data);p = p-pNext;printf(“n“);return;bool emp

41、ty(PSTACK pS)if (pS-pTop = pS-pBottom)return true;elsereturn false;/把 pS 所指向的栈出栈一次,并把出栈的元素存入 pVal 形参所指向的变量中,如果出栈失败,返回 false,否则返回 truebool pop(PSTACK pS, int * pVal)if ( empty(pS) ) /pS 本身存放的就是 S 的地址return false;elsePNODE r = pS-pTop;*pVal = r-data;pS-pTop = r-pNext;free(r);r = NULL;return true;/clea

42、r 清空void clear(PSTACK pS)if (empty(pS)return;elsePNODE p = pS-pTop;PNODE q = NULL;while (p != pS-pBottom)q = p-pNext;free(p);p = q;pS-pTop = pS-pBottom;数据结构 4553 笔记1+2+100 的和用递归实现# include long sum(int n)if (1 = n)return 1;elsereturn n + sum(n-1);int main(void)printf(“%ldn“, sum(100);return 0;不同函数间的

43、相互调用# include void f();void g();void k();void f()printf(“FFFFn“);g();printf(“1111n“);void g()printf(“GGGGn“);k();printf(“2222n“);void k()printf(“KKKKn“);int main(void)f();return 0;阶乘的递归实现# include /假定 n 的值是 1 或大于 1 的值long f(long n)if (1 = n)return 1;elsereturn f(n-1) * n;int main(void)printf(“%ldn“,

44、 f(100);return 0;结成的循环使用# include int main(void)int val;int i, mult=1;printf(“请输入一个数字: “);printf(“val = “);scanf(“%d“, for (i=1; i# include typedef struct Queueint * pBase;int front;int rear;QUEUE; void init(QUEUE *);bool en_queue(QUEUE *, int val); /入队void traverse_queue(QUEUE *);bool full_queue(QU

45、EUE *);bool out_queue(QUEUE *, int *);bool emput_queue(QUEUE *);int main(void)QUEUE Q;int val;init(en_queue(en_queue(en_queue(en_queue(en_queue(en_queue(en_queue(en_queue(traverse_queue(if ( out_queue(elseprintf(“出队失败!n“);traverse_queue(return 0;void init(QUEUE *pQ)pQ-pBase = (int *)malloc(sizeof(in

46、t) * 6);pQ-front = 0;pQ-rear = 0;bool full_queue(QUEUE * pQ)if ( (pQ-rear + 1) % 6 = pQ-front )return true;elsereturn false;bool en_queue(QUEUE * pQ, int val)if ( full_queue(pQ) )return false;elsepQ-pBasepQ-rear = val;pQ-rear = (pQ-rear+1) % 6;return true;void traverse_queue(QUEUE * pQ)int i = pQ-front;while (i != pQ-rear)printf(“%d “, pQ-pBasei);i = (i+1) % 6;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 网络科技 > 数据结构与算法

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报