c程序设计(part1)_第1页
c程序设计(part1)_第2页
c程序设计(part1)_第3页
c程序设计(part1)_第4页
c程序设计(part1)_第5页
已阅读5页,还剩89页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、整理课件高级程序设计高级程序设计C+Advanced Programming in C+(part 1)南京大学软件学院郑滔费彝民楼费彝民楼904室室整理课件Alan Mathison TuringJohn von Neumann Alonzo ChurchKurt GodelA man provided with paper, pencil, and rubber, and subject to strict discipline, is in effect a universal Turing Machine.ENIAC:Electronic Numerical Integrator an

2、d Computer EDVAC:Electronic Discrete Variable Automatic ComputerACM 1947 Association of Computing MachineryTuring Award 1966整理课件Algol 60BCPLCAlgol 68ANSI CSimula 67C With ClassC+FORTRAN 90、PL/1CLUAdaJohn Backus,1953, 1977Alan JPerlis ,1958,1966E.W.Dijkstra 1960,1972C. A. R. Hoare1966,1980Niklaus Wir

3、th 1966, 1994Donald E.Knuth 1965,1974Dennis Ritchie Ken Thompson1972,1983Ole-Johan DahlKristen Nygaard1962,2001Barbara Liskov 70, 2008整理课件背景背景1962, Kristen Nygaard(KN), initiated a projectSimulation LanguageUNIVAC选择选择FORTRAN or ALGOL60?the block structuregood programming securityEuropean patriotism入

4、手入手break out of the strict LIFO regime of block instances措施措施A procedure-like activity declarationExplicit process pointers for dynamic naming and referencingA mechanism for accessingmechanisms for the scheduling and sequencing of processes实现实现writing a new runtime system(garbage collector)compiler

5、extensions:block prex“ SIMULA”,兼容Algol60Simula I整理课件思考思考activity/process: useful for programming and system design in generalobjects belonging to different classes having common propertiesClassresume operator is a basic coroutine call defined for whole languageinheriting方法方法morebottom up program des

6、igntop down oriented mechanism through a concept of virtual procedures“garbage collectorforbidding class prefixing across block levelsTony Hoare :abstraction function“OO paradigmSimula 67SmalltalkBETAC+EiffelJAVA整理课件Bjarne StroustrupnDenmark nPh.D, University of CambridgenAT&T Labs, College of E

7、ngineering Chair Professor in Computer Science at Texas A&M UniversitynMember of the National Academy of Engineering in 2004nACM、IEEE Fellownhttp:/ designed and implemented C+整理课件课程内容nC+程序设计基础n C+程序设计 钱能 清华大学出版社nC+程序设计技巧n Effective C+n More Effective C+n Thinking in C+侯捷侯捷整理课件考核方式n机考50%n平时测验40%2

8、10%+20%n作业 10%n笔试50%整理课件参考书nPrinciplenThe Annotated C+ Reference Manual, Margaret A.Eillis and Bjarne Stroustrup, Addison-Wesley, 1990, ISBN 0-201-51459-1nThe Design and Evolution of C+, Bjarne Stroustrup, Addison-Wesley, 1994, ISBN 0-201-54330-3nC+ Primer, Fourth Edition, Stanley B.Lippman, Josee L

9、ajoienProgramming - Principles and Practice Using C+ Bjarne Stroustrup,Addison-Wesley ISBN 978-0321543721,December 2008整理课件参考书nLanguage & Standard LibrarynThe C+ Programming Language (Third Edition), Bjarne Stroustrup, Addison-Wesley, 1998, ISBN 0-201-92488-9nspecial edition 2000nThinking in C+,

10、 Bruce Eckel, Prentice Hall, 1995整理课件参考书nProgrammingnEffective C+, Second Edition: 50 Specific Ways to Improve Your Programs and Designs, Scott Meyers, Addison-Wesley, 1998, ISBN 0-201-92488-9nMore Effective C+: 35 New Ways to Improve Your Programs and Designs, Scott Meyers, Addison-Wesley, ISBN 0-2

11、01-63371-XnC+ Strategies and Tactics, Robert Murray, Addison-Wesley, 1993, ISBN 0-201-56382-7Generic Programming Efficient, Reusable整理课件参考书nSamplesnC+ Programming Style, Tom Cargill, Addison-Wesley, 1992, ISBN 0-201-56365-7nOthersnAdvanced C+: Programming Styles and Idioms, James Coplien, Addison-We

12、sley, 1992, ISBN 0-201-54855-0nDesigning and Coding Reusable C+, Martin D.Carroll and Margaret A.Ellis, Addison-Wesley, 1995, ISBN 0-201-51284-X整理课件参考书nOthersnScientific and Engineering C+, John J.Barton and Lee R.Nackman, Addison-Wesley, 1994, ISBN 0-201-53393-6nDesign Patterns: Elements of Reusabl

13、e Object-Oriented Software, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley, 1995, ISBN 0-201-63361-2整理课件C和C+nC nBCPL (Martin Richards) B (Ken Thompson) C (Dennis Ritchie) nUnix的开发语言 1972nANSI C 1989 Kernighan Ritchie n可应用于多种技术领域和多种平台的开发系统-从最小的嵌入式处理器到最大的超级计算机和数据处理系统n主要特点

14、n语言简洁、紧凑n可直接访问内存,胜任OS的开发n目标代码质量高、运行效率高n具有良好的可移植性整理课件C和C+nC+ n“C with class” Bjarne Stroustrup1980 年 贝尔实验室n1983年取名为C+Rick Mascitti nANSI C+标准草案1994年n主要特点n完全包含C语言成分(C的超集)n添加了OOP的完全支持C+ 支持支持 C 所支持的全部编程技巧所支持的全部编程技巧任何任何 C 程序都能被程序都能被 C+ 用基本相同的方法编写,并具备相同的时间和空间开销用基本相同的方法编写,并具备相同的时间和空间开销整理课件Programmer needs

15、to be trusted与可能出现的错误相比,更重要的是能做什么好的事情整理课件Programming ParadigmnProcedural Paradigmnbased upon the concept of the procedure callnObject-Oriented ParadigmnThe theory of concepts, and models of human interaction with real world phenomenaImperative programming整理课件Programming ParadigmnFunctional Paradigmn

16、Mathematics and the theory of functionsnLisp(atom, list,cons,) app:EmacsnLogicalnAutomatic proofs within artificial intelligencenBased on axioms, inference rules, and queriesnprologdeclarative programmingHaskell 整理课件程序设计范型n结构化程序设计n功能分解、逐步求精n单入口、单出口的三种控制结构n缺点n数据和操作的分离使得程序难以理解n重用性差整理课件程序设计范型n面向对象程序设计n

17、本质n对象将数据和处理数据的过程当成一个整体n封装 vs 信息隐藏n继承 vs 重用n多态性n动态定连(Dynamic Binding)n一名多用、类属整理课件程序定律n结构化程序设计n程序数据结构算法n面向对象程序设计n程序对象对象对象 “消息消息”整理课件结构化程序设计部分整理课件语言的组成n三要素nSyntax、Semantics、PragmaticsnData StructureAlgorithmn数据n名、类型、值、地址、性质n数据处理n操作算术(移位)、逻辑、IOn控制流顺序、选择、重复ADT抽象数据类型BackusNaur FormCFGnat = 0-9+signedNat =

18、 (+|-)?natNumber = signedNat(. nat)?(E signedNat)?letter = a-zA-Zdigit = 0-9identifier = letter (letter|digit)*语用乃语言成分在程序特定执行中的实际效用语用乃语言成分在程序特定执行中的实际效用相对性、动态性、多元性You have a green light“Pragmatics studies the factors that govern our choice of languagein social interaction and the effects of our choic

19、e on others.”整理课件类型n类型系统n静态/动态n弱/强nC+是强类型语言n要求程序设计者在使用之前对数据的类型进行声明n隐式转换 coersionn显式转换 casting整理课件基本数据类型(primitive)nchar、int、float、doublenModifiers: long、short、signed、unsignednchar 只能用signed、unsigned修饰nfloat 不能被修饰ndouble 只能用long修饰nint 可以用4种修饰符组合修饰n省略表示n操作符 sizeofnANSI C+: wchar_t、bool整理课件基本数据类型ntyped

20、efn为已有的类型定义一个同义词typedef double profit ;typedef int INT ;整理课件表达式n操作数、操作符和标点符号组成的序列,表示一个计算过程n优先级a+b*cn结合性a+b-cn求值次序(a+b)*(a-b) 与编译系统有关n类型转换约定 int x=10; float y=2.0; x*y整理课件表达式n计算与类型n相同类型的两个操作数运算后,其结果仍然为该类型的值n类型对计算的限制n类型具有取值范围,超出取值范围的值没有意义n混合类型计算和类型转换n转换总是朝表达能力更强的方向,并且转换总是逐个逐个运算符进行n自动转换( coersion)n显式转换

21、(casting)整理课件表达式n求值次序与副作用n操作数的求值次序在ANSI C+中没有规定,由各编译系统自行决定。n不同的编译系统求值顺序不同int a=3, b=5; c = a*b + +b;n求值次序使交换率失去作用int a=3; c = a+ * +a;n副作用的危害n破坏了可移植性n降低了程序的可读性n消除副作用整理课件表达式n表达式种类n算术表达式n关系和逻辑表达式n赋值表达式n逗号表达式n字位运算符表达式n操作符可重载整理课件表达式n赋值表达式n左值右值表达式n左值:可以出现在赋值表达式左部的表达式, 具有存放数据的空间n类型不同时,先计算右值表达式的值,再转换为左值类型,

22、然后赋值n算术表达式n增量和减量操作符n前增量(前减量) +a ( -a ) 前增量的结果是左值n后增量(后减量) a+ ( a- )n提高编译结果的执行效率整理课件表达式n条件运算符表达式n ? : n唯一的三目运算符n只计算一个运算分量只计算一个运算分量n如果 和的值类型相同,且均为左值,则该条件运算符表达式为左值表达式n可嵌套nsign(x) x 0? 1: x = 0? 0 : -1n就近原则整理课件表达式n逗号表达式n,n的值作为该逗号表达式的值int a, b, c; d = (a = 1, b = a+2, c = b+3);cout d endl;6n如为左值,则该逗号表达式为

23、左值整理课件表达式n字位运算符表达式n对整型数二进制位(bit)的操作,将整型数看作二进制序列n取反 、与 &、或 |、异或 n按位取反 单目n按位与 &双目复位n按位或 |双目置位n异或 双目n与全0的二进制串的运算不变n与全1的二进制串的运算取反n与本身的运算清零n与同一个对象运算两次还原n移位运算符表达式整理课件语句n表达式语句nIO语句n、可重载n控制流语句n顺序、选择、重复iostream整理课件语句nswitchnswitch 后必须是整型表达式,case 后必须是整型常量表达式n值各不相同n各个case(包括default)出现的次序可以任意ncase 仅起标号作

24、用,不改变执行顺序nbreakn多个case可以公用一组语句n通常可以和if语句配合使用enumIO整理课件函数n原则n函数定义不允许嵌套n先定义后使用n函数的执行机制n建立被调用函数的栈空间n保存调用函数的运行状态n参数传递n值传递 (call by value)n引用传递 (call by reference)n保存返回地址n将控制转交被调函数 Call by name Call by value-resultRuntime Environmentvoid p(int x) +i; +x; int a10; int i = 1; a1 = 1; a2 = 2; p(ai);void p(i

25、nt x, int y)+x; +y;int a = 1; p(a,a);Call by name:delayed evaluationCall by value-resultcopy-restore_stdcall _cdecl_fastcall整理课件函数n函数原型n遵循先定义后使用原则n自由安排函数定义的位置n标准库函数的函数原型都在头文件(.h)中提供,用# #includeinclude 指令包含这些原型文件n用户自定义的函数必须在源代码中说明函数原型n函数原型是一条语句n函数原型不必含有参数的名字,只需含有参数的类型n函数原型必须和函数定义在返回值类型、函数名和参数表完全一致,编译

26、器在遇到函数调用时,需检查函数原型整理课件函数n函数重载n原则n一组重载函数,相互间至少在参数个数、类型或顺序上有所不同n返回值类型不作为区别重载函数的依据n匹配重载函数的顺序n严格n内部转换n用户定义的转换void f(long); void f(double); f(10); ambiguousName manglingextern “C”整理课件函数n带默认参数的函数n默认参数的声明n函数原型中给出n先定义的函数中给出n默认参数的顺序规定n从右到左,中间不能间断n默认参数与函数重载void f(int); void f(int, int=2); ambiguous整理课件函数n内联函数i

27、nlinen目的n提高程序的可读性n提高程序的运行效率n对象n使用频率高频率高的小段小段代码n实现方法n编译系统将为 inline 函数创建一段代码,在每次调用时,用相应的代码替换n限制n非递归n由编译系统控制整理课件 函数明智地运用inlining(Use inlining judiciously)n只是对编译系统的提示nBig/complex(loop, switch)/nFunction pointernCompiler (outlined)nstatic functionnrecompile/link vs rebuild缺点n增大目标代码(object code) n病态的换页(th

28、rashing)n降低指令快取装置的命中率(instruction cache hit rate)整理课件程序结构n程序结构n逻辑结构n物理结构n多个源文件组成 n只能其中一个文件含有主函数n工程文件n外部函数n外部变量extern mainf1f2fncpp1cpp2cpp3obj1obj2objnCompilerexeLibLinker整理课件const double pi=3.14;int count=0;void show() . void process() int i=0; while () char c; float salary;staticstaticextern void

29、show();extern float salary;int count=0;void process() salary = 100; for() char c; show();staticstatic#include “a.h”int k=0;static程序级程序级文件级文件级函数级函数级块级块级CodeDataStackHeapnamespaceconst double pi=3.14;#include “const.h”#include “const.h”void main() int x; .; 编译预处理编译预处理常量定义常量定义变量变量/函数声明函数声明内联函数内联函数类型定义类

30、型定义整理课件作用域与生命期n程序的局部性原则n程序的内存结构nCode、Global data、Stack、HeapnScopen全局变量nstaticn局部变量n静态局部变量nstatic(生命期)整理课件程序结构nStatic global variable static n防止名冲突n值可靠nStatic functionn限制使用n对函数定义的补充static nConstantn缺省为 staticn开放 extern整理课件程序结构n标识符在程序中有效的范围n程序级外部量、外部函数n文件级静态函数、静态全局量n函数级标号、形式参数、局部量n块级n可见性n分析在某一位置标识符的有效

31、性n: 引用全局变量nnamespacenusing namespceint global;/程序级void f() /程序级static float f; /文件级static void g() /文件级 void main() int x; /函数级 if (x0) float x=0; /块级 整理课件多文件结构nHeader File声明的部分n宏定义n包含指令n类型定义n函数声明n内联函数n常量定义n变量声明整理课件编译预处理n包含#include “”n宏定义define 字符串 字符串undef宏名字带参数的宏n条件编译#if#else#elif#endif#ifdef#ifnd

32、efn设定编译器状态#pragmaoncewarning(disable:4101)warning(error:160)整理课件数组n特征n相同数据类型n有序n连续存储0n-1n一维数组n类型定义n函数参数n仅传递地址n元素个数需通过参数显示给出,不能通过sizeof 取得n字符串整理课件数组n多维数组n定义T Ac1c2n存储组织n参数传递n只能缺省第一维n降维处理typedef T A1c2;A1 Ac1; 整理课件数组nvoid f(int a, int n);nvoid f(int a3, int n);nchar s1=abc; cout s1;nchar s2=a,b,c; cou

33、t s2;nchar s33;s3 = abc;char s2=a,b,c,0;X整理课件结构和联合nstructn赋值同类型nalignmentn参数传递nunionn共享存储空间_declspec(align(8)#pragma pack(n) 整理课件结构和联合enum FIGURE_TYPE LINE, RECTANGLE, ELLIPSE;struct Line struct Rectangle FIGURE_TYPE t ; FIGURE_TYPE t; int x1, x2, y1, y2; int left, top, right, bottom; ; ;struct Elli

34、pse FIGURE_TYPE t; int x, y, r; ;union FIGUREFIGURE_TYPE t;Line line;Rectangle rect; Ellipse ellipse;例:定义一个能存储例:定义一个能存储100100个图形的数组,个图形的数组,其中的图形可以是:直线、矩形、圆其中的图形可以是:直线、矩形、圆FIGURE figures100;void main() input( figures, 100 ); for (int i=0;i100;i+) draw(figuresi); void draw_line(int,int,int,int);void d

35、raw_rect(int,int,int,int);void draw_ellipse(int,int,int);整理课件结构和联合void draw(FIGURE figure)switch ( figure.t )case LINE: draw_line(figuresi.line.x1, ); break;case RECTANGLE: draw_rect(figuresi.rect.left, ); break;case ELLIPSE:draw_ellipse(figuresi.ellipse.x, ); break;void input (Figure fig, int size)

36、 int t; for (int k=0; k t; switch (t) case LINE: figk.type = LINE; figk.line.x2 figi.line.y2; break; case RECTANGLE: case ELLIPSE: . 多态性多态性整理课件指针 管理地址信息管理地址信息 按地址管理数据按地址管理数据n指针定义与基本操作n指针与函数n指针与数组n指针与结构n多级指针n动态变量整理课件指针定义与基本操作n定义n格式 * int *p; /p为一个指针变量n定义多个指针变量时,每个指针变量前都要有* *int *p,*q; /p和q均为指针变量int *

37、p, q; /p为指针变量,q为整型变量n用 typedef typedef 定义一个指针类型 typedef int* Pointer; Pointer p,q; / p和q均为指针变量n赋值 int *p = 0 x12345678; int *p = (int *)0 x12345678; 整理课件指针定义与基本操作n操作符& & 取地址* * 间接取内容 int x=9; int *p; p = &x;*p = 1000; 9x0X5A12p 0X7B77 0X5A12 1000整理课件指针定义与基本操作n指针指向一个变量: 指针变量的值为另一个变量的地址nPo

38、inter literal 0 0 、NULL NULL n不指向任何对象n初始化初始化!p1000 x整理课件指针定义与基本操作n指针运算n赋值同类型同类型赋值 int x; double d; int *p, *q; p = &x; p = &d; /error q = p;*p = *q;p = NULL;整理课件指针定义与基本操作n+/-+/- 整型值n一个指针值可以与一个整型值进行+/-+/-运算,结果为同类型的指针值int x; int *p;p = p + 1;p = &x + 2;p+; p-;n当一个指针值与一个整型值进行+/-+/-运算时,实际+/-

39、+/-的数值由该指针的基类型决定int *p; double *q;p+; /p的值加4 ( sizeof(int) )q+; /q的值加8 ( sizeof(double) ) 整理课件指针定义与基本操作n同类型指针相减同类型的指针值可以相减,结果为整型值int *p, *q;int offset;offset = p - q;int a10;int *p, *q,offset;p = &a0;q = &a3;offset = p - q; / offset = 3整理课件指针定义与基本操作n同类型指针比较n= 、 !=n一般不使用 、=、=n输出int x=1; int *

40、p=&x;cout p; /p的值 (x的地址)cout *p; / p所指向元素的值特例: char *p=ABCD;cout p; /p指向的字符串,即: ABCDcout *p; /p指向的字符,即:Acout (void *)p /p的值整理课件指针定义与基本操作nvoid * 只管理地址信息 void *p;n任何类型的指针均可以赋值给void void * *void *any_pointer; int x; double y;any_pointer = &x;any_pointer = &y;n不能进行其他指针运算和间接引用,除非做强制类型转换*any_p

41、ointer /error*(int *)any_pointer) /OK*(double *)any_pointer) /OK整理课件指针定义与基本操作例:将某指针所指向的一块内存清零。void memset ( void *pointer, unsigned size) char *p = (char *)pointer; for (int k=0;ksize;k+) *p+ = 0;void memset(void *, int, unsigned);EX: void memcpy(void *des, void *src, unsigned size);整理课件指针定义与基本操作n常量

42、指针与指针常量n常量指针const * const int x=0; int y;const int *p; /p为指向常量的指针(常量指针)int *q;p = &x; /OKq = &y; /OK*p = 1; /Error*q = 2; /Okq = &x; /Error! Why?p = &y; /OK! Why? constantPvariable x(常量)const_cast消除函数副作用整理课件指针定义与基本操作n指针常量 * const 必须在定义时初始化 int x, y;int * const p = &x; /p为指向一个整型变量

43、的指针常量p = &y; /Error*p = 1; /Ok variableP(常量)constant xconst int * const p; ?整理课件指针与函数n指针作为行参n提高传输效率n函数副作用n常量指针nFunction Pointern指向函数的指针double f(int x) int g() void main() double (*fp) (int); / typedef double (*FP)(int); / FP fp; fp = f; fp = &f; (*fp)(10); fp(10); fp = g; /Error整理课件函数指针n函数参数

44、 编写函数,使其能计算任意一元函数在某区间上的定积分 #include double integrate(double (*f)(double),double a, double b) f(x), a , b, double my_func(double x) void main() integrate(sin,0,1); integrate(cos,1,2); integrate(my_func,1,10); 整理课件示例nsortint scompare(const void *elem1, const void *elem2)TStudent *p1 = (TStudent *)elem

45、1;TStudent *p2 = (TStudent *)elem2;return strcmp(p1-name,p2-name);void MySort(void *base, unsigned num, unsigned width, int (*compare)(const void *elem1, const void *elem2) char *A = (char *)base; char *tmp = (char *)malloc(width); for (unsigned i=1;inum;i+) for (unsigned j=0;j0) /Aj Aj+1) memcpy(tm

46、p,A+j*width,width); /int tmp = Aj; memcpy(A+j*width, A+(j+1)*width,width);/Aj = Aj+1; memcpy(A+(j+1)*width,tmp,width); /Aj+1 = tmp; free(tmp);TStudent student = ;int num = sizeof(student)/sizeof(student0);int width = sizeof(student0);MySort(student,num,width,icompare);CALLBACK整理课件指针与数组n数组元素操作n下标表达式n

47、访问效率n数组元素的指针表示法n一维数组n多维数组int b2010; / typedef int A10; A b20; int *q; q = &b00; q = b0; bij *(&b00+i*10+j) *(q+i*10+j) qi*10+jA *p; int (*p)10;p = &b0; p = b;bij *(*(b+i)+j) *(*(p+i)+j) pijint a10; int *p; int i;p = &a0; p = a; ai *(a+i) *(p+i) pi&ai a+i p+i &pi整理课件指针与数组#incl

48、ude int maximum(int grade, int num)int max = 0; for (int k=0;k max)max = gradek; return max;void main()int A24 = 68,69,70,71 , 85,86,87,89; cout “the max grade is” maximum( );sizeof(A)/sizeof(A00)maximum(&A00,2*4);A0,2*4整理课件指针与数组n指针数组 数组中的元素为指针char *s1 = “C+”, “PASCAL”, “FORTRAN”; char s28 = “C+

49、”, “PASCAL”, “FORTRAN”; s1C + + 0P A S C A L 0F O R T R A N 0C+0PASCAL0FORTRAN0s2整理课件指针数组nmain 函数nint main(int argc, char *argv, char *env)nargc: : 参数个数nargv: : 命令行参数nenv: : 环境参数nargc : 3nenv:cmd: setsystem(“test.exe”)C/C+标准int main()int main(int argc, char *argv)整理课件#include #include using namespac

50、e std;void MyPrint(char *s, .) va_list marker; va_start(marker,s); int i=0; char c; while (c=si) != 0) if (c != %) cout c; else i+; switch (c=si) case f: cout va_arg(marker,double); break; case d: cout va_arg(marker,int);break; case c: cout va_arg(marker,char);break; i+; cout xn结构作为函数参数n大块数据传输nconst

51、整理课件多级指针n指针变量所指向的数据类型为指针类型int *pp; /pp为指针变量,指向另一个指针变量(int *)int *p ;int x = 2;p = &x; pp = &p;*p = 100; *pp= 200;n指针变量必须初始化或赋值,否则将会导致运行时刻的严重错误整理课件多级指针n编写一个函数,交换两个字符串void swap(char *p1, char *p2)char *tmp = *p1; *p1 = *p2; *p2 = tmp;void main()char *p1 = abcd;char *p2 = 1234;cout p1 p2 endl;s

52、wap(&p1,&p2);cout p1 p2 endl;整理课件动态变量n动态变量n编译时刻无法确定n在程序运行时刻,动态产生、消亡n通过指向动态变量的指针变量来实现的n分配在堆区(heap)n局部变量也是动态产生、消亡,但在程序运行前,编译程序已经知道它们的存在n动态变量与C+中的静态(static)变量属于不同的范畴,动态变量是程序设计的概念,static 是C+语言的概念整理课件动态变量n动态变量的产生 操作符nnew nnew int *p = new int10;int (*p2)5 = (int (*)5)p;for (int i=0;i10;i+) pi = i

53、+1;for (int j=0;j2;j+) for (int k=0;k5;k+) cout p2jk ; cout endl;typedef int i5Array 5;void main() i5Array *p = new i5Array 2; for (int j=0;j2;j+) for (int k=0;kcontent = some_int_value; p-next = NULL;n链表为空 head = p;n表头插入 p-next = head; head = p;整理课件单链表n表尾插入 NODE *q = head; while (q-next ) q = q-next; q-next = p; n插在链表中某结点(值为a)的后面 NODE *q = head; while (q & q-content != a ) q = q-next; if (q) /存在a p-next = q-next; q-next = p; else / 不存在a cout content != a) q1 = q2; q2 = q2-next; if (q2) /存在a if (!q1) / a是第一个结点 p-next = q2; head = p; else / a不是第一个结点 p-next = q2; q

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论