如何将数组中的数组指向动态数组? [英] How to point an array inside a dynamic array to something?

查看:190
本文介绍了如何将数组中的数组指向动态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 struct ,如下所示:

  struct Key_Node 
{
int key;
struct Package_Node * next_package;
};

我将创建一个动态数组 struct Key_Node arrayMain [X] code>其中 X 的值将由用户输入,并根据它将创建动态数组。



由于我不知道数组的大小,显然我不能将动态数组的每个指针指向某个东西。那么我在这里做什么?



我有另一个 struct ,看起来像这样。

  struct Package_Node 
{
int bar_code;
float package_weight;
struct Package_Node * next_packaged;
};

Key_Node mainArray [dynamicvalue] p>

package_node totalPackages [dynamicvalue]



是动态数组和链表。我将创建随机包,并使用散列表方法对它们进行排序。如果我的 X 是3,我的随机条形码是10,我会做 10%3 其结果1,所以随机的 package_node 将添加到 mainArray [1] ,列表将增长。

解决方案

似乎你需要一点帮助才能让你开始。看来您被要求实施哈希表。在 C 中,您可以使用 malloc C ++ 您使用,请参阅在什么情况下我使用malloc和新的?



这应该让你开始,希望它有帮助。 b
$ b

  #include< iostream> 
struct Key_Node
{
int key;
struct Package_Node * next_package;
};

struct Package_Node
{
int bar_code;
float package_weight;
struct Package_Node * next_packaged;
};

int main(){
size_t X;
std :: cout<<< 输入X:;
std :: cin>> X;
Key_Node * mainArray = new Key_Node [X]; $($)

for(size_t i = 0; i< X; ++ i){
mainArray [i] .next_package = NULL;
}

// - 其余的程序在这里

delete [] mainArray;

return 0;
}


I have a struct like this:

struct Key_Node
{
    int key;
    struct Package_Node *next_package;
};

I will create a dynamic array "struct Key_Node arrayMain[X]" where the value of X will be entered by the user and depending on it I will create the dynamic array.

Since I don't know the size of the array, obviously I can not point each pointer of the dynamic array to something. So what will I have to do here?

I have another struct that looks like this.

struct Package_Node
{
    int bar_code;
    float package_weight;
    struct Package_Node *next_packaged;
};

Key_Node mainArray[dynamicvalue] and

package_node totalPackages[dynamicvalue]

are a dynamic array and linked list in order. I will be creating random packages and will sort them using hashtable methods. If my X is 3 and my random barcode is 10, I will be doing 10 % 3 which results 1, so the random package_node will be added to mainArray[1] and the list will grow like that.

解决方案

It seems you need a little help to get you started. Looks like you are being asked to implement a hash table. In C you use malloc to allocate dynamic storage, in C++ you use new, see In what cases do I use malloc vs new?

This should get you started, hope it helps.

#include <iostream>
struct Key_Node
{
    int key;
    struct Package_Node *next_package;
};

struct Package_Node
{
    int bar_code;
    float package_weight;
    struct Package_Node *next_packaged;
};

int main() {
    size_t X;
    std::cout << "Enter X: ";
    std::cin >> X;
    Key_Node *mainArray = new Key_Node[X];

    for(size_t i=0; i<X; ++i) {
        mainArray[i].next_package = NULL;
    }

    //-- Rest of your program goes here

    delete [] mainArray;

    return 0;
}

这篇关于如何将数组中的数组指向动态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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