Hot Posts

Deletion in arrays at the beginning,end and specific location in cpp program Data structure and algorithm

 CODE



#include<iostream>

using namespace std;

int size=4;

int i,pos,count=4;


void dele_start()

{

int a[4]={1,2,3,4};

    cout<<"The array numbers before deletion are"<<endl;

        for ( i = 0; i<size; i++)

        {

            cout<<a[i]<<"  ";

        }

        

       for(i=0;i<size;i++)

       {

        a[i]=a[i+1];

        if(i==3)

        {

        a[i]=NULL;

   count--;

}

   }

        

        cout<<"\nAfter deletion at the begining the array becomes:"<<endl;

        for ( i = 0; i<size-1; i++)

        {

            cout<<a[i]<<"  ";

        }       

}



void dele_end()

{ int a[4]={1,2,3,4};

    cout<<"The array numbers before deletion are"<<endl;

        for ( i = 0; i < size; i++)

        {

            cout<<a[i]<<"  ";

        }

        a[size-1]=NULL;

        count--;

        cout<<"\nAfter deletion at the end the array becomes:"<<endl;

        for ( i = 0; i < size-1; i++)

        {

            cout<<a[i]<<"  ";

        }

        

}


void specific_loc()

{      

int a[4]={1,2,3,4};

cout<<"\nEnter the position do you want to delete:"<<endl;

        cin>>pos;

         cout<<"The array numbers before deletion are"<<endl;

        for ( i = 0; i < size; i++)

        {

            cout<<a[i]<<"  ";

        }

        if(pos<=0 || pos>size)

        {

            cout<<"\ninvalid position!!!"<<endl;

        }

        else{

            for ( i = pos-1; i <size-1; i++)

            {

                a[i]=a[i+1];

            }

            count--;

            cout<<"\nThe updated array is :"<<endl;

            for ( i = 0; i <size-1; i++)

            {

                cout<<a[i]<<"  ";

            }

            

        }

}



int main()

{   int choice;

while (true)

{

  



cout<<"\nEnter your Choice:"<<endl;

    cout<<"\nPress 1 for deletion at begining"<<"\nPress 2: for deletion at end"<<"\nPress 3: for deletion at Specific Location"<<endl;

    cin>>choice;

    switch (choice)

    {

    case 1:

        dele_start();

        break;

    case 2:

        dele_end();

        break;

    case 3:

        specific_loc();

        break;

    default:

    cout<<"\ninvalid input"<<endl;

        break;

    

    }

}

    return 0;

}


OUTPUT


Enter your Choice:


Press 1 for deletion at begining

Press 2: for deletion at end

Press 3: for deletion at Specific Location

1

The array numbers before deletion are

1  2  3  4

After deletion at the begining the array becomes:

2  3  4

Enter your Choice:


Press 1 for deletion at begining

Press 2: for deletion at end

Press 3: for deletion at Specific Location

2

The array numbers before deletion are

1  2  3  4

After deletion at the end the array becomes:

1  2  3

Enter your Choice:


Press 1 for deletion at begining

Press 2: for deletion at end

Press 3: for deletion at Specific Location

3


Enter the position do you want to delete:

2

The array numbers before deletion are

1  2  3  4

The updated array is :

1  3  4

Enter your Choice:


Press 1 for deletion at begining

Press 2: for deletion at end

Press 3: for deletion at Specific Location


Post a Comment

1 Comments