Data Structures Using C++: Ms - Akhila Shaji Assistant Professor SSM College, Rajakkad
Data Structures Using C++: Ms - Akhila Shaji Assistant Professor SSM College, Rajakkad
Using C++
Ms.Akhila Shaji
Assistant Professor
SSM College,Rajakkad
Array operations
•There are several operations that can be
performed on an array. They are
Insertion
Deletion
Traversal
Merging
Sorting
Searching
Insertion operation
Insertion of a new element in an array can be done in
two ways:
i. Insertion at the end of the array
Inserting an element at the end of an array can be easily
done provided the memory space allocated for the array
is large enough to accommodate the additional element.
for eg: Insert15 at the end of the array.
int a[5]={11,13,14,4};
a[0] a[1] a[2] a[3] a[4]
Before insertion
11 13 14 4 0
After insertion
11 13 14 4 15
ii. Insertion at required position
• For inserting the element at required position,
elements must shifted one place to the right(down)
of their current position from the specified
position.
•Then we have placed the new element at vacant
position.
•The shifting is to be done to accommodate the
new element and keep the order of the elements.
For eg:insert 12 in 2nd position in the array
int a[5]={11,13,14,4};
a[0] a[1] a[2] a[3] a[4]
Before Insertion 11 13 14 4 0
11 13 14 4 0
After shifting 11 13 14 4
In the array 2nd position becomes vacant for insertion
After insertion
11 12 13 14 4
Insertion Algorithm
Insert(a,len,pos,num)
//a is a linear array with len elements
//pos is the position at which number num will be
inserted.
1. [ Initialize the value of i ] Set i = len
2. Repeat for i = len down to pos //for(i=len;i>=pos;i--)
[shift the elements down(right) by 1 position]
set a[i+1]=a[i]
[end of loop]
3. [Insert the element at required position]
set a[pos] = num
4. [ Reset len] Set len=len+1
5. Display the new list of arrays
6. End
Deletion operation
i. Deletion at the end of the array
Deleting an element at the end of an array can be
easily done. Deleting element from the end means just
remove the end element from the array and decrement
the array size.
for eg: Delete15 from the end of the array.
int a[5]={11,13,14,4,15};
a[0] a[1] a[2] a[3] a[4]
Before Deletion 11 13 14 4 15
After Deletion 11 13 14 4
ii. Deletion at required position
11 13 14 4
After Deletion
In the array 2nd position becomes vacant after
deletion. Shift one position to the left from 3rd position
After shifting 11 13 14 4
Deletion Algorithm
Delete(a,pos,n)
//a is a linear array with n elements
//pos is the position at which element will be deleted.
1. Set item=a[pos]
2. Repeat for j = pos to n-1 //for(j=pos;j<=n-1;j++)
[shift the elements up(left) by 1 position]
set a[j]=a[j+1]
[end of loop]
3. [ Reset n] Set n=n-1
4. Display the new list of arrays
5. End
Traversing Linear Arrays
Linear Array
•••
Traversing Linear Arrays
Linear Array
•••
13 Traversing Linear Arrays
Linear Array
•••
14 Traversing Linear Arrays
Linear Array
•••
15 Traversing Linear Arrays
Linear Array
•••
16 Traversing Linear Arrays
Linear Array
•••
Traversing of Arrays