Professional Documents
Culture Documents
Data Structure and Algorithm Chapter Three: Linear Array
Data Structure and Algorithm Chapter Three: Linear Array
Data Structure and Algorithm Chapter Three: Linear Array
Chapter Three
Linear Array
Introduction
• Array is a container which can hold fixed number of items
and these items should be of same type.
data_type array-name[size];
Initialization of array :
or
Example
int marks[5]={50,25,72,45,30};
Marks[0]=50;
Marks[1]=25;
Marks[2]=72;
Marks[3]=45;
Marks[4]=30;
Accessing Linear Array Elements
array.
#define MAX 15 {
{ }
cout<<"\n\nStored Data in array: ";
int i,a[MAX],no,pos,n, y;
for(i=0;i<n;i++)
y= MAX-3; {
Elements is"<<y<<endl; }
cout<<"\n\nEnter position number of your insertion: ";
cin>>n;
cin>>pos;
if(n>y) if(pos>n)
{
cout<<“the max size of the array is <<y<<endl;
cout<<"\n\nThis is out of range";
else { }
Cont.
else
{
cout<<"\n\nEnter new number: ";
cin>>no;
--pos;
for(i=n;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=no;
La insertion2.cpp cout<<"\n\nNew data in array: ";
for(i=0;i<n+1;i++)
{
cout<<a[i]<<" ";
}
}
}
return 0;
}
Deletion Operation
• Deletion refers to removing an existing element from the
array and re-organizing all elements of an array.
1.Start
2. Set N=k+1
Set A[N-1]=A[N]
N=N+1
4. Exit
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream> else
#define MAX 15 {
cout<<"\n\nEnter position the element to delete: "; cout<<"\n\nNew data in array: ";
cin>>pos; for(i=0;i<n-1;i++)
if(pos>n) {
cout<<"\n\nThis is out of range"; cout<<a[i]<<" ";
else }
{ }
--pos; }
for(i=pos;i<n;i++) return 0;
{ }
a[i]=a[i+1];
}
La deletion 2.cpp
Search Operation
• You can perform a search for array element based on its
value or its index.
1. Start
5. Exit
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream> while(j<n){
if(a[j]==item){
using namespace std;
break;
int main() }
{ j=j+1;
}
int i, n=5, item=4, j=0; if(j>=n){
int a[]={2,4,6,4,10}; cout<<"\n\n element "<<item<<" not
found "<<endl;
cout<<"\n\nOriginal elements }
of the array are "; else{
for(i=0;i<n;i++) cout<<"\n\nfound element "<<item<<" at
position "<<j+1<<endl;
{
}
cout<<a[i]<<" "; cout<<"\n\nfound element "<<item<<" at position
} "<<j+1<<endl;
return 0;
} LA search.cpp
Implementation
• search for array element based on its value
#include<iostream> for(i=0; (i<m) && (A[i] !=find); i++)
using namespace std; continue;
int main() if(i==m)
{ cout<<"\n\n"<<find<<" is not in the
intA[] = {8,25,36,44,52,60,75,89}; list"<< endl;
cin>>find;
LA search2.cpp
Update Operation
• Update operation refers to updating an existing element from
the array at a given index.
Algorithm
1. Start
3. Stop
Implementation
• Below is an example of implementation of the above algorithm.
#include<iostream> a[k-1]=item;
cout<<"\n\nUpdated elements
using namespace std;
of the array are ";
int main()
{ for(i=0;i<n;i++)
{
int i, n=5, item=7, j=0, k=3; cout<<a[i]<<" ";
int a[]={2,4,6,8,10}; }
return 0;
cout<<"\n\nOriginal elements of the array are "; }
for(i=0;i<n;i++)
{
LA update.cpp
cout<<a[i]<<" ";
} LA search and update.cpp
Ex.
• Write an algorithm and C++ implementation that
search and update a linear array