1

Say I want to create a list of books, and those books have one or more authors, as well as main characters in the book. What is the best way to create structures for this? Would the below be accurate, or different set up?

struct name
{
char prefix[5];
char first[50];
char middle[50];
char last[50];
char suffix[5];
struct name *next; /* linked list   */
struct name *previous; /* linked list   */
};


struct book
{
 struct name authors;  
 struct name main_characters;
char title[100];   
char publisher[100]; 

struct book *next; struct book *previous;

};
1
  • 2
    authors and main_characters are the heads of lists aren't they? They should be pointers.
    – user325117
    Mar 20, 2016 at 13:43

3 Answers 3

2

I think your plan is fine, using pointers for authors and main_characters. As you're using intrusive linked lists for the books and the authors and main characters, you might find it helpful to factor out the linked list structure and operations.

If you declare a structure like this:

struct node
{
    struct node *next;
    struct node *previous;
};
typedef struct node node;

You can embed it into each of your types as the first element:

struct name
{
    node linked_list;
    char name_prefix[10];
    char name_first[50];
    char name_middle[50];
    char name_last[50];
    char name_suffix[5];
};

struct book
{
    node linked_list;
    name *authors;  
    name *main_characters;

    /* variables for book */
    char title[100];   /* the title of the book */
    char publisher[100]; /* publisher */
    //etc.
};

This makes your types convertible to the node type. You can then define linked list operations in terms of the node type:

void 
node_add_node(node **head, node *object)
{
    if (*head == NULL) {
        *head = object;
    }
    else {
        node *current, *previous;
        for (current = *head; current != NULL; current = current->next) {
            previous = current;
        }
        previous->next = object;
    }
}

And then define type-safe operations to add books to a list of books and names to books:

void 
books_add_book(struct book **books, struct book *book)
{
    node_add_node((node**)books, (node*)book);
}

void
book_add_author(struct book *book, struct name *author)
{
    node_add_node((node**)&book->authors, (node*)author);
}

void
book_add_main_character(struct book *book, struct name *character)
{
    node_add_node((node**)&book->main_characters, (node*)character);
}

Then, implement the constructors:

void node_init(node *node)
{
    node->previous = NULL;
    node->next = NULL;
}

struct book *
book_create(const char *title, const char *publisher)
{
    struct book *b = malloc(sizeof(book));
    if (b) {
        node_init(&b->linked_list);
        b->authors = NULL;
        b->main_characters = NULL;
        strcpy(b->title, title);
        strcpy(b->publisher, publisher);
    }
    return b;
}

struct name *
name_create(const char *prefix, const char *first, const char *middle, 
    const char *last, const char *suffix)
{
    name *n = malloc(sizeof(name));
    if (n) {
        node_init(&n->linked_list);
        strcpy(n->name_prefix, prefix);
        strcpy(n->name_first, first);
        strcpy(n->name_middle, middle);
        strcpy(n->name_last, last);
        strcpy(n->name_suffix, suffix);
    }
    return n;
}

And then you can create books like this (NOTE: I increased the size of your name_prefix to 10):

 struct book *books = NULL;
 struct book *b = book_create("War and Peace", "Wordsworth");
 struct name *n = name_create("Count", "Lev", "Nikolayevich", "Tolstoy", "");
 book_add_author(b, n);
 n = name_create("Count", "Pyotr", "Kirillovich", "Bezukhov", "");
 book_add_main_character(b, n);
 n = name_create("Countess", "Natalya", "Ilyinichna", "Rostova", "");
 book_add_main_character(b, n);
 books_add_book(&books, b);
9
  • Martin, say I have various delimited files containing books, authors, and main characters. Would the answer you provided be appropriate for reading in the information from the files, and creating the list. Does there need to be a "foreign key" of sorts, to associate the data in the files. For example: ` struct name { node linked_list; char book_id_fk[20]; //<--foreign key char name_prefix[10]; // ...other fields }; struct book { node linked_list; int book_id[20]; //<--primary key name *authors; name *main_characters; }; `
    – devnuts
    Mar 21, 2016 at 11:01
  • formatting of that comment didn't work out so well. sorry
    – devnuts
    Mar 21, 2016 at 11:08
  • also getting an uninitialized pointer error in none_add_node: previous->next = object;
    – devnuts
    Mar 21, 2016 at 11:23
  • I didn't initialise authors or main_characters to NULL. FIxed.
    – user325117
    Mar 21, 2016 at 11:54
  • You would need a synthetic foreign key if you have different books or authors with the same name, which is quite likely with a large database.
    – user325117
    Mar 21, 2016 at 11:58
1

What is the best way to create structures for this?

What's best depends on the details of the problem, and is to some extent a matter of opinion.

Would the below be accurate, or different set up?

The two alternatives you presented both look reasonable.

The first alternative, with the two name structs themselves being members of the book struct, might be a bit more convenient under some circumstances on account of requiring less dynamic allocation. On the other hand, it makes the first element of each inner linked list a special case, which might make your code more complicated overall.

The second alternative, with the book struct containing pointers to two name structs, gives you a bit more consistency, and it better accommodates the possibility that one or both internal lists is empty. All things considered, that's the one I would probably go with.

0

The data structure you need is most of the time decided on the basis of the kind of data you have and the operations which you want to perform on it.

Linked list is suitable for the following needs:

  1. Frequent insertion and deletion of data
  2. The amount of data might not fit in one place/or available in first go.

It comes with the following issues:

  1. Slow to search and retrieve data
  2. Add more complexity and in-directions into your code.

As far as I can see a dynamic array of strings(char array) will suffice for the authors or you can use a array of pointer to your struct name if granularity is something you are concerned of since a book may have multiple authors.

If you want static array following can do it for you:

#define MAX_AUTHOR_LIMIT 10
struct boook {
    ...
    struct name *authors[MAX_AUTHOR_LIMIT];
    int author_count;
    ...
}

Or if you want to be better, you can use a dynamic array as well.

Link list is also an alternative, but Its not a good idea to use link for small set of data items which doesn't need frequent insertion deletion, but again as per I pointed out you know what application needs to do decide on the basis of trade-off.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.