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

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

问题描述

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

什么是删除一个数组并在此过程中的元素使数组较小的最佳方式。

即数组为n的大小,然后我拿出数组中的元素,然后在数组增长由我删除它从量较小。

基本上,我像对待一副扑克牌的排列,一旦我把卡掉在甲板它不应该存在了的顶部。

编辑:我打算自己开车的日子结束前的疯狂,所有帮助我试图交换物品的价值感谢,但它不工作的权利。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;枚举的面孔{王牌= 0,杰克= 10,皇后,国王};
字符* facecheck(INT D);
INT平局(INT甲板上,INT I);
诠释的main()
{
    INT甲板[52],I,N;
    炭西服[4] [9] =
    {
        心
        钻石,
        俱乐部
        黑桃};
    N = 0;    对于(i = 0; I< 52;我++)
    {
        甲板[I] = N;
        Ñ​​++;
     };    对于(i = 0; I< 52;我++)
    {
        如果(I%13 == 0 ||我13%== || 10%,我13 == || 11%,我13 == 12)
            的printf(%S,facecheck(I%13));
        其他的printf(%d个,我13%+ 1);
        的printf(,西服%S \\ n个[我/ 13]);
    }    画(甲板上,我);
    返回0;
}字符* facecheck(INT D)
{
    静态的char *脸[] =
    {
        高手,
        插口,
        女王,
        国王 };    如果(D == ACE)
        返回脸[0];
    其他
    {
        如果(D ==杰克)
            返回脸[1];
        其他
        {
            如果(D ==女王)
                返回脸[2];
            其他
            {
                如果(D ==王)
                    返回脸[3];
            }
        }
    }
}INT平局(INT甲板,int i)以
{
    INT手[5],J,温度[J]。    对于(i = 0; I< 52;我++)
    {
             J =
             };    对于(I = 0; I&小于5;我+ +)
    {
          甲板[I] =手[];
          输出(A卡已经绘\\ n);
          甲板[I] =温度[J-1];
          温度[J] =甲板[I]
          };          回到甲板上;
}


解决方案

您真的不希望被reallocing每次你删除的东西时内存。如果你知道的粗糙尺寸您的甲板的然后选择与您的阵列合适的大小,并保持一个指针列表的当前的结束的。这是一个堆栈

如果你不知道你的甲板的尺寸,并认为它可以得到真正的大以及不断变化的大小,那么你将不得不做一些稍微复杂和实施的链接列表

在C,你有两个简单的方法来声明一个数组。


  1. 在堆栈中,作为一个静态数组

      INT myArray的[16]; // 16整数静态数组


  2. 在堆,作为一个动态分配的数组

      //动态分配的16整数数组
    为int * myArray的=释放calloc(16,sizeof的(INT));


标准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.

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.

EDIT: 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;
}

解决方案

You don't really want to be reallocing memory every time you remove something. If you know the rough size of your deck then choose an appropriate size for your array and keep a pointer to the current end of the list. This is a stack.

If you don't know the size of your deck, and think it could get really big as well as keeps changing size, then you will have to do something a little more complex and implement a linked-list.

In C, you have two simple ways to declare an array.

  1. On the stack, as a static array

    int myArray[16]; // Static array of 16 integers
    

  2. On the heap, as a dynamically allocated array

    // Dynamically allocated array of 16 integers
    int* myArray = calloc(16, sizeof(int));
    

Standard C does not allow arrays of either of these types to be resized. You can either create a new array of a specific size, then copy the contents of the old array to the new one, or you can follow one of the suggestions above for a different abstract data type (ie: linked list, stack, queue, etc).

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

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