Array and Abstract Data Types in Data Structure
ADTs or abstract data types are the ways of classifying data structures by providing a minimal expected interface and some set of methods. It is very similar to when we make a blueprint before actually getting into doing some job, be it constructing a computer or a building. The blueprint comprises all the minimum required logistics and the roadmap to pursuing the job.
Static and Dynamic Arrays:
- Static arrays – Size cannot be changed
- Dynamic arrays – Size can be changed
Memory Representations of Array:
- Elements in an array are stored in contiguous memory locations.
- Elements in an array can be accessed using the base address in constant time → O (1).
- Although changing the size of an array is not possible, one can always reallocate it to some bigger memory location. Therefore resizing in an array is a costly operation.
Operations on Array
- Insertion
- Deletion
- Traversal
Source Code
#include <stdio.h>
// Array Traversal - Printing all elements of array
void ArrayTraversal(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%dth element : %d\n", i + 1, arr[i]);
}
}
// Array Insertion - Adding elements in array
int ArrayInsertion(int arr[], int size, int capacity, int index, int element)
{
if (index > size || index < 0)
{
printf("Please enter valid index...");
}
else if (size == capacity)
{
printf("HouseFull !!!");
}
else
{
if (index == size)
{
arr[index] = element;
}
else
{
for (int i = size - 1; i >= index; i--)
{
arr[i + 1] = arr[i];
}
arr[index] = element;
size++;
}
}
return (size);
}
// Array Deletion - Deleting elements from array
int ArrayDeletion(int arr[], int size, int capacity, int index)
{
if (index < 0 || index > size)
{
printf("Invalid Index...\n");
}
else
{
if (index == size)
{
size--;
}
else
{
for (int i = index; i < size; i++)
{
arr[i - 1] = arr[i];
}
size--;
}
}
return (size);
}
int main()
{
/* code */
int arr[10] = {1, 45, 23, 34, 6, 39, 79, 100};
int capacity = sizeof(arr) / sizeof(int);
int size = 8;
printf("Array before Insertion : \n");
ArrayTraversal(arr, size);
size = ArrayInsertion(arr, size, capacity, 3, 25);
printf("\nArray After Insertion : \n");
ArrayTraversal(arr, size);
printf("Array before Deletion : \n");
ArrayTraversal(arr, size);
size = ArrayDeletion(arr, size, capacity, 3);
size = ArrayDeletion(arr, size, capacity, 8);
printf("\nArray After Deletion : \n");
ArrayTraversal(arr, size);
return 0;
}
Output
Array before Insertion :
1th element : 1
2th element : 45
3th element : 23
4th element : 34
5th element : 6
6th element : 39
7th element : 79
8th element : 100
Array After Insertion :
1th element : 1
2th element : 45
3th element : 23
4th element : 25
5th element : 34
6th element : 6
7th element : 39
8th element : 79
9th element : 100
Array before Deletion :
1th element : 1
2th element : 453th element : 23
4th element : 25
5th element : 34
6th element : 6
7th element : 39
8th element : 799th element : 100
Array After Deletion :
1th element : 1
2th element : 45
3th element : 25
4th element : 34
5th element : 6
6th element : 39
7th element : 79
Comments
Post a Comment