user-0 提问: 我用while来建立一个树，不成功，帮我看看。

回复:
user: user-0
content: 输入得到的结果为

user: user-0
content: 我修必了一下程序，成功显示了，不知这个树图是否正确，  程序如下：  #include "stdafx.h" #include "stdio.h" #include "stdlib.h"  typedef int Item;  typedef struct node  { 	Item item; 	struct node *left; 	struct node *right; 	 }Node;  typedef struct tree { 	Node *root; 	int size; }Tree;  void InitializeTree(Tree *ptree); void CreateTree(int *ar, int n, Item item, Tree *ptree); void CountTree(Tree *ptree); void PreShowTree(Tree *ptree); void MidShowTree(int m, int n, Tree *ptree);  int main(void) { 	 Tree data; 	Item number; 	int array[3]; 	array[0] = 0; 	array[1] = 0; 	array[2] = 0;  	InitializeTree(&data); 	puts("Please enter the first item:(enter 'q' to quit)"); 	while(scanf("%d", &number) == 1) 	{ 		while(getchar() != '\n') 		{ 			continue; 		} 		CreateTree(array, 3, number, &data); 		puts("Please enter another item:(enter 'q' to quit)"); 	} 	Tree copy; 	copy = data; 	printf("There are %d items on the tree.\n", array[0]); 	printf("There are %d items on the left tree.\n", array[1]); 	printf("There are %d items on the right tree.\n", array[2]); 	puts("先序显示："); 	PreShowTree(&data); 	puts("\n中序显示："); 	MidShowTree(array[1], array[2], ©); 	printf("\n");  	return 0; }  void InitializeTree(Tree *ptree) { 	ptree->root = NULL; 	ptree->size = 0; }  void CreateTree(int *ar, int n, Item item, Tree *ptree) { static	Node *current; 	current = ptree->root; 	Node *pnew; 	pnew = (Node *)malloc(sizeof(Node)); 	if(pnew == NULL) 	{ 		fprintf(stderr, "Failing to allocate memory.\n"); 	} 	else 	{ 		pnew->item = item; 		pnew->left = NULL; 		pnew->right = NULL; 		if(current == NULL) 		{ 			ptree->root = pnew; 		//	current = pnew; 		} 		else 		{			 			if(item > 10) 			{ 				while(1) 				{ 					 					if(current->left != NULL) 					{ 						current = current->left; 					} 					else 					{ 						break; 					} 				}				 				current->left = pnew; 				ar[1] = ar[1] + 1; 			} 			else 			{ 				while(1) 				{ 					if(current->right != NULL) 					{ 						current = current->right; 					} 					else 					{ 						break; 					}	 				}			 				current->right = pnew;	 				ar[2] = ar[2]+1; 			}			 		} 		ptree->size++; 		ar[0] = ptree->size; 	} }   void CountTree(Tree *ptree) { 	 ptree->size; } void PreShowTree(Tree *ptree) { 	Node *current; 	current = ptree->root; 	if(ptree->root != NULL) 	{ 		printf("%d ", ptree->root->item); 		while(ptree->root->left != NULL) 		{ 			printf("%d ", ptree->root->left->item);	 			ptree->root = ptree->root->left;		 			//printf("%d ", ptree->root->item); 		}  		while(current->right != NULL) 		{ 			printf("%d ", current->right->item);	 			current = current->right;		 			//printf("%d ", ptree->root->item); 		} 	 	}	 }  void MidShowTree(int m, int n, Tree *ptree) { 	int i = 0; 	  Node *current; 	current = ptree->root; //	printf("%d ", current->item); 	 	if(ptree->root != NULL) 	{							 		while(1) 		{ 			current = ptree->root; 			while (i<m && current->left != NULL) 			{ 			//	printf("%d ", ptree->root->left->item); 				current = current->left; 				i++;				 			} 			printf("%d ", current->item); 			i = 0; 			m = m-1; 			if(m == 0) 			{ 				break; 			} 		} 			printf("%d ",ptree->root->item);  		while(1) 		{ 			current = ptree->root; 			while (i<n && current->right != NULL) 			{ 			//	printf("%d ", ptree->root->left->item); 				current = current->right; 				i++;				 			} 			printf("%d ", current->item); 			i = 0; 			n = n-1; 			if(n == 0) 			{ 				break; 			} 		} 		   	} }

user: user-0
content: 谢谢楼上，我根据您说的再改改程序。

user: user-1
content: 不要使用 while (条件) 更不要使用 while (组合条件) 要使用 while (1) {  if (条件1) break;  //...  if (条件2) continue;  //...  if (条件3) return;  //... } 因为前两种写法在语言表达意思的层面上有二义性，只有第三种才忠实反映了程序流的实际情况。 典型如： 下面两段的语义都是当文件未结束时读字符 whlie (!feof(f)) {  a=fgetc(f);  //...  b=fgetc(f);//可能此时已经feof了！  //... } 而这样写就没有问题： whlie (1) {  a=fgetc(f);  if (feof(f)) break;  //...  b=fgetc(f);  if (feof(f)) break;  //... } 类似的例子还可以举很多。

user: user-2
content: step by step debug

user: user-0
content: 我随便输入几个数，得到如下结果： Please enter the first item:(enter 'q' to qu 12 Please enter another item:(enter 'q' to quit 45 Please enter another item:(enter 'q' to quit 8 Please enter another item:(enter 'q' to quit 9 Please enter another item:(enter 'q' to quit 6 Please enter another item:(enter 'q' to quit 7 Please enter another item:(enter 'q' to quit q There are 6 items on the tree. 先序显示： 6 7 中序显示： 7 Press any key to continue