You are on page 1of 6

DATA STRUCTURES

What is a data structure?


Data structure is a tool to preserve data and relation among the data through an appropriate
representation.
The data structure that defines possible logical relationships among data elements. It offers
various organizing methods for logically related data. It suggest those operations that are suitable and
efficiently be operated on particular related data items. It recommends a storage structure that matches
and/or retains logical relationship among data elements. It recommends possible programming
languages to be employed for solving a problem.

“Data Structure is mathematical or logical representation of data in the memory.”


Classification of data structure:

Data Structures

Primitive Data Non-Primitive


Structure Data Structure

Integer
Linear Data Non-Linear
Floating point Structure Data Structure

Character Arrays Trees

Pointers Stack Graph

Queues

Linked List

Files
Primitive data structure:
Data structures that are directly operated/manipulated by the machine instructions are
known as primitive data structure.
The integer, real (float), character, pointers are the primitive data structures

Operations on primitive data structures:


The various operations which are performed on primitive data structure are:
1. Create: Create operation is used to create a new data structure. This operation reserves
memory space for the program elements:
Ex: int x; float a; etc.
2. Destroy: Destroy operation is used to destroy or remove the data structure from the
memory space.
3. Select: Select operation is used by the programmer to access the data within data
structure
4. Update: Update operation is used to change data of data structures.
Ex: int x=2;
Again x=6
Here 2 is automatically replaced by 6
Non-primitive data structure:
The non-primitive data structure are the data structure that cannot be manipulated
directly by machine instructions.
A non-primitive data structure can be classified as linear data structure and non-linear data
structure.
Linear data structure:
A data structure is called linear if its elements are ordered in one to one correspondence with
the natural numbers.
A data structure, where each element excluding the first and the last elements can have
adjacent elements.
Ex: 5 6 8 7 12 45 8 66 } list of elements
Linear data structure can be classified as arrays, stack, queue, linked list.
Operations on linear data structure:
The operations which are performed on linear data structure are
1. Traversal: Traversing means accessing each element exactly once so that the elements
of a data structure can be processed. Traversing is also called as visiting.
2. Insertion: The process of adding a new data item in the given collection of data items.
3. Deletion: The process of deleting an existing data item from the given collection of data
items.
4. Searching: The process of finding the location of the data item if it exists in the given
collection of data items.
5. Sorting: The process of arranging the data items in some order i.e. in ascending or
descending order.
6. Merging: The process of combining the data items of two structures to form a single
structure is called merging
Arrays:
An array is a collection of data elements of same data type under same name.
In array each element is numbered as 0, 1, 2, 3, …. , n-1. These numbers are called indices
or subscripts. These numbers are used to locate the position of the data elements within the array.
In C++ the subscripts always starts with 0.
Types of arrays:
There are three types of arrays
1. One dimensional array
2. Two dimensional array
3. Multi-dimensional array
One dimensional array:
One dimensional array contains only one subscript. Here each element is accessed by one
subscript. Array elements are always stored in contiguous memory locations.

Features of one dimensional array:


• Array size should be positive number only
• String arrays always terminates with null character
• Array elements are counted from 0 to n-1
• Useful for multiple reading of elements
Declaration of one dimensional array:
Syntax:
datatype array-name [size];
Where data type may be an int, float or char. And the array-name is any valid c++ identifier.
The number of elements that can be stored in the array is called size of the array. The size of the
array must be an integer constant. The size size should be enclosed within the square brackets.
Example:
int a[10];
float height[20];
char ch[5];
Calculating the length of the array:
The length of the array can be calculated by
L = UB – LB + 1
Where L is the length of the array, UB is the largest index and LB is smallest index of an array.
Ex:
If an array contains 5 elements which are stored in the position starting from 0,1,2,3,4 then
the UB=4 and LB=0
Then size of the array is L=4 – 0 +1 =5
Representation of one dimensional array:
Elements of linear array are stored in consecutive memory locations.
Let A be a one dimensional array with n number of elements. Then the storage representation is
shown in the figure
0 1000
Data 45
1 1001

2 25
1002

3 89
1003
Subscript 4 36
1004 Location address

5 77
1005

. 29
.

. ..
.
n-1 .
.
..

Associated with each element in the memory there is a subscript and the memory address
location.
The starting address of an array is called the “base address(B)”.
The number of bytes used to represent the element in the memory is called word size(W).
Address of an array element with subscript I in the array can be computed using the following
relation.
A[i] = Base address + i * word size
A[i] = B + I * W
A[0] = 1000 + 0 * 2 = 1000
A[1] = 1000 + 1 * 2 = 1002
A[2] = 1000 + 2 * 2 = 1003
A[3] = 1000 + 3 * 2 = 1004
……………………………………………
Basic operations on one dimensional array:
The operations which are performed on one dimensional array
1. Traversal: Traversing means accessing each element exactly once so that the elements
of a data structure can be processed. Traversing is also called as visiting.
2. Insertion: The process of adding a new data item in the given collection of data items.
3. Deletion: The process of deleting an existing data item from the given collection of data
items.
4. Searching: The process of finding the location of the data item if it exists in the given
collection of data items.
5. Sorting: The process of arranging the data items in some order i.e. in ascending or
descending order.

Traversing a linear array:


Traversing is the process of visiting each element of a array exactly once from the
beginning to last element.
Program to read the elements into the array and printing the elements.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10], i, n;
clrscr();
printf(“enter the size of an array\n”);
scanf(“%d”,&n);
printf(“enter array elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“the elements are…\n”);
for(i=0;i<n;i++)
printf(“%d\t”, a[i]);
getch();
}
Output:
Enter the size of an array 5
Enter the array elements 3 5 8 9 6
The array elements are 3 5 8 9 6

You might also like