从C中的数组中删除元素 [英] Removing elements from an array in C

查看:30
本文介绍了从C中的数组中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有一个关于 C 中数组的简单问题

I just have a simple question about arrays in C

从数组中删除元素并在此过程中缩小数组的最佳方法是什么.

What's the best way to remove elements from an array and in the process make the array smaller.

即数组大小为 n,然后我从数组中取出元素,然后数组随着我从中删除它的数量变小.

i.e the array is n size, then I take elements out of the array and then the array grows smaller by the amount that I removed it from.

基本上,我将阵列视为一副牌,一旦我从牌组顶部取下一张牌,它就不应该再在那里了.

basically I'm treating the array like a deck of cards and once I take a card off the top of the deck it shouldn't be there anymore.

我会在一天结束之前让自己发疯,感谢所有帮助我正在尝试价值交换的事情,但它不起作用.

I'm going to drive myself crazy before the end of the day, thanks for all the help I'm trying the value swapping thing but it's not working right.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

enum faces{Ace = 0, Jack = 10, Queen, King};
char * facecheck(int d); 
int draw(int deck, int i); 
int main() 
{ 
    int deck[52], i, n;
    char suits[4][9] = 
    {
        "Hearts",
        "Diamonds",
        "Clubs",
        "Spades"};


    n = 0;

    for(i = 0; i<52; i++)
    {
        deck[i] = n;
        n++;
     };



    for(i=0; i<52; i++)
    {       
        if(i%13 == 0 || i%13 == 10 || i%13 == 11 || i%13 == 12)
            printf("%s ", facecheck(i%13) );
        else printf("%d ", i%13+1);
        printf("of %s \n", suits[i/13]);
    }

    draw(deck, i);


    return 0; 
}  

char * facecheck(int d)
{
    static char * face[] = 
    {
        "Ace",
        "Jack",
        "Queen",
        "King" };

    if(d == Ace)
        return face[0];
    else
    {
        if(d == Jack) 
            return face[1];
        else
        {
            if(d == Queen)
                return face[2];
            else 
            { 
                if(d == King)
                    return face[3];
            }
        }
    } 
}

int draw(int deck,int i ) 
{ 
    int hand[5], j, temp[j];

    for(i=0; i<52; i++)
    {
             j = i
             }; 

    for(i = 0; i < 5; i++)
    {
          deck[i] = hand[]; 
          printf("A card has been drawn \n");
          deck[i] = temp[j-1];
          temp[j] = deck[i];
          };

          return deck;
}

推荐答案

实际上有两个不同的问题.第一个是将数组的元素保持在适当的顺序,以便在删除元素后没有漏洞".第二个实际上是调整数组本身的大小.

There are really two separate issues. The first is keeping the elements of the array in proper order so that there are no "holes" after removing an element. The second is actually resizing the array itself.

C 中的数组被分配为固定数量的连续元素.没有办法实际删除数组中单个元素使用的内存,但可以移动元素以填充通过删除元素产生的空洞.例如:

Arrays in C are allocated as a fixed number of contiguous elements. There is no way to actually remove the memory used by an individual element in the array but the elements can be shifted to fill the hole made by removing an element. For example:

void remove_element(array_type *array, int index, int array_length)
{
   int i;
   for(i = index; i < array_length - 1; i++) array[i] = array[i + 1];
}

静态分配的数组无法调整大小.动态分配的数组可以使用 realloc() 调整大小.这可能会将整个数组移动到内存中的另一个位置,因此所有指向数组或其元素的指针都必须更新.例如:

Statically allocated arrays can not be resized. Dynamically allocated arrays can be resized with realloc(). This will potentially move the entire array to another location in memory, so all pointers to the array or to its elements will have to be updated. For example:

remove_element(array, index, array_length);  /* First shift the elements, then reallocate */
array_type *tmp = realloc(array, (array_length - 1) * sizeof(array_type) );
if (tmp == NULL && array_length > 1) {
   /* No memory available */
   exit(EXIT_FAILURE);
}
array_length = array_length - 1;
array = tmp;

如果请求的大小为 0,或者出现错误,realloc 将返回一个 NULL 指针.否则它返回一个指向重新分配的数组的指针.临时指针用于在调用 realloc 时检测错误,因为除了退出之外,还可以保留原始数组原样.当 realloc 无法重新分配数组时,它不会改变原始数组.

realloc will return a NULL pointer if the requested size is 0, or if there is an error. Otherwise it returns a pointer to the reallocated array. The temporary pointer is used to detect errors when calling realloc because instead of exiting it is also possible to just leave the original array as it was. When realloc fails to reallocate an array it does not alter the original array.

请注意,如果数组很大或者删除了很多元素,那么这两个操作都会相当慢.如果优先考虑有效的插入和删除,还可以使用其他数据结构,例如链表和哈希.

Note that both of these operations will be fairly slow if the array is large or if a lot of elements are removed. There are other data structures like linked lists and hashes that can be used if efficient insertion and deletion is a priority.

这篇关于从C中的数组中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆