Insertions de tableaux

Dans la section précédente, nous avons appris comment fonctionne l'opération d'insertion. Il n'est pas toujours nécessaire qu'un élément soit inséré à la fin d'un tableau. Ce qui suit peut être une situation d'insertion de tableau -

  • Insertion au début d'un tableau
  • Insertion à l'index donné d'un tableau
  • Insertion après l'index donné d'un tableau
  • Insertion avant l'index donné d'un tableau

Insertion au début d'un tableau

Lorsque l'insertion se produit au début, tous les éléments de données existants sont décalés d'un cran vers le bas. Ici, nous concevons et implémentons un algorithme pour insérer un élément au début d'un tableau.

Algorithme

Nous supposons A est un tableau avec Néléments. Le nombre maximum d'éléments qu'il peut stocker est défini parMAX. Nous allons d'abord vérifier si un tableau a un espace vide pour stocker un élément, puis nous procéderons au processus d'insertion.

begin

IF N = MAX, return
ELSE
   N = N + 1
   
   For All Elements in A
      Move to next adjacent location
      
   A[FIRST] = New_Element
   
end

Implémentation en C

#include <stdio.h>

#define MAX 5

void main() {
   int array[MAX] = {2, 3, 4, 5};
   int N = 4;        // number of elements in array
   int i = 0;        // loop variable
   int value = 1;    // new data element to be stored in array

   // print array before insertion
   printf("Printing array before insertion −\n");
   
   for(i = 0; i < N; i++) {
      printf("array[%d] = %d \n", i, array[i]);
   }

   // now shift rest of the elements downwards   
   for(i = N; i >= 0; i--) {
      array[i+1] = array[i];
   }

   // add new element at first position
   array[0] = value;

   // increase N to reflect number of elements
   N++;

   // print to confirm
   printf("Printing array after insertion −\n");
   
   for(i = 0; i < N; i++) {
      printf("array[%d] = %d\n", i, array[i]);
   }
}

Ce programme devrait produire le résultat suivant -

Production

Printing array before insertion −
array[0] = 2
array[1] = 3
array[2] = 4
array[3] = 5
Printing array after insertion −
array[0] = 0
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5

Insertion à l'index donné d'un tableau

Dans ce scénario, on nous donne l'emplacement exact (index) d'un tableau où un nouvel élément de données (value) doit être inséré. Nous allons d'abord vérifier si le tableau est plein, si ce n'est pas le cas, nous déplacerons tous les éléments de données de cet emplacement d'un pas vers le bas. Cela fera de la place pour un nouvel élément de données.

Algorithme

Nous supposons A est un tableau avec Néléments. Le nombre maximum d'éléments qu'il peut stocker est défini parMAX.

begin

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
   
end

Implémentation en C

#include <stdio.h>

#define MAX 5

void main() {
   int array[MAX] = {1, 2, 4, 5};
   
   int N = 4;        // number of elements in array
   int i = 0;        // loop variable
   int index = 2;    // index location to insert new value
   int value = 3;    // new data element to be inserted

   // print array before insertion
   printf("Printing array before insertion −\n");

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

   // now shift rest of the elements downwards   
   for(i = N; i >= index; i--) {
      array[i+1] = array[i];
   }

   // add new element at first position
   array[index] = value;

   // increase N to reflect number of elements
   N++;

   // print to confirm
   printf("Printing array after insertion −\n");

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

Si nous compilons et exécutons le programme ci-dessus, il produira le résultat suivant -

Production

Printing array before insertion −
array[0] = 1
array[1] = 2
array[2] = 4
array[3] = 5
Printing array after insertion −
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5

Insertion après l'index donné d'un tableau

Dans ce scénario, on nous donne un emplacement (index) d'un tableau après lequel un nouvel élément de données (value) doit être inséré. Seul le processus de recherche varie, le reste des activités est le même que dans l'exemple précédent.

Algorithme

Nous supposons A est un tableau avec Néléments. Le nombre maximum d'éléments qu'il peut stocker est défini parMAX.

begin

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
   
end

Implémentation en C

#include <stdio.h>

#define MAX 5

void main() {
   int array[MAX] = {1, 2, 4, 5};
   
   int N = 4;        // number of elements in array
   int i = 0;        // loop variable
   int index = 1;    // index location after which value will be inserted
   int value = 3;    // new data element to be inserted

   // print array before insertion
   printf("Printing array before insertion −\n");

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

   // now shift rest of the elements downwards   
   for(i = N; i >= index + 1; i--) {
      array[i + 1] = array[i];
   }

   // add new element at first position
   array[index + 1] = value;

   // increase N to reflect number of elements
   N++;

   // print to confirm
   printf("Printing array after insertion −\n");

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

Si nous compilons et exécutons le programme ci-dessus, il produira le résultat suivant -

Production

Printing array before insertion −
array[0] = 1
array[1] = 2
array[2] = 4
array[3] = 5
Printing array after insertion −
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5

Insertion avant l'index donné d'un tableau

Dans ce scénario, on nous donne un emplacement (index) d'un tableau devant lequel un nouvel élément de données (value) doit être inséré. Cette fois, nous cherchons jusqu'àindex-1 c'est-à-dire qu'un emplacement avant l'index donné, le reste des activités est identique à celui de l'exemple précédent.

Algorithme

Nous supposons A est un tableau avec Néléments. Le nombre maximum d'éléments qu'il peut stocker est défini parMAX.

begin

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
   
end

Implémentation en C

#include <stdio.h>

#define MAX 5

void main() {
   int array[MAX] = {1, 2, 4, 5};
   
   int N = 4;        // number of elements in array
   int i = 0;        // loop variable
   int index = 3;    // index location before which value will be inserted
   int value = 3;    // new data element to be inserted

   // print array before insertion
   printf("Printing array before insertion −\n");

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

   // now shift rest of the elements downwards   
   for(i = N; i >= index + 1; i--) {
      array[i + 1] = array[i];
   }

   // add new element at first position
   array[index + 1] = value;

   // increase N to reflect number of elements
   N++;

   // print to confirm
   printf("Printing array after insertion −\n");

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

Si nous compilons et exécutons le programme ci-dessus, il produira le résultat suivant -

Production

Printing array before insertion −
array[0] = 1
array[1] = 2
array[2] = 4
array[3] = 5
Printing array after insertion −
array[0] = 1
array[1] = 2
array[2] = 4
array[3] = 5
array[4] = 3