Home > Software engineering >  Adding a linked list to another linked list in C programming
Adding a linked list to another linked list in C programming

Time:12-20

I am abeginner trying to add a linked list to another linked list using c. the problem is that the program is entering an infinite loop and i don't know why. And here's the following c code

typedef struct bookInfo
{
    int code;
    struct bookInfo *next;
} bookInfo;

typedef struct subscriber
{
    int code;
    struct bookInfo *books;
    struct subscriber *next;
    struct subscriber *prec;
} subscriber;

typedef bookInfo *Book;
typedef subscriber *Subscriber;
typedef Subscriber *SubscriberList;

void newBook(Book *bk, int val)
{
    bookInfo *new_node = malloc(sizeof(bookInfo));
    bookInfo *last = *bk;
    new_node->code = val;
    new_node->next = NULL;

    if (*bk == NULL)
    {
        *bk = new_node;
    }
    else
    {
        while (last->next != NULL)
            last = last->next;
        last->next = new_node;
    }
}

Subscriber Add_book(Subscriber S, Book Bk)
{
    bookInfo *newNode = malloc(sizeof(bookInfo));
    bookInfo *tmp;
    newNode->next = NULL;
    newNode->code = Bk->code;
    if (S == NULL)
        printf("\nl'abonnee est nulle");
    else
    {
        if (S->books == NULL)
            S->books = newNode;    
        else
        {
            tmp = S->books;
            while (tmp != NULL)
                tmp = tmp->next;
            tmp->next = newNode;
            printf("\nl'ajout du livre a ete effectue");
        };
    }
    return S;
};

Hope you guys can help me and thank you. I don't know if the problem in the function newBook or what and here it's my main function

int main()
{
    book_ref, sub_ref = NULL;
    newSubscriber(&sub_ref);
    bookInfo b1 = {20,NULL};
    Add_book(sub_ref, &b1);
    printf("\n%d : %d", sub_ref->code, sub_ref->books->code);
}

CodePudding user response:

In your code,

while (tmp != NULL) tmp = tmp->next; When this loop ends, tmp is NULL, so the next line will try accessing null pointer.

You should correct it as, while(tmp->next != NULL)

CodePudding user response:

In order to remove the infinite loop, All i had to do was to define the pointer of books in the subscriber struct to NULL

void newBook(Book *bk, int val)
{
    bookInfo *new_node = malloc(sizeof(bookInfo));
    bookInfo *last = *bk;
    new_node->code = val;
    new_node->next = NULL;
    new_node->books = NULL;

    if (*bk == NULL)
    {
        *bk = new_node;
    }
    else
    {
        while (last->next != NULL)
            last = last->next;
        last->next = new_node;
    }
}

Subscriber Add_book(Subscriber S, Book Bk)
{
    bookInfo *newNode = malloc(sizeof(bookInfo));
    bookInfo *tmp;
    newNode->next = NULL;
    newNode->code = Bk->code;
    newNode->books = NULL;
    if (S == NULL)
        printf("\nl'abonnee est nulle");
    else
    {
        if (S->books == NULL)
            S->books = newNode;    
        else
        {
            tmp = S->books;
            while (tmp != NULL)
                tmp = tmp->next;
            tmp->next = newNode;
            printf("\nl'ajout du livre a ete effectue");
        };
    }
    return S;
};

and everything worked.

  • Related