数据结构与算法分析 C++语言描述(第2版)Larry Nyhoff 抽象数据类型 PPT
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Data Structures and Abstract Data Types
Chapter 3
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
• Name of the array is a pointer constant
– The base address
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
• Consider multiple pages of the student grade book
const int NUM_ROWS = 10, NUM_COLS = 5, NUM_RANKS = 10; typedef double ThreeDimArray[NUM_ROWS][NUM_COLS][NUM_RANKS];
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
9
Multidimensional Arrays
8
Multidimensional Arrays
• Consider a table of test scores for several different students
• Two-dimensional array
– Syntax
ElementType arrayName [numRows][numCols]
Leabharlann Baidu
15
The new Operator
• Syntax for arrays new Type [capacity] • This command issues a run-time request for a block of memory
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
2
Arrays
• Collection of data elements
13
Multidimensional Arrays as Parameters
• Must specify
– The type and name of the array (a pointer to the base address) – The number of rows – The number of columns (or the length of each row)
• An array of arrays
– An array whose elements are other arrays
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
b[5]
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
4
Character Arrays
– All of same type – Each accessed by specifying position
• Static array
– Compiler determines how memory allocated
• Dynamic array
– Allocation takes place at run time
• Elements stored in rowwise order • Also called column major order
location [0][4] is followed in memory by location [1][0]
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
3
Single Dimension Arrays
• Most C++ compilers do not by default check indices for out of range • Results of out of range array access
– Program can exceed allowed memory area – Program can give puzzling results
• Syntax:
ElementType arrayName [CAPACITY]; ElementType arrayName [CAPACITY] = { initializer_list };
• Example:
int b [10];
• Elements accessed by
– name and [ ] operation
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
12
Memory Allocation in 2-Dimensional Arrays
• Note example, Fig. 3.5
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
1
Chapter Contents
3.1 Data Structures, Abstract Data Types and Implementations 3.2 Static Arrays 3.3 Multidimensional Arrays (optional) 3.4 Dynamic Arrays 3.5 C-Style Structs (optional) 3.6 Procedural Programming
5
Subscript Operation
• We have said elements accessed by name and [ ]
numList[5]
• Consider the [ ] to be an operator
– The subscript operator – Performs address translation
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
7
Out of Range Errors
• Note example program
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
11
Array of Array Declarations
• Each of the rows is itself a one dimensional array of values
scoresTable [1][3]
scoresTable[2]
is the whole row numbered 2
• Dynamic (run time) allocation mechanism provided
– Acquire memory as needed – Release memory when no longer needed
• C++ commands – new and delete
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
6
Using Arrays
• Accessing array for output
– See Fig. 3.3
• Accessing array for input from keyboard
– See Fig. 3.4
• Note use of arrays as parameters
– Must specify number of elements of array being used
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
10
Array of Array Declarations
14
Dynamic Arrays
• Recall earlier mention of arrays being fixed size at compile time
– Space wasted by unused elements – Program cannot adjust if size set too small
– extra locations filled with null character
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
• Elements of an array may be of any type
– Including characters
• Example:
char name [NAME_CAPACITY] = "John Doe";
• If array initialized shorter than specs
Chapter 3
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
• Name of the array is a pointer constant
– The base address
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
• Consider multiple pages of the student grade book
const int NUM_ROWS = 10, NUM_COLS = 5, NUM_RANKS = 10; typedef double ThreeDimArray[NUM_ROWS][NUM_COLS][NUM_RANKS];
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
9
Multidimensional Arrays
8
Multidimensional Arrays
• Consider a table of test scores for several different students
• Two-dimensional array
– Syntax
ElementType arrayName [numRows][numCols]
Leabharlann Baidu
15
The new Operator
• Syntax for arrays new Type [capacity] • This command issues a run-time request for a block of memory
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
2
Arrays
• Collection of data elements
13
Multidimensional Arrays as Parameters
• Must specify
– The type and name of the array (a pointer to the base address) – The number of rows – The number of columns (or the length of each row)
• An array of arrays
– An array whose elements are other arrays
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
b[5]
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
4
Character Arrays
– All of same type – Each accessed by specifying position
• Static array
– Compiler determines how memory allocated
• Dynamic array
– Allocation takes place at run time
• Elements stored in rowwise order • Also called column major order
location [0][4] is followed in memory by location [1][0]
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
3
Single Dimension Arrays
• Most C++ compilers do not by default check indices for out of range • Results of out of range array access
– Program can exceed allowed memory area – Program can give puzzling results
• Syntax:
ElementType arrayName [CAPACITY]; ElementType arrayName [CAPACITY] = { initializer_list };
• Example:
int b [10];
• Elements accessed by
– name and [ ] operation
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
12
Memory Allocation in 2-Dimensional Arrays
• Note example, Fig. 3.5
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
1
Chapter Contents
3.1 Data Structures, Abstract Data Types and Implementations 3.2 Static Arrays 3.3 Multidimensional Arrays (optional) 3.4 Dynamic Arrays 3.5 C-Style Structs (optional) 3.6 Procedural Programming
5
Subscript Operation
• We have said elements accessed by name and [ ]
numList[5]
• Consider the [ ] to be an operator
– The subscript operator – Performs address translation
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
7
Out of Range Errors
• Note example program
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
11
Array of Array Declarations
• Each of the rows is itself a one dimensional array of values
scoresTable [1][3]
scoresTable[2]
is the whole row numbered 2
• Dynamic (run time) allocation mechanism provided
– Acquire memory as needed – Release memory when no longer needed
• C++ commands – new and delete
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
6
Using Arrays
• Accessing array for output
– See Fig. 3.3
• Accessing array for input from keyboard
– See Fig. 3.4
• Note use of arrays as parameters
– Must specify number of elements of array being used
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
10
Array of Array Declarations
14
Dynamic Arrays
• Recall earlier mention of arrays being fixed size at compile time
– Space wasted by unused elements – Program cannot adjust if size set too small
– extra locations filled with null character
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
• Elements of an array may be of any type
– Including characters
• Example:
char name [NAME_CAPACITY] = "John Doe";
• If array initialized shorter than specs