Professional Documents
Culture Documents
DATA STRUCTURES
Data Structure
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.
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 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.
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
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:
Float B[10];
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 –
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.
Following is an example to assign a single element of the array –
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
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
A
6 D
5 O
4 O
3 M
2 H
1 A
0 M
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
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 –
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 –
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 −
0 1 2 3
a= 4 5 6 7
8 9 10 11
General Syntax
Datatype arrayName [RowSize][ColumnSize]={{data of row 0},{data of row 1},{ data
of row 2}};
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};
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.
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";
}
}
#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";
}
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();
}