Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Insertion

 Insertion -Data structure


#include<iostream>

using namespace std;


int main()

{

    int arr[50];


    int n;

    cout<<"Enter the size of Array: ";

    cin>>n;


    cout<<"Enter elements of Array: ";

    for(int i=0; i<n; i++)

        cin>>arr[i];


    int index, element;

    cout<<"Enter Index No: ";

    cin>>index;

    if(index<0 || index>n)

    {

        cout<<"Insertion is not possible!"<<endl;

    }

    else{

        cout<<"Enter Element: ";

        cin>>element;


        for(int i=n; i>index; i--)

            arr[i]=arr[i-1];

        arr[index]= element;

        n++;

    }


    cout<<"Your Array is :";

    for(int i=0; i<n; i++)

        cout<<"  "<<arr[i];

}

Post a Comment

0 Comments