二叉排序树上实现学生信息管理
      树中结点结构为 成绩 lchild rchild head
      链表中的结点形式为 学号 姓名 next
      功能:1 按成绩查询
            2 插入一个新学生信息
            3 删除一个学生信息
            4 按成绩排序(高到低)
#include<stdio.h>
#include<malloc.h>
#define NULL 0struct BiTree
{float score;
 struct BiTree *lchild,*rchild,*head;
};struct BSTList
{int num;
 char name[100];
 struct BSTList *next;
};struct BiTree*SearchBST(BiTree T,float key,BiTree f,BiTree p)
{
 if(!T){p=f;return(T);}
 else if(key=T->score){p=T;return(NULL);}
 else if(key<T->score)return Search(T->lchild,key,T,p);
 else return Search(T->rchild,key,T,p);
}void InsertBST(BiTree T,float e)
{struct BiTree *s,*p;
 p=T;
 if(SearchBST(T,e,NULL,p)=NULL)
{s=(BiTree*)malloc(sizeof(BiTree));
 s->score=e;s->lchild=s->rchild=NULL;
 if(!p)T=s;
 else if(e<p->score){p->lchild=s;CreatList(p->head);}
 else {p->rchild=s;CreatList(p->head);}
}
 else CreatList(p->head->next);
}void BiTreePrint()
{struct BiTree *s;
 /*s=*SearchBST(BiTree T,float key,BiTree f,BiTree p);*/
 while(s!=null)
 printf("%d,%s,%f",s->head->num,s->head->name,s->score);
 s=s->head->next;
}void CreatList(BiTree T)
{struct BSTList *p;
 p=(BSTList*)malloc(sizeof(BSTList));
 T=p;
 printf("Enter the Number of the Student\n");
 scanf("%d",p->num);
 printf("Enter the Name of the Student\n");
 /*scanf();*/ 
 p->next=NULL;
}void deleteNode(BiTree ptree, float key)
{BiTree parentp, p, r; p=*ptree;  parentp=NULL;
 while(p!=NULL)
{if(p->key==key)  break;  
 parentp=p;
 if(p->key>key)  p=p->lchild;   
 else  p=p->rchild;             
}
 if(p==NULL)  return;    
 if(p->llink==NULL)        
{if(parentp==NULL)  
  *ptree=p->rchild;      
 else if(parentp->lchild==p)           
  parentp->lchild=p->rchild;     
 else  parentp->rchild=p->rchild;    
}
 else
{r=p->lchild;
 while(r->rchild!=NULL)  r=r->rchild;     
 r->rchild=p->rchild;       
 if(parentp==NULL)    
 else if(parentp->lchild==p)    
 parentp->lchild=p->lchild;
 else  parentp->rchild=p->lchild;
}
void manu()
{printf("********************************\n");
 printf("* 1.插入新学生信息 *\n");
 printf("* 2.按成绩查询 *\n");
 printf("* 3.删除学生信息 *\n");
 printf("* 4.按成绩排序(从高到低) *\n");
 printf("* 5.退出 *\n");
 prinft("********************************\n");
}
main()
{manu();
 int i;
 float s;
 struct BiTree *T;
 scanf("%d",i);
 if(i==1)
{printf("Enter the score of the student:\n");
 scanf("%f",s);
 InsertBST(T,s);
 }
 else if(i==2)
{printf("Enter the score of the student:\n");
   scanf("%f",s);
 T=SearchBST(BiTree T,float key,BiTree f,BiTree p)
}
}

解决方案 »

  1.   


    程序我已经修改过了,可以运行,但不能完成删除功能,大家帮忙看一下.
    #include<stdio.h>
    #include<malloc.h>
    #define NULL 0typedef struct List
    {int num[10];
     char name[10];
     struct List *next;
    }List,*BSTList;typedef struct BiNode
    {int score;
     struct BiNode *lchild,*rchild;
     struct List *head;
    }BiNode,*BiTree;BiTree SearchBST(BiTree T,int key)
    {
    /* BiTree p;*/
     if(T==NULL){return(NULL);}
     else if(key==T->score){return(T);}
    else
    while(T!=NULL)
    {if(key<T->score)
     T=T->lchild;
     else
     T=T->rchild;
    }
     return T;
    }void CreatList(BiTree T)
    {char a[1];
     BSTList p,q;
     q=T->head;
     p=(BSTList)malloc(sizeof(List));
     p->next=q;
     T->head=p;
     printf("Enter the Number of the Student\n");
     scanf("%s",&p->num);gets(a);
     printf("Enter the Name of the Student\n");
     gets(p->name);
    }BiTree InsertBST(BiTree T,int e)
    {BiTree s,p,q;
     BSTList u;
     q=SearchBST(T,e);
     if(q==NULL)
    {s=(BiTree)malloc(sizeof(BiTree));
     s->score=e;s->lchild=s->rchild=s->head=NULL;
     if(T==NULL){T=s;CreatList(T);}
     else
    {p=T;
     while(p!=NULL)
    {if(e<p->score){p=p->lchild;}
     else {p=p->rchild;}
    }
     p=s;
     CreatList(p);
    }
    }
     else
    {
     CreatList(q);
    }
    return T;
    }void BiTreePrint(BiTree T)
    {
     BSTList s;
     s=T->head;
     printf("Number\tName\tScore\n");
     while(s!=NULL)
     {
      printf("%s\t%s\t%d\n",s->num,s->name,T->score);
      s=s->next;
     }
    }void deleteNode(BiTree ptree, int key)
    {BiTree parentp, p, r; p=ptree;  parentp=NULL;
     while(p!=NULL)
    {if(p->score==key)  break;
     parentp=p;
     if(p->score>key)  p=p->lchild;
     else  p=p->rchild;
    }
     if(p==NULL)  return;
     if(p->lchild==NULL)
    {if(parentp==NULL)
    ptree=p->rchild;
     else if(parentp->lchild==p)
    parentp->lchild=p->rchild;
     else  parentp->rchild=p->rchild;
    }
     else
    {r=p->lchild;
     while(r->rchild!=NULL)  r=r->rchild;
     r->rchild=p->rchild;
     if(parentp==NULL);
     else if(parentp->lchild==p)
     parentp->lchild=p->lchild;
     else  parentp->rchild=p->lchild;
     }
    }BiTree print(BiTree T)
    {
       int i;
       BiTree p=T;
       BSTList q;
       q=p->head;
         printf("Score:%d",p->score);
       while(q)
       {
         printf("\tNumber:");
         for(i=0;i<strlen(q->num);i++)
         printf("%d",q->num);
         printf("\tname:");
         for(i=0;i<strlen(q->name);i++)
         printf("%c\n",q->name[i]);
         q=q->next;
       }
      return (T);
    }
    int visit(BiTree T)
    {if(T!=NULL)
        {if(visit(T->rchild))
    if(print(T))
      if(visit(T->lchild)) return 1;
           return 0;
        }
       return 1;
    }void manu()
    {printf("\n*****************************************************************\n");
     printf("* 1.ENTER THE NEW STUDENTS MESSAGES *\n");
     printf("* 2.SEARCH BY THE SCORES *\n");
     printf("* 3.DELETE THE STUDENTS MESSAGES *\n");
     printf("* 4.SORT THE STUDENTS MESSAGES(FROM THE HIGH TO LOW) *\n");
     printf("* 5.EXIT *\n");
     printf("*****************************************************************\n");
    }
    main()
    {int q;
     int i;
     int s;
     BiTree p,T,r;
     T=T->lchild=T->rchild=T->head=NULL;
     r=T;
    label: manu();
     printf("Please Enter the Menu Number:\n");
     scanf("%d",&i);
     if(i==1)
    {printf("Enter the score of the student:\n");
     scanf("%d",&s);
     r=InsertBST(r,s);
     goto label;
    }
     else if(i==2)
    {printf("Enter the score of the student:\n");
     scanf("%d",&s);
     p=SearchBST(r,s);
     BiTreePrint(p);
     goto label;
    }
     else if(i==3)
    {printf("Enter the delete score of the student:\n");
     scanf("%d",&s);
     deleteNode(r,s);
     goto label;
    }
     else if(i==4)
    {q=visit(r);
    }
     else if(i==5)
    {printf("Press Enter to exit!\n");
     getchar();
     getchar();
     exit(0);
    }
     else goto label;
    }题目是二叉排序树上实现学生信息管理
          树中结点结构为 成绩 lchild rchild head
          链表中的结点形式为 学号 姓名 next
          功能:1 按成绩查询
                2 插入一个新学生信息
                3 删除一个学生信息
                4 按成绩排序(高到低)
      

  2.   

    回复人: Stefine(平常心对待考试太重要了,可我总做不到!) ( ) 信誉:100  2005-07-01 23:52:00  得分: 0  
     
     
       看到过多次了用链表做的搞懂数据结构一般就没有什么大问题了建议自己去搜搜或者把问题症状讲清楚点网友们不可能有这么多时间一个个的给你去调试
      
     
    ========================================
    不要睁着眼睛说瞎话,OK?这个明明就是二叉树……二叉树还是有点复杂的,特别是排序的,如果要求最优的话就更麻烦……