c - 如何删除数组中找到的元素并将数组元素向左移动?

标签 c

int search(int a[]) {

   int i,V,index;
   printf("Enter the element (V),That you want to find:>");
   scanf("%d",&V);

   for (i=0;i<N;i++) {
       if(a[i]==V) {
           V=a[i];
           index=i;
       }
   }
   printf("%d is located in a[%d].",V,index
)

最佳答案

如果您不关心元素的顺序,您可以在 O(1) 时间内删除找到的元素。

// Find the element you're looking for.
int index = find(A, V);

// Stuff the last element into the index found.
A[index] = A[N-1];

// Reduce the total number of elements.
N--;

关于c - 如何删除数组中找到的元素并将数组元素向左移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4491625/

相关文章:

c - 如何在循环中使用 getch() (nCurses)

c - gcc 是否应该能够找到它随附安装的 header ?

c - 回文检查算法

c - 意外的无限循环数组 c

比较字符串中的char与C中的指针

c++ - 如何使用 VS2017 将 float 据从 .txt 文件写入二维数组?

使用 CreateWindowEx 创建一个没有图标的窗口

c++ - 在 c/c++ 中包括结构和编译

c - 导入模块——用 C 语言定制的模块

c - 函数中的双指针取消引用