Member Avatar for Kristjon

Hello guys ! Hope you're doing well.
I have faced a problem in declaring linked lists inside a linked list.
I made a picture because it speaks more than 1000 words
list1

Here's my structure declaration:

typedef struct author *link;
struct author
{
       char name[20];
       link next;
       link down;
};
typedef link LIST;
typedef link pos_author;


typedef struct book *ptr;
struct book
{
       char tittle[20];
       int year;
       int page;
       ptr down;
};
typedef ptr pos_book;

But, the program won't compile because it generates an error in insert-function.
cannot convert author*' tobook' in assignment
cannot convert book*' toauthor
' in assignment

Here is the Inserting Function:

void insert(LIST L)
{
     char ch;
     do{
        printf("Author:   ");

        pos_author tmp=(pos_author)malloc(sizeof(struct author);
        scanf("%s",tmp->name);
        tmp->next=NULL;

        do{
        printf("Book: ");
        pos_book temp=(pos_book)malloc(sizeof(struct book));
        scanf("%s %d %d",temp->tittle,&temp->year,&temp->page);
        temp->down=NULL;

        temp->down=tmp->down; // EROOR
        tmp->down=temp;           //ERROR

        printf("\nAnother Book: [Y/N]  ");
        ch=getch();}while(ch=='Y'||ch=='y');

        tmp->next=L->next;
        L->next=tmp;

        printf("Another Author: [Y/N]  ");
        ch=getch();}while(ch=='Y'||ch=='y');
}

Looking forward to get an answer from you. Thanx in advance.

In the declaration of struct author, you define down as type link, which is a typedef for struct author*. What you actually want is for it to be a ptr (that is, struct book*). Thus, you'll want to move the typedef of type ptr up to before the definition of struct author, and change link down; to ptr down;.

typedef struct author *link;
typedef struct book *ptr;

struct author
{
       char name[20];
       link next;
       ptr down;
};
typedef link LIST;
typedef link pos_author;


struct book
{
       char title[20];
       int year;
       int page;
       ptr down;
};
typedef ptr pos_book;

Oh, and you misspelled 'title', BTW. HTH.

Member Avatar for Kristjon

Thank you mate for your help:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.