Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
哪位大大看看,RE?#include<stdio.h> #include<string.h> typedef struct tree tree; struct tree { char name[10]; tree *brother,*child; }; tree *head=NULL; char input[100]; void insert(tree* &sym,int pos) { int i,j; char name[10]; tree *nod,*tmp,*pre1,*pre2; for(i=pos,j=0;i<(int)strlen(input);i++,j++) { if(input[i]=='\\') { i++; break; } name[j]=input[i]; } name[j]='\0'; if(sym==NULL) { nod=new(tree); strcpy(nod->name,name); nod->brother=NULL; nod->child=NULL; sym=nod; } else if(strcmp(name,sym->name)<0) { nod=new(tree); strcpy(nod->name,name); nod->child=NULL; nod->brother=sym; sym=nod; } else if(strcmp(name,sym->name)==0) nod=sym; else { pre2=NULL; pre1=sym;tmp=sym->brother; while(tmp!=NULL&&strcmp(name,tmp->name)>0) { pre2=pre1; pre1=tmp; tmp=tmp->brother; } if(tmp!=NULL&&strcmp(name,tmp->name)==0) nod=tmp; else if(tmp==NULL) { nod=new(tree); strcpy(nod->name,name); nod->child=NULL; nod->brother=pre1; pre2->brother=nod; } else { nod=new(tree); strcpy(nod->name,name); nod->child=NULL; nod->brother=tmp; pre1->brother=nod; } } if(i<(int)strlen(input)) insert(nod->child,i); } void ouput(tree *sym,int num) { int i; tree *tmp,*temp; tmp=sym; while(tmp!=NULL) { for(i=0;i<num;i++) printf(" "); printf("%s\n",tmp->name); if(tmp->child!=NULL) ouput(tmp->child,num+1); temp=tmp; tmp=tmp->brother; delete temp; } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%s",input); insert(head,0); } ouput(head,0); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator