c - Cannot print out data in a binary tree -
actually, here asking debug. so, if don't have interest in helping me debugging, can stop here go back. however, believe code contain small bug because have tested part of it.
i have little test myself. code work when commented out function pinrtonenode(), mean input data binary tree. bug in printonrnode() function.
and also, compile time error is
c:\users\...\local\temp\ccwcuby.oprompt c:\users\...\.../bst.c:45printonenode collect2.exe:error:ld returned 1 exit status
by way, bst.c
source code in
so here code:
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct bstnode{ struct bstnode* leftchild; struct bstnode* rightchild; char data[20]; }bstnode; void prompt(); //this function prompt user selection everytime //after input data( inputdata() ) or print data( printonenode() ) void inputdata(); void printonenode(); bstnode firstnode; bstnode* mynode=&firstnode; //i don't know if it's initialize way int main() { //mynode=malloc(sizeof(bstnode)); mynode->leftchild=null; mynode->rightchild=null; strcpy(mynode->data,""); while(1) prompt(); // use while loop prompt user selection continuously return 0; } /* function used prompt user selection input*/ void prompt(){ int i=0; printf("please select:\n"); printf("1.input data\n2.print 1 data\n3.exit\n"); scanf("%d",&i); switch(i){ case 1: inputdata(); break; case 2: printonenode(); break; case 3: exit(0); default: printf("please input valid number!(1-3)"); } } /* used input data binary tree*/ void inputdata(){ char data[20]={0}; printf("input data here(character / less 19 characters!): "); scanf("%s",data); bstnode** ptr=&mynode; while(1){ if(strcmp(data,(*ptr)->data)){ if((*ptr)->rightchild!=null){ ptr=&((*ptr)->rightchild); continue; } else{ (*ptr)->rightchild=malloc(sizeof(bstnode)); ptr=&((*ptr)->rightchild); (*ptr)->rightchild=null; (*ptr)->leftchild=null; strcpy((*ptr)->data,data); break; } } else{ if((*ptr)->leftchild!=null){ ptr=&((*ptr)->leftchild); continue; } else{ (*ptr)->leftchild=malloc(sizeof(bstnode)); ptr=&((*ptr)->leftchild); (*ptr)->leftchild=null; (*ptr)->rightchild=null; strcpy((*ptr)->data,data); break; } } } printf(" data have been input successfully!\n"); return; } /* used print data, 1 @ time*/ void printonenode(){ bstnode** ptr=&mynode; printf("the first node is%s\n",(*ptr)->data); while(1){ printf("select side of node want print now(l/r)?(q quit) "); //i ask user whether wanna print out data in node in right side // of current node or left side char a; scanf("%c",&a); switch(a){ case 'l': if((*ptr)->leftchild!=null){ ptr=&((*ptr)->leftchild); printf("\t%s\n",(*ptr)->data); } else printf("there no more leftchild"); break; case 'r': if((*ptr)->rightchild!=null){ ptr=&((*ptr)->rightchild); printf("\t%s\n",(*ptr)->data); } else printf("there no more rightchild!"); break; case 'q': return; default: return; } } }
please help. , improvement snippet of code beside bug appreciated vert much. thanks.
Comments
Post a Comment