Thread: A List of Linked Lists

  1. #1
    kristjon_ciko
    Guest

    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

    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.
    Attached Images Attached Images A List of Linked Lists-list-jpg 
    Last edited by kristjon_ciko; 08-15-2012 at 07:52 AM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Linked lists--do these programs both clear the list?
    By dsured in forum C Programming
    Replies: 2
    Last Post: 04-15-2011, 06:38 PM
  3. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  4. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM
  5. Linked list of linked lists???
    By Qui in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 01:13 PM

Tags for this Thread