#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
typedef struct student *stud;
struct student{
int score;
stud left;
stud right;
};
static stud* q;
static int N,head,tail;
void QUEUEinit(int maxN)
{
q=(stud *)malloc((maxN+1)*sizeof(LEN));
N=maxN+1;
head=N;
tail=0;
}
stud create() //先序遍历创建二插树
{
stud p;
int score;
printf("请输入成绩,0结束:");
scanf("%d",&score);
if(score==0)
{
p=NULL;
}
else{
p=(stud)malloc(LEN);
p->score=score;
printf("左结点:\n");
p->left=create();
printf("右结点:\n");
p->right=create();
}
return p;
}
int QUEUEempty()
{
return head%N==tail;
}
void QUEUEput(stud p)
{
q[tail++]=p;
tail=tail%N;
}
stud QUEUEget()
{
head=head%N;
return q[head++];
}
void traverse(stud p,void(*visit)(stud))
{
QUEUEinit(20);
QUEUEput(p);
while(!QUEUEempty()){
(*visit)(p=QUEUEget());
if(p->left!=NULL)
QUEUEput(p->left);
if(p->right!=NULL)
QUEUEput(p->right);
}
}
void print(stud p)
{
printf("%d\t",p->score);
}
int main()
{
stud p=create();
traverse(p,print);
return 0;
}