【问题标题】:C programming: Best data structure for this situation [closed]C 编程:这种情况下的最佳数据结构 [关闭]
【发布时间】:2013-05-24 16:34:31
【问题描述】:

我必须为上面的图像制作一个动态结构。图像代表一个仓库,有 4 个走廊,每个走廊有 3 个货架。货架内有少量产品(P1 P3、P5 等)以及每个产品的数量。

我想知道您是否可以帮助我为这个问题选择最好和最简单的解决方案。 也许是指向结构数组的二维指针数组?或链接列表?

【问题讨论】:

  • 货架、走廊和产品的数量是否总是在一定范围内?
  • 嘿。走廊和架子都有固定的编号。但货架内的产品可能会有所不同。
  • 我宁愿使用链表,但这是我个人的看法。
  • 使用其中任何一个。你到底有什么问题?
  • 需要支持哪些操作?这些数据将如何变异?

标签: c pointers data-structures struct


【解决方案1】:
struct {
  char* product_name;
  int quantity;
} product_s;

struct {
  char* shelf_name;
  int num_products;
  struct product_s *products;
} shelf_s;

typdef struct {
  int corridor;
  int num_shelf;
  struct *shelf_s;
} corridor_t;

这将存储您的信息。

下一步:

#define NUMBER XXX
corridor_t *corridor = (corridor_t*)malloc(sizeof(*corridor) * NUMBER);

用法清楚吗?

【讨论】:

  • 谢谢。真是大错特错。 xD
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多