アットウィキロゴ

lab11

#include<stdio.h>
#include<stdlib.h>
enum yn{yes,no};
struct node{
	int element;
	struct node *left;
	struct node *right;
};

enum yn member(int x,struct node *p);
void insert(int x,struct node **p);
int min(struct node *p);


int main(){
	struct node *init;
	enum yn a;
	FILE *fp;
	fp=fopen("aaa.dat","r");
	int x,i,c,suu;
	fscanf(fp,"%d",&c);
	init =NULL;
	for(i=0;i<c;i++){
		fscanf(fp,"%d",&x);
		insert(x,&init);
		a=member(x,init);
		x=min(init);
	}
	scanf("%d",&suu);
	if(member(suu,init)==yes)printf("yes\n");
	else printf("no\n");
	printf("min : %d\n",min(init));
	fclose(fp);
}
enum yn member(int x,struct node *p)
{
	struct node *q;
	q=p;
	while(q!=NULL){
		if(q->element==x)return yes;
		if(q->element<x) q=q->right;
		else q=q->left;
	}
	return no;
}
void insert(int x,struct node **p)
{
	struct node *q,**r;
	r=p;
	q=*r;
	while(q!=NULL){
		if(q->element==x)return;
		if(q->element<x) {
			r=&(q->right);
			q=q->right;
		}
		else {
			r=&(q->left);
			q=q->left;
		}
	}
	*r=malloc(sizeof(struct node));
	(*r)->element=x;
	(*r)->left=NULL;
	(*r)->right=NULL;
	return;
} 

int min(struct node *p){
	struct node *q,*r;
	q=p;if(q==NULL){
		printf("a");
		exit(1);
	}
	while(q!=NULL){
		r=q;
		q=q->left;
	}
	return r->element;
} 

タグ:

+ タグ編集
  • タグ:
最終更新:2007年07月09日 13:04