目录

点击这里

在上一节中,我们已经了解了插入操作的工作原理。 并不总是需要在数组的末尾插入元素。 以下可能是阵列插入的情况 -

  • 在数组的开头插入
  • 在数组的给定索引处插入
  • 在给定的数组索引之后插入
  • 在给定的数组索引之前插入

在数组的开头插入

当插入发生在开头时,它会导致所有现有数据项向下移动一步。 在这里,我们设计并实现了一种在数组开头插入元素的算法。

算法 (Algorithm)

我们假设A是一个包含N元素的数组。 它可以存储的最大元素数由MAX定义。 我们将首先检查数组是否有任何空的空间来存储任何元素,然后我们继续插入过程。

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

在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]);
   }
}

该程序应产生以下输出 -

输出 (Output)

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

在数组的给定索引处插入

在这种情况下,我们给出了需要插入新数据元素( value )的数组的确切位置( index )。 首先,我们将检查阵列是否已满,如果不是,则我们将从该位置向下移动所有数据元素一步。 这将为新数据元素腾出空间。

算法 (Algorithm)

我们假设A是一个包含N元素的数组。 它可以存储的最大元素数由MAX定义。

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   

在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]);
   }
}

如果我们编译并运行上面的程序,它将产生以下结果 -

输出 (Output)

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

在给定的数组索引后插入

在这种情况下,我们得到一个数组的位置( index ),之后必须插入一个新的数据元素( value )。 只有搜索过程变化,其余活动与前一个示例相同。

算法 (Algorithm)

我们假设A是一个包含N元素的数组。 它可以存储的最大元素数由MAX定义。

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   

在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 <b class="notranslate">value</b> 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]);
   }
}

如果我们编译并运行上面的程序,它将产生以下结果 -

输出 (Output)

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

在给定的数组索引之前插入

在这种情况下,我们得到一个数组的位置( index ),在此之前必须插入一个新的数据元素( value )。 这次我们寻求index-1即在给定指数之前的一个位置,其余的活动与前面的例子相同。

算法 (Algorithm)

我们假设A是一个包含N元素的数组。 它可以存储的最大元素数由MAX定义。

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   

在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 <b class="notranslate">value</b> 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]);
   }
}

如果我们编译并运行上面的程序,它将产生以下结果 -

输出 (Output)

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
↑回到顶部↑
WIKI教程 @2018