c - 无法查看将第一个值添加到二叉搜索树

c - Adding first value to binary search tree cant be view

我正在做这个涉及 bst、sll 和堆栈等算法的项目。我的 bst 函数有问题。 Bst 用于按顺序显示所有人。第一次尝试添加新人时,当我查看它时,它没有显示。当我检查文件时,它具有我输入的值。当文件不为空时,它工作正常。

这是我的 bst 函数:

            typedef struct person
            {
                    char fname[50],lname[50],mname[50],gend[50],bdate[11],doe[11],dor[11],cont[20];
                    int age,pnum,room;
                    struct person *next;
                    struct person *lchild; //for bst
                    struct person *rchild; //FOR BINARY SEARCH TREE
                }person;



person *insB(person *root, person *newnode) //INSERT TO BST 
{
    if(newnode->pnum < root->pnum)
    {
        if(root->lchild==NULL)
        {
            root->lchild=newnode;
        }
        else
        {
            root->lchild=insB(root->lchild,newnode);
        }
    }

    if(newnode->pnum > root->pnum)
    {
        if(root->rchild == NULL)
        {
            root->rchild = newnode;
        }
        else
        {

            root->rchild=insB(root->rchild,newnode);
        }
    }
    return root;
}
void insF(int prisonNumber, room *r, person *root, person *newnode) //insert
{
    FILE *fp,*fp1;
    person s;
    int maxRoom,i;
    fp=fopen("list.txt","a");
    fp1=fopen("orig.txt","a");
    if(fp==NULL || fp1==NULL)
    {
        printf("insF error");
    }
    else
    {
        logo();
        s.pnum=prisonNumber;

        settextstyle(TRIPLEX_FONT,HORIZ_DIR,1);

        outtextxy(20,89,"FIRST NAME:---------------------- ");
        outtextxy(20,105,"LAST NAME:----------------------- ");
        outtextxy(20,121,"MIDDLE NAME:--------------------- ");
        outtextxy(20,137,"GENDER (MALE/FEMALE):------------- ");
        outtextxy(20,153,"AGE:---------------------------- ");  
        outtextxy(20,169,"BIRTHDATE (MM/DD/YYYY):----------- ");
        outtextxy(20,185,"EMERGENCY CONTACT:--------------- ");
        outtextxy(20,219,"DATE OF ENTRY (MM/DD/YYYY):------- ");
        outtextxy(20,235,"DATE OF RELEASE (MM/DD/YYYY):------ ");
        outtextxy(20,250,"PRISONER NUMBER:----------------- ");
        outtextxy(20,266,"PRISONER ROOM NUMBER:------------ ");

        gotoxy(51,17);
        printf("%d",s.pnum);

        fflush(stdin);

        gotoxy(51,7);
        gets(s.fname);

        gotoxy(51,8);
        gets(s.lname);


        gotoxy(51,9);
        gets(s.mname);

        gotoxy(51,10);
        gets(s.gend);

        fflush(stdin);
        gotoxy(51,11);
        scanf("%d",&s.age);

        fflush(stdin);
        gotoxy(51,12);
        gets(s.bdate);

        gotoxy(51,13);
        gets(s.cont);

        gotoxy(51,15);
        gets(s.doe);

        gotoxy(51,16);
        gets(s.dor);


        fflush(stdin);
        gotoxy(51,18);
        scanf("%d",&s.room);

        strcpy(s.gend,strlwr(s.gend));

        maxRoom=gCnt(s.room);

        if(maxRoom>6)
        {
            outtextxy(20,300,"Room is full.");  
            outtextxy(20,320,"Check room availabilty and try again.");      

        }

        else
        {
            for(i=0;i<strlen(s.fname);i++)
            {
                if(s.fname[i]==' ')
                {
                    s.fname[i]='-';
                }
            }
            for(i=0;i<strlen(s.lname);i++)
            {
                if(s.lname[i]==' ')
                {
                    s.fname[i]='-';
                }
            }
            for(i=0;i<strlen(s.mname);i++)
            {
                if(s.mname[i]==' ')
                {
                    s.mname[i]='-';
                }
            }
            for(i=0;i<strlen(s.cont);i++)
            {
                if(s.cont[i]==' ')
                {
                    s.cont[i]='-';
                }
            }

            if(s.bdate[2]=='/' && s.bdate[5]=='/' && s.doe[2]=='/' && s.doe[5]=='/' && s.dor[2]=='/' && s.dor[5]=='/')
            {   
                                    if(strcmp(s.gend,"male")==0)
                                    {
                                        if(s.room>100)
                                        {
                                            outtextxy(20,300,"Room is exclusive for female prisoners.");    
                                            outtextxy(20,320,"Check room availabilty and try again.");          
                                        }
                                        else
                                        {

                                            fprintf(fp,"%d %d %s %s %s %s %d %s %s %s %s\n",s.pnum,s.room,s.fname,s.lname,s.mname,s.gend,s.age,s.bdate,s.cont,s.doe,s.dor);
                                            fprintf(fp1,"%d %d %s %s %s %s %d %s %s %s %s\n",s.pnum,s.room,s.fname,s.lname,s.mname,s.gend,s.age,s.bdate,s.cont,s.doe,s.dor);

                                            r=nPri(r,s);
                                            outtextxy(20,300,"Successfully added.");
                                            newnode=create_person(s);
                                            if(root==NULL)
                                            {
                                                root=newnode;

                                            }
                                            else
                                            {
                                                insB(root,newnode);

                                            }

                                        }
                                    }
                                    else if(strcmp(s.gend,"female")==0)
                                    {
                                        if(s.room<101)
                                        {
                                            outtextxy(20,300,"Room is exclusive for male prisoners.");  
                                            outtextxy(20,320,"Check room availabilty and try again.");          
                                        }
                                        else
                                        {

                                            fprintf(fp,"%d %d %s %s %s %s %d %s %s %s %s\n",s.pnum,s.room,s.fname,s.lname,s.mname,s.gend,s.age,s.bdate,s.cont,s.doe,s.dor);
                                            fprintf(fp1,"%d %d %s %s %s %s %d %s %s %s %s\n",s.pnum,s.room,s.fname,s.lname,s.mname,s.gend,s.age,s.bdate,s.cont,s.doe,s.dor);

                                            r=nPri(r,s);
                                            outtextxy(20,300,"Successfully added.");
                                            newnode=create_person(s);
                                            if(root==NULL)
                                            {
                                                root=newnode;

                                            }
                                            else
                                            {
                                                insB(root,newnode);

                                            }

                                        }
                                    }
                                    else
                                    {
                                            outtextxy(20,300,"Invalid gender.");    
                                            outtextxy(20,320,"Use male or female and try again.");  


                                    }
            }
            else
            {
                outtextxy(20,300,"Invalid birthdate/entry/release.");   
                outtextxy(20,320,"Pls follow the format MM/DD/YYYY and try again.");    

            }       
        }





    }
    fclose(fp);
    fclose(fp1);





}
person *dAll(person *root) //display all in file
{
    FILE *fp;
    person *newnode, s;


    cleardevice();

    fp=fopen("list.txt","r");

    if(fp==NULL)
    {
        printf("dALL error");

    }
    else
    {
        while(fscanf(fp,"%d %d %s %s %s %s %d %s %s %s %s",&s.pnum,&s.room,s.fname,s.lname,s.mname,s.gend,&s.age,s.bdate,s.cont,s.doe,s.dor)==11)
        {


            newnode=create_person(s);
            if(root==NULL)
            {
                root=newnode;

            }
            else
            {
                insB(root,newnode);

            }
        }

        /*gotoxy(1,1);

        cleardevice();
        gotoxy(1,1);
        printf("PN");
        gotoxy(5,1);
        printf("RN");
        gotoxy(9,1);
        printf("LAST,FIRST MIDDLE");
        gotoxy(40,1);
        printf("GNDR");
        gotoxy(48,1);
        printf("AGE");
        gotoxy(55,1);
        printf("BIRTHDATE");
        gotoxy(67,1);
        printf("CONTACT");
        */

    }
    fclose(fp);
    return root;
}
        void dInr(person *temp, int place) //BST FUNCTION INORDER RECURSIVELY
        {

            if(temp!=NULL)
            {   
                dInr(temp->lchild,place);

                gotoxy(1,place);
                printf("%d",temp->pnum);
                gotoxy(5,place);
                printf("%d",temp->room);
                gotoxy(9,place);
                printf("%s,%s %s",temp->lname,temp->fname,temp->mname);
                gotoxy(40,place);
                printf("%s",temp->gend);
                gotoxy(48,place);
                printf("%d",temp->age);
                gotoxy(55,place);
                printf("%s",temp->bdate);
                gotoxy(67,place);
                printf("%s",temp->cont);
                //gotoxy(65,place);
                //printf("%s",temp->doe);
                //gotoxy(66,place);
                //printf("%s",temp->dor);

                //printf("%d\t\t%d\t\t%s,%s %s\t\t%s\t\t%d\n",temp->pnum,temp->room,temp->fname,temp->lname,temp->mname,temp->gend,temp->age);

                place++;
                dInr(temp->rchild,place);

            }
        }



    void main()
    {

        /*room *r=NULL,*last=NULL,*temp;
        person *p=NULL;

        clrscr();
        r=room_insert(r,last);
        person_push(r);

        temp=r;

        */
        //indisp(r);

        room *r=NULL,*last=NULL,*temp;
        //person *p=NULL;
        person *root=NULL,*newnode;
        char ch,ch1;
        int gd = DETECT, gm,check,flag=0,prisonNumber=1,flag1=0,place=2;

        initgraph(&gd,&gm,"D:\TC\BGI");

        cleardevice();
        logo();

        /*  push(r);
                temp=r;

            cleardevice();

            printf("!");
            while(temp!=NULL)
            {   
            printf("!");
                for(p=temp->personList;p;p=p->next)
                {
                    //gotoxy(20,20);
                    printf("%s----%d",p->lname,temp->roomNumber);
                }


            temp=temp->nextRoom;

            }
            getch();
        */
        check=secu();

        if(check==1)
        {
            cleardevice();
            r=room_insert(r,last);
            push(r);
            root=dAll(root);
            while(flag==0)
            {
                flag1=0;
                cleardevice();
                logo();
                settextstyle(TRIPLEX_FONT,HORIZ_DIR,2);
                outtextxy(20,80,"[1]ADD PRISONER");
                outtextxy(20,100,"[2]INFORMATION");
                outtextxy(20,120,"[3]EDIT INFORMATION");
                outtextxy(20,140,"[4]DELETE");
                outtextxy(20,160,"[5]PRISON UPDATE");
                outtextxy(20,180,"[6]EXIT");
                outtextxy(20,200,"Input number of choice.");

                ch=getch();

                cleardevice();
                switch(ch)
                {
                    case'1':
                        prisonNumber=gNum();
                        insF(prisonNumber,r,root,newnode);


                        getch();

                        break;
                    case'2':
                        while(flag1==0)
                        {
                                            cleardevice();
                                            logo();
                                            outtextxy(20,80,"[1]VIEW ALL PRISONERS");
                                            outtextxy(20,100,"[2]VIEW PRISONERS BY ROOM NUMBER");
                                            outtextxy(20,120,"[3]VIEW PRISONERS BY PRISONER NAME");
                                            outtextxy(20,140,"[4]GO BACK TO MAIN MENU");
                                            outtextxy(20,200,"Input number of choice.");
                                            ch1=getch();


                                            switch(ch1)
                                            {
                                                case'1':
                                                    cleardevice();
                                                    gotoxy(1,1);
                                                    gotoxy(1,1);
                                                    printf("PN");
                                                    gotoxy(5,1);
                                                    printf("RN");
                                                    gotoxy(9,1);
                                                    printf("LAST,FIRST MIDDLE");
                                                    gotoxy(40,1);
                                                    printf("GNDR");
                                                    gotoxy(48,1);
                                                    printf("AGE");
                                                    gotoxy(55,1);
                                                    printf("BIRTHDATE");
                                                    gotoxy(67,1);
                                                    printf("CONTACT");
                                                    dInr(root,place);
                                                    getch();
                                                    break;
                                                case'2':
                                                    dRmn(r);
                                                    //r=dPri(r);
                                                    //outtextxy("")
                                                    getch();
                                                    break;
                                                case'3':
                                                    dNme(r);
                                                    getch();
                                                    break;
                                                case'4':
                                                    flag1=1;

                                                    break;
                                            }

                        }
                        break;
                    case'3':
                        cInf();
                        break;

                }



            }

            getch();
        }

        getch();
    }

您要么必须 return 根,要么在根可能需要更改的任何函数中使用双指针指向它。

这与这段代码完全相同:

void func(int param) {
    param++;
}
int main (void) { 
int x = 5;
func(x);
printf("%d", x);
//what gets printed?
}