Class Notes of Data Structures Dr. David B. Y. Tsai Feb., 2009 IM-2B Part Two Preliminaries of Algorithmic Notation Variable Names – 1. Using capital letters, as in MAX or Data. Single-letter names of variables used as counters or subscripts will also be capitalized. 2. Variable Declaration: TYPE Variable, such as integer MAX 3. Local and Global Variables: (1) A computer program may contain a main program and various subprograms has led to the notation of local variables and Global variables. (2) A subprogram contains its own list of variables, call local variables. (3) Subprograms may contain parameters, variables which transfer data between a subprogram and its calling program. Assignment Statement – Using dots-equal notation. Max := DATA[1] Input and Output – 1. 2. Using a Read statement to input and assign data to variables, such as Read : Variable names. Data in variables is output by using Write or Print statement with the form: Write: Messages and/or variable names. Procedures – They will be used for an independent algorithmic module which solves a particular problem. Call SWITCH(DataA, DataB) : Procedure: SWITCH (DA, DB) 1. Set TEMP:= DA, DA:=DB and DB:= TEMP. 2. Return. Control Structures 1. Sequential flow 2. Conditional flow – [End of If structure] (1) Single alternative If condition, then: [Module A] [End of If structure.] (2) Double alternative If condition, then: [Module A] Else: [Module B] [End of If structure.] (3) Multiple alternative If condition(1), then: [Module A1] Class Notes of Data Structures Dr. David B. Y. Tsai Feb., 2009 IM-2B Else If condition(1), then: [Module A2] : Else If condition(M), then: [Module AM] Else: [Module B] [End of If structure.] 3. Repetitive flow Repeat for K = R to S by T: [Module] [End of loop.] Repeat while condition: [Module] [End of loop.] EXAMPLE: Algorithm: to find the location LOC and the value MAX of the largest element of DATA. 1. Set K:=1, LOC:=1 and MAX:=DATA[1] 2. Repeat Step 3 and 4 while K ≦ N: 3. If MAX < DATA[k], then: Set LOC:=K and MAX:=DATA[k]. [End of If Struture.] 4. Set K:=K+1. [End of Step 2 loop.] 5. Write: LOC, MAX. 6. Exit. Space Allocation 1. Space requirement P := new(space size) [pointer variable P] Example: integer *P [to declare a pointer variable P] P:= new(integer) [to allocate an integer space whose address is stored into P variable] *P:=10 [variable P point a space which contains data 10] 2. Space release delete(p) [variable p contains the beginning address of space to be deleted]