Professional Documents
Culture Documents
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
• Data structures provide a means to manage
large amounts of data efficiently for uses
such as large database and internet
indexing service . Usually, efficient data
structures are key to designing
efficient algorithms
• abstract data type (ADT) is a mathematical
model for data types, where a data type is
defined by its behavior from the point of view
of a user of the data
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Abstract Data Types
• Abstract Data type (ADT) is a type (or class) for objects whose
behavior is defined by a set of value and a set of operations.
• The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented. It
does not specify how data will be organized in memory and
what algorithms will be used for implementing the operations. It
is called “abstract” because it gives an implementation-
independent view. The process of providing only the essentials
and hiding the details is known as abstraction.
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Data Types
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Pointers
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
• int x;
• Int *p;
• X=5;
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
• Int x;
• char ch = ’Q’;
• char* p;
• p = &ch; // p holds the address of ch
• cout << *p; // outputs the character ’Q’
• ch = ’Z’; // ch now holds ’Z’
• cout << *p; // outputs the character ’Z’
• *p = ’X’; // ch now holds ’X’
• cout << ch; // outputs the character ’X’
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
• pointers are very useful when building data
structures where objects are linked to one
another through the use of pointers
• only to fundamental types, such as char and
int—they may also point to complex types
and even to functions
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
• char c[ ] = {’d’, ’a’, ’t’};
• char* p = c; // p points to c[0]
• char* q = &c[0]; // q also points to c[0]
• cout <<c[2] <<p[2] <<q[2]; // outputs “ttt”
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
array
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
• int a[ ] = {10, 11, 12, 13}; // declares and
initializes a[4]
• bool b[ ] = {false, true}; // declares and
initializes b[2]
• char c[ ] = {’c’, ’a’, ’t’}; // declares and
initializes c[3]
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Processing One-Dimensional Arrays
• Some basic operations performed on a one-
dimensional array are:
− Initializing
− Inputting data
− Outputting data stored in an array
− Finding the largest and/or smallest element
• Each operation requires ability to step through
the elements of the array
• Easily accomplished by a loop
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Processing One-Dimensional Arrays
(continued)
• Consider the declaration
int list[100]; //array of size 100
int i;
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Partial Initialization of Arrays
During Declaration
• The statement:
int list[10] = {0};
declares list to be an array of 10 components
and initializes all of them to zero
• The statement:
int list[10] = {8, 5, 12};
declares list to be an array of 10 components,
initializes list[0] to 8, list[1] to 5,
list[2] to 12 and all other components are
initialized to 0
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Partial Initialization of Arrays
During Declaration (continued)
• The statement:
int list[] = {5, 6, 3};
declares list to be an array of 3 components
and initializes list[0] to 5, list[1] to 6, and
list[2] to 3
• The statement:
int list[25]= {4, 7};
declares an array of 25 components; initializes
list[0] to 4 and list[1] to 7; all other
components are initialized to 0
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Some Restrictions on Array
Processing
• Consider the following statements:
• Solution:
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Some Restrictions on Array
Processing (continued)
• The following is illegal too:
• Solution:
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Arrays as Parameters to Functions
• Arrays are passed by reference only
• The symbol & is not used when declaring an
array as a formal parameter
• The size of the array is usually omitted
− If provided, it is ignored by the compiler
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Constant Arrays as Formal
Parameters
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Base Address of an Array and
Array in Computer Memory
• The base address of an array is the address, or
memory location of the first array component
• If list is a one-dimensional array, its base
address is the address of list[0]
• When we pass an array as a parameter, the
base address of the actual array is passed to
the formal parameter
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Functions Cannot Return a Value
of the Type Array
• C++ does not allow functions to return a
value of the type array
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Parallel Arrays
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Two-Dimensional Arrays
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Accessing Array Components
• Syntax:
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Accessing Array Components
(continued)
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Two-Dimensional Array
Initialization During Declaration
• Two-dimensional arrays can be initialized
when they are declared:
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Initialization
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Print
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Input
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Sum by Row
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Sum by Column
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Largest Element in Each Row and
Each Column
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Reversing Diagonal
• Before:
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Reversing Diagonal (continued)
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science
Reversing Diagonal (continued)
• After:
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science