【精品數(shù)據(jù)結(jié)構(gòu)】preface_第1頁(yè)
【精品數(shù)據(jù)結(jié)構(gòu)】preface_第2頁(yè)
【精品數(shù)據(jù)結(jié)構(gòu)】preface_第3頁(yè)
【精品數(shù)據(jù)結(jié)構(gòu)】preface_第4頁(yè)
【精品數(shù)據(jù)結(jié)構(gòu)】preface_第5頁(yè)
已閱讀5頁(yè),還剩48頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Data Structures,Algorithms,and Applications in C+,Chen Peipei March 13, 2003,Chapter 1,preface,The purpose and contents of the course,Introduce most used data structures and algorithms Prerequisite of other courses Introduce algorithm analysis Review C+,The purpose and contents of the course,1. Intr

2、oduce most used data structures and algorithms. Use proper data structures to solve different problems. Example : Game problem Management of library catalogue by computer Management of the traffic lights in intersections,The purpose and contents of the course,Example 1: Game problem:,Next step: x ha

3、s five choices.,The purpose and contents of the course,Example 1 uses a tree structure.,The purpose and contents of the course,Example 2: Management of library catalogue by computer,It is a linear list.,D.S. Sartaj Sahni 000001 computer 2000.1,The purpose and contents of the course,Example 3: Manage

4、ment of the traffic lights in intersections,C, E are one-way road, there are 13 path to go. Can go at the same time : AB EC Cannot go at the same time: EB AD,This is a graph,The purpose and contents of the course,2 . Prerequisite of other courses: Principles of compiling : use stack to compute expre

5、ssion and implement recursive procedure Operating System: use queue to implement job scheduling Database: use B+ tree to organize, store and load massive data in the hard memory. .,The purpose and contents of the course,3. Basic methods of algorithm analysis standards of the performance of an algori

6、thm: time complexity, space complexity, and accuracy 4. Review C+,What is Data Structure,1. Data is the carrier of information. Data is a set of numbers , characters,and other symbols that can be used to describe the objective things. These symbols can be input into computers , identified and proces

7、sed by the computer program.,What is Data Structure,Data can divided into two classes: numerical data: int, float, complex, non-numerical data: character, string, graph, voice,What is Data Structure,. Data structure A data structure is a data object together with the relationships among the data mem

8、bers that compose the object Data_Structure=D,R is a data object, R is a limited set of relationships of all the data members in D.,What is Data Structure,Linear structure Data structure Non-linear stucture,ADT and OO,1. Data type Definition: is a set of values together with a operation set that ope

9、rate on these values. Example: int type value set: ,-2,-1,0,1,2, operation set: +, -, *, /, %, ,ADT and OO,Most of the programming languages provide a group of predefined data type. Atom data type int, float, double Structure data typearray, struct,ADT and OO,2. ADTs: Abstract Data Types Abstract: i

10、s a method used to hide the information. Example: int x ; float x, y: : : x=735; x=x*y+3.0; : : abstract of int data type abstract of float data type assignment operation,ADT and OO,Abstract data type: is a new programming method that part the usage and implementation, in order to encapsulate and hi

11、de the information.,ADT NaturalNunber is objects: 一個(gè)整數(shù)的有序子集合,它開(kāi)始于0,結(jié)束于機(jī)器能表示的最大整數(shù)(MAXINT)。 Function: Zero( ):NaturalNumber IsZero(x):Boolean Add(x,y):NaturalNumber Equal(x,y):Boolean Successor(x):NaturalNumber Subtract(x,y):NaturalNumber end NaturalNumber,ADT and OO,3. OO: object-orientedobjectclassi

12、nherit communicate object=attribute values+ operates class: objects of same attributes and operates. an instance is an object of the class. different object has deferent attribute value,ADT and OO,Inherit: base classintegrate the same part (including attributes and operations) in all the derived cla

13、sses derived classadd the specific attributes and operations to the base class Example: base classpolygon derivedclassquadrilateral,triangular,ADT and OO,Communication: each class object communicate with others using messages. Message: instructions that one class object used in order to require anot

14、her class object to perform some operation.,Algorithm definition,Algorithm : an operation sequence of soluting a problem Characteristic: 1.finite 2. deterministic 3. initial action 4. sequence ends,Algorithm definition,Program: 1. Program is written by languages that can be performed by machine. Alg

15、orithm has multiple descriptive methods, such as language, graph, table. 2.program cant satisfy the finiteness. For example, OS.,Templates in C+,Problem: program1.1: int Abc(int a,int b,int c) return a+b+b*c+(a+b-c)/(a+b)+4; program1.2: float Abc(float a,float b,float c) return a+b+b*c+(a+b-c)/(a+b)

16、+4; ,Templates in C+,program1.1 and 1.2 differ only in the data type of the formal parameters and of the value returned. We can write a generic code in which the data type is a variable whose value is determined by the compiler.This generic code is written using the template statement in program1.3,

17、Templates in C+,Program1.3 template T Abc(T a,T b,T c) return a+b+b*c+(a+b-c)/(a+b)+4; From this generic code the compiler can construct 1.1 by substituting int for T and 1.2 by substituting float for T.,Parameter passing,1) Value Parameters (Program 1.1) template T Abc (T a, T b, T c ) return a+b+b

18、*c+(a+b-c)/(a+b)+4; z=Abc(2,x,y);,Parameter passing,2) Reference Parameters templateT Abc (T ,Recursive Function,1) direct recursive 2) indirect recursive Example 1 factorial function f(n)=n! 1 n1 (recursive component) f(5)=5f(4)=5*4f( 3)=5*4*3f(2)=5*4*3*2f(1)=120,f(n)=,Recursive Function,int factor

19、ial (int n) if (n=1) return 1; else return n*Factorial(n-1); ,Recursive Function,Example 2: computes the sum of the elements a0 through an-1 (Program 1.9) a0, a1, , an-2, an-1,Recursive Function,Template T Rsum(T a, int n) if (n0) return Rsum(a,n-1)+an-1; return 0; ,Recursive Function,Example 3: Per

20、mutation (Program 1-10) a,b,c : abc, acb, bac, bca, cab, cba permutation of n elements has n!,Recursive Function,Template void perm(T list , int k, int m) int i; if (k=m) for (i=0; i=m; i+) cout listi; cout endl; else for (i=k; i=m; i+) swap(listk, listi); perm(list, k+1, m); swap(listk, listi); ,Dy

21、namic Memory Allocation,1. New int *y = new int ; *y = 10; int *y = new int(10); Delete delete y ; delete x;,Dynamic Memory Allocation,2.Exception Handling float *x = new float n; an exception occurs float * x; try x=new floatn; catch (xalloc) cerr“out of Memory”endl; exit(1);,Dynamic Memory Allocat

22、ion,3. One-dimensional Arrays float * x = new float n;,Dynamic Memory Allocation,4. Two-Dimensional Arrays Use dynamically allocated two-dimensional arrays x00 x01 x02 x03 x04 x10 x11 x12 x13 x14 x20 x21 x22 x23 x24 T * x ;,Dynamic Memory Allocation,memory structure for a three by five array,0 1 2 3

23、 4,X0,X1,X2,Dynamic Memory Allocation,Program 1.13 template void Make2DArray (T * ,Other concept C+,Class Constructor Member function Operator overloading Friends of a class,Example,void selectsort(int a , const int n) for ( int i=0; in-1; i+) int k=i; for ( int j=i+1; jn; j+) if ( ajak) k=j; int te

24、mp=ai; ai=ak;ak=temp; ,Example,# ifndef DATALIST_H # define DATALIST_H # include template class datalist private: Type * Element; int ArraySize; void Swap(const int mark1,const int mark2); int MaxKey (const int low,const int high);,Example,public: datalist(int size=10):ArraySize(size),Element(new TypeSize) datalist( )delete Element; void Sort( ); friend ostream # endif,Example,# ifndef SELECTTM-H # define SELECTTM

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論