3

Im trying to implement a program using a bi-dimensional array of linked lists, to store lists of products and their quantity. For now, i've only done functions do add and show what's inside the list of the first array element t[0][0]. There's no error's when I add the product name and quantity, but when I try to show the list, I get no result. Can you check if im making some mistakes? Thanks for the help.

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));


}
2
  • 2
    Not related, but you may want to reconsider naming variables 'new'. 'new' is a commonly reserved keyword in c++ and it can be a bit confusing to read. Commented May 24, 2013 at 20:48
  • Oops. Sorry about that, never did anything in C++. And thanks for the heads up Commented May 24, 2013 at 20:55

1 Answer 1

1

You at least want something like:

#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;
}

But this obviously still wastes all the storage space for name and quantity in t[0][0]. You can fix that by changing

product t[4][3];

to

pprod t[4][3];

and

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

to

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

I also don't get why you want to organize t as a two dimensional linked list. (EDIT: Carla explained that in the comments)

Error in show_all()

You had two off by 1 errors in show_all()

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]);
        }
    }
}

You've changed the dimensions of t to be t[3][2] so it should be i = 0; i < 3; i++ and j = 0; j < 2; j++ instead. Here's how C programmers would usually handle this:

#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]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

The problem proposed its based on a warehouse, composed by 4 corridors, each corridor has 3 shelfs. The products are placed inside the shelfs, by product name and quantity. I thought I could make and 2d array to select [corridor][shelf], and inside a linked list for the products. How can I avoid wasting that storage space?
@CarlaPateiro, I've made an edit to my answer and have one more suggestion for you.
With the edit, "t[4][3]" its now an 2d-array of pointers right? Thanks the suggestion :)
right ^_^. If you're using insert_beginning() for everything and it calls malloc() then that's the logical change.
Sorry to bother you again Scott. When I use the insert_beginning() function to add products in another array position, like [0][2], they duplicate in the position [1][0]. Do you know why? Thanks :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.