-
1 Attachment(s)
A List of Linked Lists
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 :D
Here's my structure declaration:
Code:
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*' to `book*' in assignment
cannot convert `book*' to `author*' in assignment
Here is the Inserting Function:
Code:
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.
-
Code:
typedef struct author *link;
struct author
{
char name[20];
link next;
link down;
};
Your "down" element has the wrong type. You want it to point to a "book" structure, so it should be of type "ptr" or "pos_book" or "struct book *".
Bye, Andreas
-
The first problem is you're creating a lot of pointer typedef's, which add very little, and obscure a lot.
The second problem is trying to do too much in one function.
Whenever I create linked list code, I start with a few simple utility functions to manage some of the detail.
Eg.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct book {
char title[20];
int year;
int pages;
struct book *next;
} book;
typedef struct author {
char name[30];
struct author *next;
struct book *wrote;
} author;
// Functions to append a node to the end of a given list
// Add other functions to say
// - insert at start
// - insert in the middle
// - remove a node from a list
void appendAuthor ( author **list, author *node ) {
author *temp = *list;
if ( temp == NULL ) {
*list = node;
} else {
while ( temp->next != NULL ) {
temp = temp->next;
}
temp->next = node;
}
}
void appendBook ( book **list, book *node ) {
book *temp = *list;
if ( temp == NULL ) {
*list = node;
} else {
while ( temp->next != NULL ) {
temp = temp->next;
}
temp->next = node;
}
}
// Functions to allocate and initialise nodes
// of the appropriate types.
author *createAuthor ( void ) {
author *a = malloc( sizeof(*a) );
if ( a ) {
strcpy(a->name,"Anon");
a->next = NULL;
a->wrote= NULL;
}
return a;
}
book *createBook ( void ) {
book *a = malloc( sizeof(*a) );
if ( a ) {
strcpy(a->title,"Blank");
a->pages = 0;
a->year = 0;
a->next = NULL;
}
return a;
}
void doit ( author **list ) {
author *a = createAuthor();
book *b = createBook();
appendAuthor(list,a);
appendBook(&a->wrote,b);
}
int main(void)
{
author *list = NULL;
doit(&list);
return 0;
}