Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define M 100
#define FLUSH while(getchar() != '\n')

typedef struct node *nd;

typedef struct 
{
       char bkNumber[5];
       char bkTitle[M];
       char bkAuthor[M];
       int bkCopyright;
       
}BOOK;

typedef struct
{
        BOOK rec;
        nd right;
        nd left;
}NODE;

BOOK getData(void);
void addNode(BOOK, nd *);

int main(void)
{
    nd root;
    BOOK data;
    int choice;
    
    printf("1] Add new book record \n2] Edit book title \n3] View all the books");
    printf("\n4] View a specific book \n5] Delete a book \n\n");
    printf("Choice: ");
    scanf("%d", &choice);
    
    system("cls");
    
    switch(choice)
    {
        case 1:
             data = getData();
             addNode(data, &root);
             break;
    }
    
    return 0;
}

BOOK getData(void)
{
     BOOK d;
     FLUSH;

     printf("Book Number: ");
     scanf("%4s", d.bkNumber);
     
     printf("Book Title: ");
     scanf("%s", d.bkTitle);
          
     printf("Author: ");
     scanf("%s", d.bkAuthor);

     printf("Copyright Year: ");
     scanf("%d", d.bkCopyright);
     
     return d;
}

void addNode(BOOK data, nd *root)
{
     nd temp, tp, tp1;
     
        temp = malloc(sizeof(NODE));
        
        strcpy(temp->rec.bkNumber, data.bkNumber);
        temp->right = NULL;
        temp->left = NULL;
        
        if(root == NULL)
                *root = temp;
        else
        {
            tp1 = *root;
            
            while(tp1 != NULL)
            {
                      tp = tp1;
                      if(temp->rec <= tp1->rec)
                          tp1 = tp1->rec;
                      else
                          tp1 = tp1->right;
            }//end of while
            
            if(temp->rec <= tp->rec)
                      tp->left = temp;
            else
                      tp->right = temp;
        }//end of else
        
        tp = NULL;
        tp1 = NULL;
        
        return;
}//end of addNode
Posted
Updated 13-Mar-15 23:06pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Mar-15 5:28am    
What line?
—SA

1 solution

All your code refers to a variable type nd defined as:
C++
typedef struct node *nd;

In this case nd defines a pointer to an 'incomplete' node structure.
The structure 'node' is defined nowhere, so when you want access fields of the structure the compiler doesn't know how to get them (the structure is incomplete no fields are defined anywhere :)).
Perhaps you would refer to a 'NODE' structure defined as:
C++
typedef struct NODE *nd;

In this case you made a typo error ;)
To avoid the problem it's better to always define structures and all their reference types on the same line:
C++
typedef struct
{
        BOOK rec;
        nd right;
        nd left;
}NODE, *nd;         //define *nd here

This will at least avoid typo errors because you don't have to rewrite references.

P.S. I assumed that you are aware that C is case sensitive language, meaning that 'node' and 'NODE' are not the same.
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900