C编程: Linked Lists

标签 c pointers data-structures linked-list

我正在尝试使用链表的二维数组来实现一个程序,以存储产品列表及其数量。目前,我只完成了添加和显示第一个数组元素 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));


}

最佳答案

你至少想要这样的东西:

#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 组织为二维链表。 (编辑:卡拉在评论中解释说)

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 < 3; i++j = 0; j < 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]);
        }
    }
}

关于C编程: Linked Lists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16743335/

相关文章:

c - 如何修改已传递给 C 函数的指针?

c - 在 for 循环中使用结构的问题

c++ - 带有 boost::shared_ptr 的空指针?

database-design - 图形和版本控制

c - 使用 miniz 解压 zip 文件会退出,状态为 -1 (TINFL_STATUS_FAILED)

c - 我在 C 中使用 sscanf 时遇到问题

c - Mint 中的 Linux GCC 编译器输出 : What to do with the file?

c - 关闭 0MQ 套接字之前休眠

c - 排序数字(下划线)

algorithm - n组所有组合的交集