Introduction to DS
Introduction to DS
o Searching
o Insertion
o Deletion
o Modification
• Traversing: Traversing a Data Structure means
to visit (Accessing) the element stored in it.
This can be done with any type of Data
Structure.
• Searching: Searching means to find a
particular element in the given data-
structure. It is considered as successful when
the required element is found.
• Insertion: It is the operation which we apply on all the
data-structures. Insertion means to add an element in
the given data structure. The operation of insertion is
successful when the required element is added to the
required data-structure.
• Deletion: It is the operation which we apply on all the
data-structures. Deletion means to delete an element in
the given data structure. The operation of deletion is
successful when the required element is deleted from
the data structure.
• Modification: Modification(Update) operation refers to
updating an existing element in the given data
structure.
Linear Data Structure and Non-linear Data Structure
Linear Data Structure Non-linear Data Structure
In a linear data structure, data elements In this structure, the elements are arranged
are arranged in a linear order hierarchically or non-linear manner.
Arrays, linked list, stack, queue are the Trees and graphs are the types of a non-
types of a linear data structure. linear data structure.
Due to the linear organization, they are Due to the non-linear organization, they are
easy to implement. difficult to implement.
Primitive data structure will contain some Non-primitive data structure can
value, i.e., it cannot be NULL. consist of a NULL value.
The size depends on the type of the data In case of non-primitive data
structure. structure, size is not fixed.
Array
• Why do we need arrays?
– We can use normal variables (v1, v2, v3, ..) when we
have a small number of objects, but if we want to
store a large number of instances, it becomes
difficult to manage them with normal variables.
– The idea of an array is to represent many instances in
one variable.
Array
• The fundamental data types, namely char, int, float, double,
variable of these types can store only one value at any
given time.
Ex:
• List of employees in an organization.
• Test scores of a class of students.
• List of customers and their telephone numbers.
• As we mentioned earlier, an array is a sequenced collection of
related data items that share a common name.
Array will be of
• One-dimensional arrays
• Two-dimensional arrays
• Multidimensional arrays
ONE-DIMENSIONAL ARRAYS
• A list of items can be given one variable name
using only one subscript and such a variable is
called a single-subscripted variable or a one-
dimensional array.
• This would cause the array number to store the values as shown below:
number [0] 35
number [1] 40
number [2] 20
number [3]
57
number [4]
19
Declaring Arrays
• Like any other variable, arrays must be declared before they
are used so that the compiler can allocate space for them in
memory.
• The general form of array declaration is
type arrayName [ arraySize ];
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]);
}
int x[3][4];
• Here, x is a two-dimensional (2d) array. The array can
hold 12 elements.
• You can think the array as a table with 3 rows and each
row has 4 columns.
//Example on 2D Array
#include<stdio.h>
int main()
{
int arr[10][10], row, col, i, j;
printf("Enter Row Size of Array (max. 10): ");
scanf("%d", &row);
printf("Enter Column Size of Array (max. 10): ");
scanf("%d", &col);
printf("\nEnter %d Array Elements: ", row*col);
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
scanf("%d", &arr[i][j]);
}
printf("\nThe Array is:\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
return 0;
}