0

I'm new to the linked list topic and I just created my first program using linked lists, the problem is it's not saving any data to the structure. It runs fine, no error, but when printing no data is displayed. Here is my code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
    int     nID;
    char    chTitle;
    struct node* next;
};

void addList(struct node *head);
void printList(struct node *head);
int checkID(struct node *head, int t);

int main(int argc, const char * argv[])
{
    int nInput;
    struct node *head = NULL;
    while (1)
    {
        printf("\n\t\t~~MENU~~\n");
        printf("1. Add a new book\n");
        printf("2. Print all data\n");
        printf("3. Exit\n");
        printf("Make your selection: ");
        scanf("%d", &nInput);

        switch (nInput)
        {
            case 1:
                addList(head);
                break;
            case 2:
                printList(head);
                break;
            case 3:
                printf("\nGoodby!!! Thanks for using the program\n");
                exit(1);
                break;
            default:
                printf("\n\t\t~~Invalid Input~~\n");
                break;
        }
    }
    return 0;
}

void addList(struct node *head)
{
    int bookId; // Used to store the BOOK ISBN so it can be checked if it already exist
    struct node *temp;

    temp = (struct node *)malloc(sizeof(struct node));


    printf("\n Enter Book Details\n");
    printf("Enter book ISBN: ");
    scanf("%d", &bookId);
    int bInd = checkID(head, bookId);
    if (bInd == 0)
    {
        printf("Enter title: ");
        scanf("%s", &temp->chTitle);
        temp->next = head;
        head = temp;
    }
    else
    {
        printf("\nSorry another book using that id!\n" );
    }
}

void printList(struct node* head)
{
    while (head != NULL)
    {
        printf("%s", &head->chTitle);
        head = head->next;
    }
}

int checkID(struct node *head, int t)
{
    head = NULL;
    while (head != NULL)
    {
        if (head->nID == t)
            return 1;
        head = head->next;
    }
    return 0;
} 
GeekyCoder
  • 67
  • 1
  • 11
  • 1
    StackOverflow isn't meant to answer open-ended questions about your code. When asking a question here, you should be prepared to explain what you tried to solve the problem. Have you tried using a debugger yet? – Tom Dec 12 '14 at 21:17
  • Recommendation: [Don't cast the result of `malloc`](http://stackoverflow.com/q/605845/10077) – Fred Larson Dec 12 '14 at 21:20
  • 4
    `addList()` changes `head` only locally. So your `head` in `main` is always null. – Fred Larson Dec 12 '14 at 21:21
  • 1
    Also, you cannot store an entire string in a single `char`. You'll need to `malloc()` a `char *` buffer for it. (This is often confused: To `scanf()` the `int i`, you'd pass its address `&i` to `scanf` but to read into the `char * s` buffer, you'd pass `s` itself.) – 5gon12eder Dec 12 '14 at 21:23
  • Oh, you can't store an ISBN in an `int`, either. `int` may not be big enough, and the check digit might be 'X'. – Fred Larson Dec 12 '14 at 21:36

2 Answers2

0

One problem is right here:

void addList(struct node *head)

addList() is getting a COPY of the head pointer, so when you modify it in this function you are only modifying that local copy. The caller's version is not modified. One way to solve this is by using a double pointer:

void addList(struct node **head)
{
    int bookId; // Used to store the BOOK ISBN so it can be checked if it already exist
    struct node *temp;

    temp = (struct node *)malloc(sizeof(struct node));


    printf("\n Enter Book Details\n");
    printf("Enter book ISBN: ");
    scanf("%d", &bookId);
    int bInd = checkID(*head, bookId);
    if (bInd == 0)
    {
        printf("Enter title: ");
        scanf("%s", &temp->chTitle);
        temp->next = *head;
        *head = temp;
    }
    else
    {
        printf("\nSorry another book using that id!\n" );
    }
}

Then your caller has to change too:

addList(&head);

Also, as @5gon12eder mentioned, a char holds only one character. You'll need a char array to hold your title:

struct node {
    int     nID;
    char    chTitle[100]; /* or how ever long your title can be */
    struct node* next;
};
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • Yep ! And maybe a `scanf("%99s"...)` just for the principle ;-) And this code still allows entry of dublicate book ids. Line `head = NULL;` should be removed in `checkID()` – Christophe Dec 12 '14 at 21:41
  • @Christophe: Yeah, there's plenty to fix here. Probably need to leave something for the OP to work on. 8v) – Fred Larson Dec 12 '14 at 21:42
0

You can change your addList header like this,

void addList(struct node *&head)

Here head becomes a reference to a pointer of type node. So when you modify head inside addList it will be reflected back in your original list.

Tamil Maran
  • 289
  • 1
  • 13