Linked list in C

Posted by ScReYm0 on Stack Overflow See other posts from Stack Overflow or by ScReYm0
Published on 2010-05-17T01:10:07Z Indexed on 2010/05/17 1:20 UTC
Read the original article Hit count: 356

Filed under:
|
|
|

I am still new at lists in C and i got a big problem... First i wanna show you my code for inserting item to the list:

void input_books_info(int number_of_books, BOOK *current)
{
    int i;
    for(i = 0; i < number_of_books; i++)
    {
        while(current->next != NULL)
            current = current->next;


        current->next = (BOOK *)malloc(sizeof(BOOK));

        printf_s("%d book catalog number: ", i + 1);
        scanf_s("%s", &current->next->catalog_number , 20);
        printf_s("%d book title: ", i + 1);
        scanf_s("%s", current->next->title ,80);
        printf_s("%d book author: ", i + 1);
        scanf_s("%s", current->next->author ,40);
        printf_s("%d book publisher: ", i+1);
        scanf_s("%s", current->next->publisher,80);
        printf_s("%d book price: ", i + 1);
        scanf_s("%f", &current->next->price, 5);
        printf_s("%d book year published: ", i + 1);
        scanf_s("%d", &current->next->year_published, 5);

        current->next->next = NULL;
        printf_s("\n\n");
    }



}

And this is my main function:

void main (void)
{
    int number_of_books, t = 1;
    char book_catalog_number[STRMAX];
    char book_title[STRMAX];
    char book_author[STRMAX];
    char reading_file[STRMAX];
    char saving_file[STRMAX];


    first = malloc(sizeof(BOOK));
    first->next = NULL;
    /*
    printf_s("Enter file name: "); gets(saving_file);
    first->next = book_open(first, saving_file);
    */
    while(t)
    {
        char m;
        printf_s("1. Input \n0. Exit \n\n");
        printf_s("Choose operation: ");
        m = getch();





        switch(m)
        {
            case '1':
                printf_s("\ninput number of books: ");
                scanf_s("%d", &number_of_books);
                input_books_info(number_of_books, first);
                printf_s("\n");
            break;


            default:
                printf_s("\nNo entry found!\n\n\n\n\n");
                break;
        }

    }

}

and last maybe here is the problem the printing function:

void print_books_info(BOOK *current)
{
    while(current->next != NULL && current != NULL)
    {

        printf_s("%s, ", current->next->catalog_number);
        printf_s("%s, ", current->author);
        printf_s("%s, ", current->next->title);
        printf_s("%s, ", current->next->author);
        printf_s("%s, ", current->next->publisher);
        printf_s("%.2f, ", current->next->price);
        printf_s("%d", current->next->year_published);
        printf_s("\n\n");
        current = current->next;
    }

}

And my problem is that, when i run the app, program is moving good. But when I start the app, the program is storing only first input of data second and third are lost ... Can you help me to figure out it... ???

© Stack Overflow or respective owner

Related posts about linked

Related posts about list