【问题标题】:C programming : Linked ListsC 编程:链表
【发布时间】:2013-05-24 20:41:57
【问题描述】:

我正在尝试使用链接列表的二维数组来实现一个程序,以存储产品列表及其数量。目前,我只完成了添加和显示第一个数组元素 t[0][0] 列表中的内容的函数。添加产品名称和数量时没有错误,但是当我尝试显示列表时,我没有得到任何结果。你能检查我是否犯了一些错误吗?感谢您的帮助。

typedef struct object product, *pprod;
struct object{
    char name[100];
    int quantity;
    pprod next;
};

product t[4][3];


int is_empty(pprod p)
{
    if(p == NULL)
        return 1;
    else
        return 0;
}
void show_info(pprod p)
{
    while(p != NULL)
    {
        printf("%s\t%d\n",
               p->name, p->quantity);
        p = p->next;
    } }

void get_data(pprod p)
{
    printf("name: ");
    scanf("%s",p->name);
    printf("quantity: ");
    scanf("%d",&p->quantity);
    p->next = NULL;
}

pprod insert_beginning(pprod p)
{
    pprod new;
    if((new = malloc(sizeof(product))) == NULL)
        printf("Error allocating memory\n");
    else
    {
        get_data(new);
        new->next = p; } p = new;
    return p;
}


int main(int argc, char *argv[]){
    insert_beginning(t[0][0].next);
    show_info(t[0][0].next);
    printf("%d",is_empty(t[0][0].next));


}

【问题讨论】:

  • 不相关,但您可能需要重新考虑将变量命名为“新”。 'new' 是 C++ 中常用的保留关键字,读起来可能有点混乱。
  • 糟糕。对此感到抱歉,从未在 C++ 中做过任何事情。感谢您的提醒

标签: c pointers data-structures linked-list


【解决方案1】:

你至少想要这样的东西:

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

typedef struct object product, *pprod;
struct object{
    char name[100];
    int quantity;
    pprod next;
};

product t[4][3];


int is_empty(pprod p)
{
    if(p == NULL)
        return 1;
    else
        return 0;
}
void show_info(pprod p)
{
    while(p != NULL) {
        printf("%s\t%d\n",
                p->name, p->quantity);
        p = p->next;
    }
}

void get_data(pprod p)
{
    printf("name: ");
    scanf("%s",p->name);
    printf("quantity: ");
    scanf("%d",&p->quantity);
    p->next = NULL;
}

pprod insert_beginning(pprod *p)
{
    pprod new;
    if ((new = malloc(sizeof(product))) == NULL) {
        printf("Error allocating memory\n");
        assert(0);
    } else {
        get_data(new);
        new->next = *p;
        *p = new;
    }
    return *p;
}


int main(int argc, char *argv[])
{
    insert_beginning(&t[0][0].next);
    show_info(t[0][0].next);
    printf("%d",is_empty(t[0][0].next));
    return 0;
}

但这显然还是浪费了t[0][0]中namequantity的所有存储空间。你可以通过改变来解决这个问题

product t[4][3];

pprod t[4][3];

int main(int argc, char *argv[])
{
        insert_beginning(&t[0][0].next);
        show_info(t[0][0].next);
        printf("%d",is_empty(t[0][0].next));
        return 0;
}

int main(int argc, char *argv[])
{
        insert_beginning(&t[0][0]);
        show_info(t[0][0]);
        printf("%d",is_empty(t[0][0]));
        return 0;
}

我也不明白您为什么要将 t 组织为二维链表。 (编辑:卡拉在 cmets 中解释了这一点)

show_all() 中的错误

您在 show_all() 中有两个差 1 的错误

void show_all()
{
    int i,j;
    for(i=0;i<=3;i++){
        for(j=0;j<=2;j++){
            printf("C:%dA:%d\n",i,j);
            show_info(t[i][j]);
        }
    }
}

您已将t 的尺寸更改为t[3][2],因此应该改为i = 0; i &lt; 3; i++j = 0; j &lt; 2; j++。以下是 C 程序员通常会如何处理这个问题:

#define ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0]))

void show_all()
{
    int i,j;
    for(i=0;i<ARRAY_SIZE(t);i++){
        for(j=0;j<ARRAY_SIZE(t[0]);j++){
            printf("C:%dA:%d\n",i,j);
            show_info(t[i][j]);
        }
    }
}

【讨论】:

  • 问题提出其基于一个仓库,由4个走廊组成,每个走廊有3个货架。产品按产品名称和数量放置在货架内。我想我可以制作二维数组来选择 [走廊] [货架],并在产品的链接列表中。如何避免浪费存储空间?
  • @CarlaPateiro,这是有道理的。
  • @CarlaPateiro,我已对我的回答进行了编辑,并为您提供了更多建议。
  • 通过编辑,“t[4][3]”现在是一个二维指针数组,对吗?谢谢建议:)
  • 正确^_^。如果您对所有内容都使用insert_beginning(),并且它调用malloc(),那么这就是合乎逻辑的变化。
猜你喜欢
  • 1970-01-01
  • 2022-01-02
  • 2012-04-21
  • 2019-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多