郝斌老师__数据结构_笔记.doc
《郝斌老师__数据结构_笔记.doc》由会员分享,可在线阅读,更多相关《郝斌老师__数据结构_笔记.doc(48页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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; ilen; +i)printf(%dn, pi);/p2 = -1; /p0 = *p p2 = *(p+2) = *(a+2) = a2/pi
2、就是主函数的aiint main(void)int a5 = 1,2,3,4,5;Show_Array(a, 5); /a等价于&a0, &a0本身就是int *类型/printf(%dn, a2);return 0;Point_1# include int main(void)int * p; /p是个变量名字, int * 表示该p变量只能存储int类型变量的地址int i = 10;int j;/p = &i;j = *p; / 等价于 j = i;printf(i = %d, j = %d, *p = %dn, i, j, *p);/p = 10; /errorreturn 0;Poi
3、nt 2# include int main(void)int * p; /p是个变量名字, int * 表示该p变量只能存储int类型变量的地址int i = 10;int j;p = &i;*p = i; / 等价于 i = i;/j = *p; / 等价于 j = i;printf(i = %d, j = %d, *p = %dn, i, j, *p);return 0;Point3# include void f(int * p) /不是定义了一个名字叫做*p的形参, 而是定义了一个形参,该形参名字叫做p,它的类型是int *p = 100; /int main(void)int i
4、= 9;f(&i);printf(i = %dn, i);return 0;数据结构610笔记# include int main(void)double * p;double x = 66.6;p = &x; /x占8个子节 1个字节是8位, 1个子节一个地址double arr3 = 1.1, 2.2, 3.3;double * q;q = &arr0;printf(%pn, q); /%p实际就是以十六进制输出q = &arr1;printf(%pn, q); return 0;# include void f(int * p);int main(void)int i = 10;f(&i
5、);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 = &i;/ int *p; p = &i;printf(%pn, p);f(&p);printf(%pn, p);return 0;void f(int * q)*q = (int *)0xFFFFFFFF;Malloc1# include # include int main(void)int a5 = 4, 10, 2, 8, 6;int len;printf(请输入你
6、需要分配的数组的长度: len = );scanf(%d, &len);int * pArr = (int *)malloc(sizeof(int) * len);/*pArr = 4; /类似于 a0 = 4;/pArr1 = 10; /类似于a1 = 10;/printf(%d %dn, *pArr, pArr1);/我们可以把pArr当做一个普通数组来使用for (int i=0; ilen; +i)scanf(%d, &pArri);for (i=0; ilen; +i)printf(%dn, *(pArr+i);free(pArr); /把pArr所代表的动态分配的20个字节的内存释
7、放return 0;Memory1# include int f();int main(void)int i = 10;i = f();printf(i = %dn, i);for (i=0; i2000; +i)f();return 0;int f()int j = 20;return j;Memory2# include # include struct Studentint sid;int age;struct Student * CreateStudent(void);void ShowStudent(struct Student *);int main(void)struct Stu
8、dent * ps;ps = CreateStudent();ShowStudent(ps);return 0;void ShowStudent(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 Stu
9、dentint sid;char name200;int age; /分号不能省int main(void)struct Student st = 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;Struct22009年8
10、月26日14:18:02如何使用结构体两种方式:struct Student st = 1000, zhangsan, 20;struct Student * pst = &st;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 = &
11、st;pst-sid = 99; /第二种方式 pst-sid 等价于 (*pst).sid 而(*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分
12、配好了内存f(&st);g2(&st);/printf(%d %s %dn, st.sid, st.name, 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
13、);pst-age = 22;数据结构1013笔记# include # include /包含了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 Ar
14、r * pArr, int val); /追加bool insert_arr(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
15、_arr(struct Arr * pArr);int main(void)struct Arr arr;int val;init_arr(&arr, 6);show_arr(&arr);append_arr(&arr, 1);append_arr(&arr, 10);append_arr(&arr, -3);append_arr(&arr, 6);append_arr(&arr, 88);append_arr(&arr, 11);if ( delete_arr(&arr, 4, &val) )printf(删除成功!n);printf(您删除的元素是: %dn, val);elseprint
16、f(删除失败!n);/*append_arr(&arr, 2);append_arr(&arr, 3);append_arr(&arr, 4);append_arr(&arr, 5);insert_arr(&arr, -1, 99);append_arr(&arr, 6);append_arr(&arr, 7);if ( append_arr(&arr, 8) )printf(追加成功n);elseprintf(追加失败!n);show_arr(&arr);inversion_arr(&arr);printf(倒置之后的数组内容是:n);show_arr(&arr);sort_arr(&arr
17、);show_arr(&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 is_empty(struct Arr * pArr)if (0 = pArr-cnt)return tru
18、e;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);bool append_arr(struct Arr * pArr, int val)/满是返回falseif ( i
19、s_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-pBasei+1 = pArr-pBasei;pArr-pBasepos-1 = val;(pArr-cnt)+;ret
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 老师 _ 数据结构 笔记
限制150内