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

Lab Report 05

Uploaded by

akammiea Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab Report 05

Uploaded by

akammiea Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ABSTRACT (LAB TASK 05)

Array Operations (Insertion, Deletion)

Md. Ahadur Rahman Munshi


EMCS-1108026

DATA STRUCTURE &


ALGORITHM LAB
EMCS-526
Problem Statement 01:
Implement Array INSERTION

Analysis:
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. Following can be a
situation with array insertion:
• Insertion at the beginning of an array
• Insertion at the given index of an array
• Insertion after the given index of an array
• Insertion before the given index of an array
• Insertion at the end of an array

Algorithm:
Insertion at the beginning of an array
IF N = MAX, return
ELSE
N=N+1

For All Elements in A


Move to next adjacent location

A[FIRST] = New_Element

Insertion at the given index of an array


IF N = MAX, return
ELSE
N=N+1

SEEK Location index

For All Elements from A[index] to A[N]


Move to next adjacent location

A[index] = New_Element

Insertion after the given index of an array


IF N = MAX, return
ELSE
N=N+1

SEEK Location index

For All Elements from A[index + 1] to A[N]


Move to next adjacent location

A[index + 1] = New_Element

Insertion before the given index of an array


IF N = MAX, return
ELSE
N=N+1

SEEK Location index


For All Elements from A[index - 1] to A[N]
Move to next adjacent location

A[index - 1] = New_Element

Insertion at the end of an 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

Source Code:
#include <iostream>
using namespace std;

const int MAX_SIZE = 100;

void displayArray(int arr[], int size) {


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

void insertAtBeginning(int arr[], int &size, int value) {


if (size >= MAX_SIZE) {
cout << "Array is full. Cannot insert at the beginning." << endl;
return;
}
for (int i = size; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = value;
size++;
}

void insertAtIndex(int arr[], int &size, int index, int value) {


if (size >= MAX_SIZE || index < 0 || index > size) {
cout << "Invalid index or array is full. Cannot insert." << endl;
return;
}
for (int i = size; i > index; i--) {
arr[i] = arr[i - 1];
}
arr[index] = value;
size++;
}

void insertAfterIndex(int arr[], int &size, int index, int value) {


if (size >= MAX_SIZE || index < 0 || index >= size) {
cout << "Invalid index or array is full. Cannot insert." << endl;
return;
}
for (int i = size; i > index + 1; i--) {
arr[i] = arr[i - 1];
}
arr[index + 1] = value;
size++;
}

void insertBeforeIndex(int arr[], int &size, int index, int value) {


if (size >= MAX_SIZE || index <= 0 || index > size) {
cout << "Invalid index or array is full. Cannot insert." << endl;
return;
}
for (int i = size; i > index - 1; i--) {
arr[i] = arr[i - 1];
}
arr[index - 1] = value;
size++;
}

void insertAtEnd(int arr[], int &size, int value) {


if (size >= MAX_SIZE) {
cout << "Array is full. Cannot insert at the end." << endl;
return;
}
arr[size] = value;
size++;
}

int main() {
int arr[MAX_SIZE] = {1, 2, 3, 4, 5};
int size = 5;

cout << "Original array\t\t: ";


displayArray(arr, size);
cout<<endl << "Insertion" << endl;

insertAtBeginning(arr, size, 10);


cout << "At the beginning\t: ";
displayArray(arr, size);
cout<<endl;

insertAtIndex(arr, size, 2, 20);


cout << "At index 2\t\t: ";
displayArray(arr, size);
cout<<endl;

insertAfterIndex(arr, size, 3, 30);


cout << "After index 3\t\t: ";
displayArray(arr, size);
cout<<endl;

insertBeforeIndex(arr, size, 4, 40);


cout << "Before index 4\t\t: ";
displayArray(arr, size);
cout<<endl;

insertAtEnd(arr, size, 50);


cout << "At the end\t\t: ";
displayArray(arr, size);

return 0;
}
Input-Output

01

Problem Statement 02:


Implement Array DELETION

Analysis:
In the deletion operation, we are deleting one or more elements from the array. Based on the
requirement, an element can be deleted from the beginning, end or any given index of array.
Following can be a situation with array deletion:
• Delete from the beginning of an array
• Delete from the given index of an array
• Delete after the given index of an array
• Delete before the given index of an array
• Delete from the end of an array

Algorithm:
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

Source Code:
#include <iostream>
using namespace std;

const int MAX_SIZE = 100;


void displayArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

void deleteFromBeginning(int arr[], int &size) {


if (size <= 0) {
cout << "Array is empty. Cannot delete from the beginning." << endl;
return;
}
for (int i = 0; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
}

void deleteFromIndex(int arr[], int &size, int index) {


if (size <= 0 || index < 0 || index >= size) {
cout << "Invalid index or array is empty. Cannot delete." << endl;
return;
}
for (int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
}

void deleteAfterIndex(int arr[], int &size, int index) {


if (size <= 0 || index < 0 || index >= size - 1) {
cout << "Invalid index or no element after this index to delete." << endl;
return;
}
for (int i = index + 1; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
}

void deleteBeforeIndex(int arr[], int &size, int index) {


if (size <= 0 || index <= 1 || index > size) {
cout << "Invalid index or no element before this index to delete." << endl;
return;
}
for (int i = index - 2; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
}

void deleteFromEnd(int arr[], int &size) {


if (size <= 0) {
cout << "Array is empty. Cannot delete from the end." << endl;
return;
}
size--;
}

int main() {
int arr[MAX_SIZE] = {1, 2, 3, 4, 5};
int size = 5;

cout << "Original array\t\t: ";


displayArray(arr, size);
cout<< endl << "Deleting:" << endl;

deleteFromBeginning(arr, size);
cout << "From the beginning\t: ";
displayArray(arr, size);
cout<< endl;

deleteFromIndex(arr, size, 2);


cout << "From index 2\t\t: ";
displayArray(arr, size);
cout<< endl;

deleteAfterIndex(arr, size, 1);


cout << "After index 1\t\t: ";
displayArray(arr, size);
cout<< endl;

deleteBeforeIndex(arr, size, 2);


cout << "Before index 2\t\t: ";
displayArray(arr, size);
cout<< endl;

deleteFromEnd(arr, size);
cout << "From the end\t\t: ";
displayArray(arr, size);

return 0;
}
Input-Output

01

You might also like