You are on page 1of 15

CHAPTER 1

DATA STRUCTURES

Data Structure

A data structure is an arrangement of data in a computer's memory or even


disk storage. An example of several common data structures are arrays, linked
lists, queues, stacks, binary trees, and hash tables. There are four types of data
structures they are: Linear, Non - Linear, Homogenous and Non - Homogenous.

Linear
In linear data structures, values are arranged in linear fashion. Arrays,
linked lists, stacks and queues are examples of linear data structures in which
values are stored in a sequence.

Non-Linear
This type is opposite to linear. The data values in this structure are not
arranged in order. Tree, graph, table and sets are examples of non-linear data
structures.

Homogenous
In this type of data structures, values of the same types of data are stored,
as in an array.

Non-homogenous
In this type of data structures, data values of different types are grouped, as
in structures and classes.

Types of data structures


Linear and non-linear data structures

Abstract Data Type


In computer science, an abstract data type (ADT) is a mathematical model
for a certain class of data structures that have similar behavior; or for certain data
types of one or more programming languages that have similar semantics.

Arrays
Array is a data structure that can store a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as

For example if we need to store some numbers like 42,84,36,65,49,87,88,53.


Without using array we would use individual variable to store the above
numbers as follows
A=42
B=84
C=36
D=65
E=49
F=87
G=88
H=53

For few numbers we can declare an individual variable. But if we have more
numbers it is very difficult to declare those numbers. So, at this point we could an
array.

A=[42, 84, 36, 65, 49, 87, 88, 53].


Int A[8] = {42, 84, 36, 65, 49, 87, 88, 53}

What is the structure of array? It is like this

Array name: A
Index Value
[7] 53
[6] 88
[5] 87
[4] 49
[3] 65
[2] 66
[1] 84
[0] 42

So, under one variable name A we can store several values. How to access a values
from array.

Literally, the array values are called by the index. So the values are seen like below

A[0] = 42
A[1] = 84
A[2] = 66
A[3] = 65
A[4] = 49
A[5] = 87
A[6] = 88
A[7] = 53

A specific element in an array is accessed by an index. Example if we need


the 5th value from the array, we can access by A[4]. All arrays consist of contiguous
memory locations. The lowest address corresponds to the first element and the
highest address to the last element.

 Array index starts with 0 and ends with n-1, where n is the size of array.
 The no. of elements in array are called as length or size of array.

Declaring Arrays
Declaration means it to fix the data types of the value or data of the array.
For example we might use many types of data for example:

1. 4, 25, 86, 49 – These data are called Integer data type


2. 5.6, 6.4, 9.5 – These data are called float data type
3. A, g, H, I, r – These data are character data type
4. This is a car, There is a pen – these data are called string data type.

The general syntax for declaring a array is as follows:

Datatype arrayName [arraySize ];

How to declare the following data?


Int A=[42, 84, 36, 65, 49, 87, 88, 53].

We have to declare as follow.


First identify what type of data – it is integer
Second what is the name of the array – the name is A
Third how many data you have – we have 8 data
As per the syntax declaration is
int A[8];

This is called a single-dimensional array. The arraySize must be an integer


constant greater than zero and type can be any valid C/C++ data type. For
example, to declare a 10 element array called B of type float, use this statement –

Float B[10];

Here B is a variable array which is sufficient to hold up to 10 float numbers.


Note: You can keep any name and size for an array, for example
int car[5];
int balance[50];
string text[12];
char test[23];
string value[5];

Initializing Arrays
Initializing the array is filling or putting the data in array. You can initialize
an array either one by one or using a single statement as follows –

int Balance[5] = {10, 2, 34, 70, 50};

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 –

int A[] = {10, 20, 34, 70, 50};

You will create exactly the same array as you did in the previous example.
Following is an example to assign a single element of the array –

float balance[4] = 50.0;

The above statement assigns the 5th element in the array with a value of 50.0. All
arrays have 0 as the index of their first element which is also called the base index
and the last index of an array will be total size of the array minus 1.

Character array
Character array contains single characters in an array, for example

1. char A[] = "Mahmood";


2. char A[10] = "Mahmood";
3. char A[] = {'M','a','h','m','o','o','d','\0'};
4. char A[7] = {'M','a','h','m','o','o','d','\0'};

A
6 D
5 O
4 O
3 M
2 H
1 A
0 M
2. char A[100] = "Mahmood";

A
9
8
7
6 D
5 O
4 O
3 M
2 H
1 A
0 M

char A[] = {'M','a','h','m','o','o','d','\0'};

A
6 D
5 O
4 O
3 M
2 H
1 A
0 M

char A[8] = {'M','a','h','m','o','o','d','\0'};

A
7
6 D
5 O
4 O
3 M
2 H
1 A
0 M

String array
String is a sequence of characters that is treated as a single data item.
A string is actually one-dimensional array of characters in C language. These are
often used to create meaningful and readable programs.
For example: The string "hello world" contains 12 characters
including '\0' character which is automatically added by the compiler at the end of
the string.
string A[]={"Ahmet", "Mehmet", "Bulent", "Fuat"};

A
3 Fuat
2 Bulent
1 Mehmet
0 Ahmet

Declaring and Initializing a string variables


There are different ways to initialize a character array variable.
char name[10]="StudyTonight"; //valid character array initialization
char name[10]={'L','e','s','s','o','n','s','\0'}; //valid initialization
Remember that when you initialize a character array by listings all its characters
separately then you must supply the '\0' character explicitly.
string A[]={"Ahmet", "Mehmet", "Bulent", "Fuat"};

Accessing Array Elements


An element is accessed by the index of the array. This is done by placing the
index of the element within square brackets after the name of the array. For
example –
float salary = balance[9];
The above statement will take the 10th element from the array and assign the
value to salary variable. The following example shows how to use all the three
above mentioned concepts viz. declaration, assignment, and accessing arrays
#include <iostream.h>
using namdspace std:
int main()
{
int Balance[500] = {10, 2, 34, 70, 50};

Balance
499

4 50
3 70
2 34
1 2
0 10

Output:

Multidimensional arrays
C++ programming language allows multidimensional arrays. Here is the
general form of a multidimensional array declaration –

Datatype arrayName [Size 1] [Size 2] [Size 3]…. [Size n];

For example, the following declaration creates a three dimensional integer array –
int threedim[5][10][4];

Two-dimensional Arrays
The simplest form of multidimensional array is the two-dimensional array. A
two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a
two-dimensional integer array of size [x][y], you would write something as follows –

Datatype arrayName [RowSize][ColumnSize];

Int A[3][4]
Int B[3][4][5]

Where type can be any valid C data type and arrayName will be a valid C
identifier. A two-dimensional array can be considered as a table which will have x
number of rows and y number of columns. A two-dimensional array a, which
contains three rows and four columns can be shown as follows −

Thus, every element in the array A is identified by an element name of the


forma[ i ][ j ], where 'a' is the name of the array, and 'i' and 'j' are the subscripts
that uniquely identify each element in 'A'.

Initializing Two-Dimensional Arrays


Multidimensional arrays may be initialized by specifying bracketed values
for each row. Following is an array with 3 rows and each row has 4 columns.
How to declare below two dimension array

0 1 2 3
a= 4 5 6 7
8 9 10 11

To declare we need to understand layout of the two dimension array. The


layout is given below:

Column 0 Column 1 Column 2 Column 3


Row 0 0 1 2 3
Row 1 4 5 6 7
Row 2 8 9 10 11
Cout<<a[1][1]
5

General Syntax
Datatype arrayName [RowSize][ColumnSize]={{data of row 0},{data of row 1},{ data
of row 2}};

int a[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};

Once the above statement execute we will the array as follows:

0 1 2 3
a= 4 5 6 7
8 9 10 11

The nested braces, which indicate the intended row, are optional. The following
initialization is equivalent to the previous example –
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Accessing Two-Dimensional Array Elements


An element in a two-dimensional array is accessed by using the subscripts,
i.e., row index and column index of the array. For example –
int val = a[2][3];
The above statement will take the 4th element from the 3rd row of the array. You
can verify it in the above figure. Let us check the following program where we have
used a nested loop to handle a two-dimensional array

Sparse Array:
A sparse array is an array in which most of the elements have the default
value (usually 0 or null). The occurrence of zero-value elements in a large array is
inefficient for both computation and storage. An array in which there are a large
number of zero elements is referred to as being sparse.
 A sparse array is simply an array most of whose entries are zero (or null, or
some other default value)
 For example: Suppose you wanted a 2-dimensional array of course grades,
whose rows are Penn students and whose columns are courses
 There are about 22,000 students
 There are about 5000 courses
 This array would have about 110,000,000 entries
 Since most students take fewer than 5000 courses, there will be a lot
of empty spaces in this array
 This is a big array, even by modern standards

Sparse Matrix

If a lot of elements from a matrix have a value 0 then the matrix is known as
SPARSE MATRIX. If the matrix is sparse we must consider an alternate way of
representing it rather the normal row major or column major arrangement. This is
because if majority of elements of the matrix are 0 then an alternative through
which we can store only the non-zero elements and keep intact the functionality of
the matrix can save a lot of memory space.

Example: Original matrix of dimension 7 x 7.


COLUMNS

0 1 2 3 4 5 6

0 0 0 0 -5 0 0 0
1 0 4 0 0 0 0 7
2 0 0 0 0 9 0 0
ROWS 3 0 3 0 2 0 0 0
4 1 0 2 0 0 0 0
5 0 0 0 0 0 0 0
6 0 0 8 0 0 0 0

Sparse Matrix

7, 7, 9
0, 3, -5
1, 1, 4
1, 6, 7
2, 4, 9
3, 1, 3
3, 3, 2
4, 0, 11
4, 2, 2
6, 2, 8

Dense Arrays:
A dense array is an array that has been created with each of its elements
being assigned a specific value. Dense arrays are used exactly in the same manner
as other arrays. They are declared and initialized at the same time. dense array can
be created as : ArrayName = new Array(value0,Value1,value2,……valueN)
In this array, since the element count starts from 0 to n, the array length is n +1.

Operation in an array:

1. Create
2. Insert
3. Delete
4. Display
5. Copy
6. Modify
7. Merge.

1. Create:
A) Static Creation – the values of the array are predefined and fixed

#include<iostream>
using namespace std;
int main()
{
int A[5]={10,20,30,40,50}; //array declaration and initialization
for (int n=0 ; n<5 ; ++n )
{
cout<<"A["<<n<<"] ="<< A[n] << "\n";
cout<<"\n";
}
}

B) Dynamic Creation – The values are varied on each execution of array by


the user.

#include <iostream>
using namespace std;
int main()
{
int array[5];
cout<<"Enter the Values of the Array1 \n";
for (int x = 0; x < 5; x++ )
{
cin>> array[x];
}
for (int n=0 ; n<5 ; ++n )
{
cout<<"A["<<n<<"] ="<< array[n] << "\n";
cout<<"\n";
}

cin.get();
}
2. Insert and modify
#include <iostream>
using namespace std;
int main ()
{
int A[5],x,n,pos,a;
cout<<"Enter the Values of the Array \n";
for ( x = 0; x < 5; x++ )
cin>> A[x];
cout<<"The position to be inserted \n";
cin>>pos;
cout<<"The Element to be inserted \n";
cin>> a;
A[pos]=a;
cout<<"The Elements in the new Array are \n";
for ( n=0 ; n<5 ; ++n )
{
cout<<"A["<<n<<"] ="<< A[n] << "\n";
cout<<"\n";
}
cin.get();
return 0;
}
3. Delete
#include <iostream>
using namespace std;
int A[5] ;
int n, pos,x;
int main ()
{
cout<<"Enter the Values of the Array \n";
for ( x = 0; x < 5; x++ )
cin>> A[x];
cout<<"The position to be deleted \n";
cin>>pos;
A[pos]=00;
cout<<"The Elements in the new Array are \n";
for ( n=0 ; n<5 ; ++n )
{
cout<<"A["<<n<<"] ="<< A[n] << "\n";
cout<<"\n";
}
cin.get();
return 0;
}

4. Display
#include<iostream>
using namespace std;
int main()
{
int A[5]={10,20,30,40,50}; //array declaration and initialization
cout<<"Array Elements are :\n\n";
for(int i=0;i<5;i++)
{
cout<<"A["<<i<<"] = "<<A[i]<<"\n";
}
cin.get();
}

5. Copy
#include <iostream>
using namespace std;
int main()
{
int x;
int array1[5];
int array2[5];
cout<<"Enter the Values of the Array1 \n";
for ( x = 0; x < 5; x++ )
{
cin>> array1[x];
}
cout<<"Array1 Elements are:\n";
for ( x = 0; x < 5; x++ )
cout<<"A["<<x<<"] ="<< array1[x] << "\n";
cout<<"\n";
cout<<"Array2 Elements are:\n";
for ( x = 0; x < 5; x++ )
{
array2[x]=0;
cout<<"A["<<x<<"] ="<< array2[x] << "\n";
}

cout<<"The Element in the array2 after copying is: \n";


for ( x = 0; x < 5; x++ )
{
array2[x]=array1[x];
cout<<"A["<<x<<"] ="<< array2[x] << "\n";
}
cin.get();
}

6. Merge.
#include <iostream>
using namespace std;
int main()
{
int x;
int array1[5];
int array2[5];
int array3[10];
cout<<"Enter the Values of the Array1 \n";
for ( x = 0; x < 5; x++ )
{
cin>> array1[x];
}
cout<<"Enter the Values of the Array2 \n";
for ( x = 0; x < 5; x++ )
{
cin>> array2[x];
}
cout<<"Array3 Elements are:\n";
for ( x = 0; x < 10; x++ )
{
array3[x]=0;
cout<<"A["<<x<<"] ="<< array3[x] << "\n";
}
cout<<"After Merging the Array3 is\n";
for ( x = 0; x < 5; x++ )
{
array3[x]=array1[x];
cout<<"A["<<x<<"] ="<< array3[x] << "\n";
}
for ( x = 5; x < 10; x++ )
{
array3[x]=array2[x-5];
cout<<"A["<<x<<"] ="<< array3[x] << "\n";
}
cin.get();
}

You might also like