Array Data Structure

An array is a collection of similar types of elements arranged in contiguous memory allocation. An array is a compartment that can hold a fixed number of items, all of which must be of the same type. The majority of data structures use arrays to adopt their algorithms. The following are key terms to comprehend the notion of Array.

(i) Element – An element is any item that is stored in an array.

(ii) Index – Each element in an extensive range has an arithmetical index that serves to identify the aspect.

Basic Operations on array

Following are the basic operations supported by an array.

  • Traverse − print all the array elements one by one.
  • Insertion − Adds an element at the given index.
  • Deletion − Deletes an element at the given index.
  • Search − Searches an element using the given index or by the value.
  • Update − Updates an element at the given index.

Traverse – This operation is used to iterate over the parts of an array.

C program –

#include <stdio.h>
main() {
   int A[] = {1,3,5,7,8};
   int c = 12, k = 3, n = 7;
   int i = 0, j = n;   
   printf("The original array elements are :\n");
   for(i = 0; i<n; i++) {
      printf("A[%d] = %d \n", i, A[i]);
   }
}

Insertion – The insert operation is used to add one or more attribute values to an array. A feature can be inserted at the end, or even any given indicators of the array depending on the requirement.

C program –

#include <stdio.h>

main() {
   int A[] = {1,3,5,7,8};
   int item = 10, k = 3, n = 7;
   int i = 0, j = n;
   
   printf("The original array elements are :\n");

   for(i = 0; i<n; i++) {
      printf("A[%d] = %d \n", i, A[i]);
   }

   n = n + 1;
	
   while( j >= k) {
A[j+1] = A[j];
      j = j - 1;
   }

A[k] = item;

   printf("The array elements after insertion :\n");

   for(i = 0; i<n; i++) {
      printf("A[%d] = %d \n", i, A[i]);
   }
}

Deletion – It pertains to the removal of respective elements from the array.

#include <stdio.h>

void main() {
   int A[] = {1,9,11,17,18};
   int k = 9, n = 11;
   int i, j;
   
   printf("The original array elements are :\n");
	
   for(i = 0; i<n; i++) {
      printf("A[%d] = %d \n", i, A[i]);
   }
    
   j = k;
	
   while( j < n) {
A[j-1] = A[j];
      j = j + 1;
   }
	
   n = n -1;
   
   printf("The array elements after deletion :\n");
	
   for(i = 0; i<n; i++) {
      printf("A[%d] = %d \n", i, A[i]);
   }
}

Searching – You can search for arrays using either its value or rather its index.

#include <stdio.h>

void main() {
   int A[] = {1,3,5,7,8};
   int item = 5, n = 5;
   int i = 0, j = 0;
   
   printf("The original array elements are :\n");
	
   for(i = 0; i<n; i++) {
      printf("A[%d] = %d \n", i, A[i]);
   }
    
   while( j < n){
      if( A[j] == item ) {
         break;
      }
		
      j = j + 1;
   }
	
   printf("Found element %d at position %d\n", item, j+1);
}

Update – The refresh operation refers to adjusting an existing array element at a given index.

#include <stdio.h>

void main() {
   int A[] = {1,3,5,7,8};
   int k = 3, n = 5, item = 10;
   int i, j;
   
   printf("The original array elements are :\n");
	
   for(i = 0; i<n; i++) {
      printf("A[%d] = %d \n", i, A[i]);
   }
    
   LA[k-1] = item;

   printf("The array elements after updation :\n");
	
   for(i = 0; i<n; i++) {
      printf("A[%d] = %d \n", i, A[i]);
   }
}

0

Leave a Comment

Your email address will not be published. Required fields are marked *

5 × 2 =