Sunday, May 24, 2020

PEP DATA STRUCTURE PRACTICE


DATA STRUCTURE PEP PRACTICE : 


Data Structure is a way to store and organize data so that it can be used efficiently . For example, we can store a list of items using array,linked list,stack,queue,tree,graph.


1. Array : In this topic we will learn   = >


  • --->Array Insertion  
  • --->Array Deletion
  • --->Array Rotation

                                                                       Array Insertion  

#include<iostream>
using namespace std;
int main()
{
int a[50],size,num,pos;
cin>>size;
if(size>50)
{
cout<<"overflow!!"<<endl;
}else
{
for(int i=0;i<size;i++)
{
cin>>a[i];
}
cin>>num>>pos;
for(int i=size-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=num;
size++;
for(int i=0;i<size;i++)
{
cout<<a[i]<<" ";
}
}
}
-----------------------------------------Complexity(worst case) : O(n) ------------------------------------------------------


                                                              Array Deletion


#include<iostream>
using namespace std;
int main()
{
int a[50],size;
cin>>size;
for(int i=0;i<size;i++)
{
cin>>a[i];
}
int pos;
cin>>pos;
if(pos<=0||pos>size)
{
cout<<"invalid condition !"<<endl;
}else
{
for(int i=pos-1;i<size-1;i++)
{
a[i]=a[i+1];
}
size--;
for(int i=0;i<size;i++)
{
cout<<a[i]<<" ";
}
}

}


-----------------------------------------Complexity(worst case) : O(n) ------------------------------------------------------




                                                        Array Rotation
EX : 


Before Rotation-------------------=>  1 , 2 , 3 , 4 , 5 , 6
After Rotation----------------------=>  3 , 4 , 5 , 6 , 1 , 2


#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int d;
cin>>d;
int b[n];
for(int i=0;i<n;i++)
{
b[(i+d)%n]=a[i];
}
for(int i=0;i<n;i++)
{
cout<<b[i]<<" ";
}
}