Algorithms and Data Structures in C++
by Alan Parker CRC Press, CRC Press LLC ISBN: 0849371716 Pub Date: 08/01/93 |
Previous | Table of Contents | Next |
Sequential arrays stored in memory also rely on pointers for index calculations. The array example in Code List 3.8 demonstrates the differences between pointers and arrays for the case of the multidimensional array. The output of the program is shown for two different platforms. Code List 3.9 shows the output of the program for a DOS system while Code List 3.10 shows the output of the program on a Unix system. For this program two different methodologies are used for implementing the storage of four integers. The memory allocation is illustrated in Figure 3.3. The key difference between the implementation of the pointers and the multidimensional array is that the array a[2][2] is not a variable. As a result, operations such as a=a+1 are invalid.
Figure 3.3 Memory Organization for Code List 3.8
Someone slightly familiar with C or C++ might be surprised to see that the output indicates that the values of &a, a, and *a are all equal. While this looks unusual it is correct. The declaration int a[2][2] in C and C++ declares a to be an array of arrays. In this case there are two arrays each containing two integers. The first array is located at address A4 while the second array is located at the address A5.
The output for b follows directly the addressing as illustrated in Figure 3.3
Code List 3.8 Array Example
Code List 3.9 Output of Code in Code List 3.8
Code List 3.10 Output of Code in Code List 3.8
An example of overloading in C++ is shown in Code List 3.11. The output of the program is shown in Code List 3.12. This program overloads the operator () which is used to index into a set of characters for a specific data bit. The packing is illustrated in Figure 3.4 for the variable e declared in the program.
Figure 3.4 Packing Bits in Memory
Code List 3.11 Operator Overloading Example
Code List 3.12 Output of Program in Code List 3.11
This section demonstrates the creation of an array class in C++ using templates. The goal of the program is to demonstrate the implementation of a feature of C++ which is already built in; therefore, the code is for instructive purposes only. The code for a program to create an array class is illustrated in Code List 3.13, The output of the program is shown in Code List 3.14. The array class is declared in the program as a generic class with a type T which is specified later when an array variable is declared. As seen in the main function three arrays are declared: a, b, and c. The array a consists of ten integers. The array b consists of five doubles. The array c consists of 3 characters. The constructor function for the array initializes all the elements of the array to zero. The function set_data is used to assign a value to a specific element in the array. The function print_data is used to print a specific element in the array.
Code List 3.13 Creating an Array Class in C++
Code List 3.14 Output from Code List 3.13
A stack is a data structure used to store and retrieve data. The stack supports two operations push and pop. The push operation places data on the stack and the pop operation retrieves the data from the stack. The order in which data is retrieved from the stack determines the classification of the stack.
A FIFO (First In First Out) stack retrieves data placed on the stack first. A LIFO (Last In First Out) stack retrieves data placed on the stack last. A LIFO stack push and pop operation is illustrated in Figure 3.5.
Figure 3.5 Push and Pop in a LIFO Stack
The source code to implement a LIFO stack class is shown in Code List 3.15. The output of the program is shown in Code List 3.16. Notice that templates are used again so the type used for the stack is defined at a later point.
Code List 3.15 LIFO Stack Class
Code List 3.16 Output of Program in Code List 3.15
This section presents the linked list data structures. This is one of the most common structures in program design.
Previous | Table of Contents | Next |