0% found this document useful (0 votes)
12 views

Traversing

The document provides an overview of data structures focusing on linear arrays, covering operations such as traversing, insertion, and deletion. It includes algorithms and C++ code examples for each operation, illustrating how to manipulate array elements. Additionally, it discusses searching methods, specifically the linear search algorithm, detailing its process and providing an example.

Uploaded by

adinathgire2645
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Traversing

The document provides an overview of data structures focusing on linear arrays, covering operations such as traversing, insertion, and deletion. It includes algorithms and C++ code examples for each operation, illustrating how to manipulate array elements. Additionally, it discusses searching methods, specifically the linear search algorithm, detailing its process and providing an example.

Uploaded by

adinathgire2645
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structures: Traversing, Insertion & Deletion

Traversing of Linear Array


Traversing means visiting the elements of a data structure at least once.
For example, LA is a linear array and we can traverse the elements using
lower bound(LB) and upper bound(UB).

Traversing refers to the process of accessing each element in a data


structure, such as an array or linked list, and performing some operation
on it.

Algorithm/Pseudo code for traversing:

Process: Applying an operation to each element of LA.


Step 1: [Initialise counter]
Set k = LB
Step 2: Repeat Step 3 and Step 4 while k≤UB
Step 3: [Visit element]
Apply process to LA[k]
Step 4: [Increase counter]
Set k = k+1
[end of Step 2 loop]
Step 5: Exit.
C++ Code for Traversing of Linear Array
#include <iostream>
using namespace std;
int main()
{
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++)
{
cout << myNumbers[i] << "\n";
}
return 0;
}

Output :
10
20
30
40
50
#include<iostream>
using namespace std;
int main()
{
int arr[5];

cout<<"Enter any five numbers:\n";


for(int i=0; i<5; i++)
{
cin>>arr[i];
}
cout<<"\nThe array \"arr\" with their indexing is as follows:\n";
for(int i=0; i<5; i++)
{
cout<<"arr["<<i<<"] = "<<arr[i]<<endl;
}
cout<<endl;
return 0;
}

Output: Enter any five numbers


1
2
3
4
5
The array "arr" with their indexing is as follows:

arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=5

Linear Array - Insertion Operation

In the insertion operation, we are adding one or more elements to the


array. Based on the requirement, a new element can be added at the
beginning, end, or any given index of array. This is done using input
statements of the programming languages.

Algorithm

Following is an algorithm to insert elements into a Linear Array until we


reach the end of the array −

1. Start
2. Create an Array of a desired datatype and size.
3. Initialize a variable 'i' as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop

Insertion in an array refers to the process of adding a new element to a


specific position within the array while shifting the existing elements to
make room for the new element.

Here's a simple algorithm in simple language to perform an insertion in


an array:
1. Define the array and the element to be inserted.
2. Determine the position where the element should be inserted.
3. Shift the elements after the insertion position to the right by one
index to make room for the new element.
4. Insert the new element at the desired position.
5. Update the length of the array.

Example

Here, we see a practical implementation of insertion operation, where


we add data at the end of the array –

Add an Element to the End of an Array

This program prompts the user to enter 5 numbers or elements for an


array, followed by the element to insert at the end of the given array.

#include<iostream>
using namespace std;
int main()
{
int arr[6], i, elem;
cout<<"Enter 5 Array Elements: ";
for(i=0; i<5; i++)
cin>>arr[i];
cout<<"\n Enter Element to Insert: ";
cin>>elem;
arr[i] = elem;
cout<<"\nThe New Array is:\n";
for(i=0; i<6; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Output:
Enter 5 Array Elements:
50
43
37
35
16
Enter Element to insert: 99

The New Array is : 50 43 37 35 16 99


C++ Program to Add an Element in the Array at the Beginning

// C++ program to demonstrate how to add an element at the


// beginning of an array
#include <iostream>
using namespace std;

int main()
{
// Original array with some unused space at the end
int arr[6] = { 10, 20, 30, 40, 50 };
int n = 5; // Number of elements in the array

cout << "Original Array: ";


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;

// Element to add
int element_to_add = 5;

// Shift all elements one position to the right


for (int i = n; i > 0; i--) {
arr[i] = arr[i - 1];
}

// Add the element at the beginning of the array


arr[0] = element_to_add;

// Print the array


cout << "Updated Array: ";
for (int i = 0; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

Output
Original Array: 10 20 30 40 50
Updated Array: 5 10 20 30 40 50
Insert an Array Element at a Specific Position
To insert an element in an array in C++ programming, you have to ask
the user to enter the size and elements of the array. And then ask to enter
the element to insert and at what position, as shown in the program
given below:
After inserting the element at the desired position, don't forget to display
the new array on the screen.

#include<iostream>
using namespace std;
int main()
{
int arr[10], i, elem, pos, n;
cout<<"Enter the Size for Array: ";
cin>>n;
cout<<"Enter Array Elements: ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"\nEnter Element to Insert: ";
cin>>elem;
cout<<"At What Position ? ";
cin>>pos;
for(i=n; i>=pos; i--)
arr[i] = arr[i-1];
arr[i] = elem;
n++;
cout<<"\nThe New Array is:\n";
for(i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Output :
Enter the Size for Array: 5
Enter Array Elements: 1 2 3 4 5
Enter Element to Insert: 6
At What Position ? 6
The New Array is:
1 2 3 4 5 6

Array - Deletion Operation

In this array operation, we delete an element from the particular index of


an array. This deletion operation takes place as we assign the value in
the consequent index to the current index.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer


such that K<=N. Following is the algorithm to delete an element
available at the Kth position of LA.

1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Example

Following are the implementations of this operation in various


programming languages

#include <iostream>
using namespace std;
int main()
{
int LA[3] = {1,3,5};
int i, n=3;
cout << "The original array elements are :"<<endl;
for(i = 0; i<n; i++)
{
cout <<LA[i] << endl;
}
for(i = 1; i<n; i++)
{
LA[i] = LA[i+1];
n = n - 1;
}
cout << "The array elements after deletion :"<<endl;
for(i = 0; i<n; i++)
{
cout <<LA[i] <<endl;
}
}

Output
The original array elements are :
1
3
5
The array elements after deletion :
1
5
Searching Methods
 Searching is the fundamental process of locating a specific
element or item within a collection of data.
 This collection of data can take various forms, such as arrays, lists,
trees, or other structured representations.
 The primary objective of searching is to determine whether the
desired element exists within the data, and if so, to identify its
precise location or retrieve it.
 It plays an important role in various computational tasks and real-
world applications, including information retrieval, data analysis,
decision-making processes, and more.

1) Linear Search Method


The linear search algorithm is defined as a sequential search
algorithm that starts at one end and goes through each element of a
list until the desired element is found; otherwise, the search
continues till the end of the dataset.
Linear Search Algorithm
Linear search is a method for searching for an element in a collection
of elements. In linear search, each element of the collection is visited one
by one in a sequential fashion to find the desired element. Linear search
is also known as sequential search.

Algorithm for Linear Search:


The algorithm for linear search can be broken down into the following
steps:
 Start: Begin at the first element of the collection of elements.
 Compare: Compare the current element with the desired element.
 Found: If the current element is equal to the desired element, return
true or index to the current element.
 Move: Otherwise, move to the next element in the collection.
 Repeat: Repeat steps 2-4 until we have reached the end of collection.
 Not found: If the end of the collection is reached without finding the
desired element, return that the desired element is not in the array.

Example of Linear Search:


In the Example We will search for value 11.

Step 1: We start with an array of random values.

[ 12, 8, 9, 11, 5, 11]

Step 2: We look at the first value in the array, is it equal to 11?

[ 12, 8, 9, 11, 5, 11]


Step 3: We move on to the next value at index 1, and compare it to 11 to see if it is
equal.

[ 12, 8, 9, 11, 5, 11]

Step 4: We check the next value at index 2.

[ 12, 8, 9, 11, 5, 11]

Step 5: We move on to the next value at index 3. Is it equal to 11?

[ 12, 8, 9, 11, 5, 11]


We have found it.Value 11 is found at index 3.Returning index position 3.
Linear Search is finished.

You might also like