Unit Nine Data Structures
Unit Nine Data Structures
Unit Nine Data Structures
A data structure is a data organization, management and storage format that enables efficient
access and modification. It’s a collection of data values, the relationships among them, and the
functions or operations that can be applied to the data.
Data structures include the array, the file, the record, the table, the tree, and so on. Any data
structure is designed to organize data to suit a specific purpose so that it can be accessed and
worked with in appropriate ways
9.1 ARRAYS
An array is a collection of variables of the same type that are referred to by a common name.
Arrays may have from one to several dimensions, although the one-dimensional array is the most
common. Arrays offer a convenient means of creating lists of related variables.
For instance, instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and
..., numbers[99] to represent individual variables. A specific element in an array is accessed by
an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
Array Declaration
To declare an array in C++, the programmer specifies the type of the elements and the number
of elements required by an array as follows −
This is called a single-dimension array. The arraySize must be an integer constant greater than
zero and type can be any valid C++ data type. For example, to declare a 10-element array called
balance of type double, use this statement
double balance[10]
1
Initializing Arrays
You can initialize C++ array elements either one by one or using a single statement as follows :
The number of values between braces { } cannot be larger than the number of elements that we
declare for the array between square brackets [ ]. If you omit the size of the array, an array just
big enough to hold the initialization is created. Therefore, if you write –
You will create exactly the same array as you did in the previous example. balance[4] = 50.0;
The above statement assigns element number 5th in the array a value of 50.0. Array with 4th
index will be 5th, i.e., last element because all arrays have 0 as the index of their first element
which is also called base index. Following is the pictorial representaion of the same array we
discussed above –
An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example
The above statement will take 10th element from the array and assign the value to salary variable.
Following is an example, which will use all the above-mentioned three concepts viz. declaration,
assignment and accessing arrays
namespace std;
2
#include <iomanip>
using std::setw;
int main () {
// output each array element's value for ( int j = 0; j < 10; j++ ) {
return 0;
One-dimensional arrays
A one-dimensional array is a list of related variables. Such lists are common in programming.
For example, you might use a one-dimensional array to store the account numbers of the active
users on a network. When computing the average of a list of values, you will often use an array
to hold the values. Arrays are fundamental to modern programming.
type name[size];
Example
3
For example, the following program loads sample with the numbers 0 through 9:
The following program creates an array of ten elements and assigns each element a value. It then
computes the average of those values and finds the minimum and the maximum value
4
5
Two-Dimensional Arrays
int twoD[10][20];
Unlike some other computer languages, which use commas to separate the array dimensions,
C++ places each dimension in its own set of brackets. Similarly, to access an element, specify
the indices within their own set of brackets. For example, for point 3,5 of array twoD, you would
use twoD[3][5].
In the next example, a two-dimensional array is loaded with the numbers 1 through 12.
6
In this example, nums[0][0] will have the value 1, nums[0][1] the value 2, nums[0][2] the value
3, and so on. The value of nums[2][3] will be 12. Conceptually, the array will look like that
shown here:
Two-dimensional arrays are stored in a row-column matrix, where the first index indicates the
row and the second indicates the column. This means that when array elements are accessed in
7
the order in which they are actually stored in memory, the right index changes faster than the
left.
You should remember that storage for all array elements is determined at compile time. Also, the
memory used to hold an array is required the entire time that the array is in existence. In the case
of a two-dimensional array, you can use this formula to determine the number of bytes of memory
that are needed:
Therefore, assuming four-byte integers, an integer array with dimensions 10,5 would have
10×5×4 (or 200) bytes allocated.
Multidimensional Arrays
C++ allows arrays with more than two dimensions. Here is the general form of a
multidimensional array declaration:
type name[size1][size2]...[sizeN];
int multidim[4][10][3];
Arrays of more than three dimensions are not often used, due to the amount of memory required
to hold them. Remember, storage for all array elements is allocated during the entire lifetime of
an array.
When multidimensional arrays are used, large amounts of memory can be consumed. For
example, a four-dimensional character array with dimensions 10,6,9,4 would require 10×6×9×4
(or 2,160) bytes. If each array dimension is increased by a factor of 10 each (that is,
100×60×90×40), then the memory required for the array increases to 21,600,000 bytes! As you
can see, large multidimensional arrays may cause a shortage of memory for other parts of your
program. Thus, a program with arrays of more than two or three dimensions may find itself
quickly out of memory
8
9.2 STRING
The C-style character string originated within the C language and continues to be supported
within C++. This string is actually a one-dimensional array of characters which is terminated by
a null character '\0'. Thus a null-terminated string contains the characters that comprise the string
followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing the string
is one more than the number of characters in the word "Hello."
It is not necessary to manually add the null terminator onto the end of string constants; the C++
compiler does this for you automatically. Therefore, the string “Mars” will appear in memory
like this
Example
9
The following program reads a string entered by the user:
Although this program is technically correct, it will not always work the way that you expect. To
see why, run the program and try entering the string “This is a test”. Here is what you will see:
Output: This
When the program redisplays your string, it shows only the word “This”, not the entire sentence.
The reason for this is that the C++ I/O system stops reading a string when the first whitespace
character is encountered. Whitespace characters include spaces, tabs, and newlines.
One way to solve the whitespace problem is to use another of C++’s library functions, gets( ).
The general form of a call to gets( ) is
gets(array-name);
To read a string, call gets( ) with the name of the array, without any index, as its argument. Upon
return from gets( ), the array will hold the string input from the keyboard. The gets( ) function
will continue to read characters, including whitespace, until you enter a carriage return. The
header used by gets() is <cstdio>.
10
This version of the preceding program uses gets( ) to allow the entry of strings containing spaces:
C++ supports a wide range of string manipulation functions. The most common are strcpy( )
The string functions all use the same header, <cstring>. Let’s take a look at these functions now.
strcpy
strcpy(to, from);
The strcpy( ) function copies the contents of the string from into to. Remember, the array that
forms to must be large enough to hold the string contained in from. If it isn’t, the to array will be
overrun, which will probably crash your program.
strcat
A call to strcat( ) takes this form: strcat(s1, s2); The strcat( ) function appends s2 to the end of
s1; s2 is unchanged. You must ensure that s1 is large enough to hold its original contents and
those of s2.
11
strcmp
strcmp(s1, s2);
The strcmp( ) function compares two strings and returns 0 if they are equal. If s1 is greater than
s2
lexicographically (that is, according to dictionary order), then a positive number is returned; if it
is less than s2, a negative number is returned.
The key to using strcmp( ) is to remember that it returns false when the strings match.
Therefore, you will need to use the ! operator if you want something to occur when the strings
are equal. For example, the condition controlling the following if statement is true when str is
equal to “C++”:
strlen
where s is a string. The strlen( ) function returns the length of the string pointed to by s.
The following program illustrates the use of all four string functions:
strcpy(s1, "C++");
12
strcpy(s2, " is power programming."); cout << "lengths: " << strlen(s1); cout << ' ' << strlen(s2)
<< '\n'; if(!strcmp(s1, s2))
cout << "The strings are equal\n"; else cout << "not equal\n"; strcat(s1, s2);
cout << s1 << " and " << s2 << "\n"; if(!strcmp(s1, s2))
return 0;
9.3 STRUCTURE
C++ arrays allow you to define variables that combine several data items of the same kind, but
structure is another user defined data type which allows you to combine data items of different
kinds. Structures are used to represent a record. Suppose you want to keep track of your books
in a library. You might want to track the following attributes about each book :
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new data
type, with more than one member, for your program. The format of the struct statement is:
{ member definition;
member definition;
...
member definition;
13
} [one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such as
int i; or float f; or any other valid variable definition. At the end of the structure's definition,
before the final semicolon, you can specify one or more structure variables but it is optional.
Here is the way you would declare the Book structure
} book;
To access any member of a structure, we use the member access operator (.). The member access
operator is coded as a period between the structure variable name and the structure member that
we wish to access. You would use struct keyword to define variables of structure type. Following
is the example to explain usage of structure
#include <iostream>
#include <cstring>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
14
int main() {
// book 1 specification
Book1.book_id = 6495237;
// book 2 specification
Book2.book_id = 6495700;
15
cout << "Book 2 author : " << Book2.author <<endl;
return 0;
You can pass a structure as a function argument in very similar way as you pass any other variable
or pointer. You would access structure variables in the similar way as you have accessed in the
above example:
char author[50];
};
int main() {
16
// book 1 specification
Book1.book_id = 6495407;
// book 2 specification
Book2.book_id = 6495700;
printBook( Book1 );
printBook( Book2 );
return 0;
17
cout << "Book id : " << book.book_id <<endl;
9.4 POINTERS
A pointer is an object that contains a memory address. Very often this address is the location of
another object, such as a variable. For example, if x contains the address of y, then x is said to
“point to” y.
Pointer variables must be declared as such. The general form of a pointer variable declaration is
type *var-name;
Here, type is the pointer’s base type. The base type determines what type of data the pointer will
be pointing to. var-name is the name of the pointer variable. For example, to declare ip to be a
pointer to an int, use this declaration:
int *ip;
Since the base type of ip is int, it can be used to point to int values. Here, a float pointer is
declared: float *fp;
In this case, the base type of fp is float, which means that it can be used to point to a float value.
In general, in a declaration statement, preceding a variable name with an * causes that variable
to become a pointer.
There are two special operators that are used with pointers: * and &. The & is a unary operator
that returns the memory address of its operand. (Recall that a unary operator requires only one
operand.)
18
For example, ptr = &total;
puts into ptr the memory address of the variable total. This address is the location of total in the
computer’s internal memory. It has nothing to do with the value of total. The operation of & can
be remembered as returning “the address of” the variable it precedes. Therefore, the preceding
assignment statement could be verbalized as “ptr receives the address of total.” To better
understand this assignment, assume that the variable total is located at address 100. Then, after
the assignment takes place, ptr has the value 100.
19