user-0 提问: 如何C源文件，引用其他文件的自定义类型？

回复:
user: user-0
content: 引用 1 楼 user-1 的回复:把需要的类型、函数声明都拿出来，放到.h头文件里面，然后在.c文件里面包含.h头文件  你好，能针对这个源代码详细点说一下吗？

user: user-1
content: 把需要的类型、函数声明都拿出来，放到.h头文件里面，然后在.c文件里面包含.h头文件

user: user-0
content: #include <stdio.h> //standard input output 标准输入输出头文件 #include <malloc.h> //memory allocation分配  /**  * 使用了BasicForTree的struct TreeNode  */ extern struct TreeNode; typedef struct TreeNode BinTree; typedef BinTree * ElementType;  typedef struct Node { 	ElementType Data; 	struct Node *next; } LinkStack;  /**  * 初始化一个头结点为空的Stack  * init a stack with a NULL head  */ LinkStack * InitStack() { 	LinkStack *s = (LinkStack *) malloc(sizeof(LinkStack)); 	s->next = NULL; 	return s; }  /**  * 若stack为空返回1,否则返回0  */ int IsStackEmpty(LinkStack *s) { 	return s->next == NULL; }  /**  * 入栈  */ void Push(LinkStack *s, ElementType e) { 	struct Node *p; 	p = (LinkStack *) malloc(sizeof(LinkStack)); 	p->Data = e; 	p->next = s->next; 	s->next = p; }  /**  * 出栈  */ ElementType Pop(LinkStack *s) { 	/*if(IsStackEmpty(s)){ 	 printf("Stack is NULL"); 	 return NULL; 	 }*/ 	if (s->next == NULL) { 		printf("Stack is NULL"); 		return NULL; 	} 	ElementType TopElem; 	struct Node *top; 	top = s->next; 	s->next = top->next; 	TopElem = top->Data; 	free(top); 	return TopElem; }  /**  * 返回栈顶元素  */ ElementType getTop(LinkStack *s) { 	if (s->next == NULL) { 		printf("Stack is NULL"); 		return NULL; 	} 	return s->next->Data; }       /*  * Queue  *  *  Created on: 2015年5月31日  *      Author: xing  */ #include <stdio.h> #include <malloc.h>  #define MaxSize 50 /**  * 使用了BasicForTree的struct TreeNode  */ extern struct TreeNode; typedef struct TreeNode BinTree; typedef BinTree * ElementType;  typedef struct { 	ElementType Data[MaxSize]; 	int front; 	int rear; } Queue;  Queue * InitQueue() { 	Queue *q = (Queue *) malloc(sizeof(Queue)); 	return q; }  void Destory(Queue * q) { 	free(q); }  int IsQueueEmpty(Queue *q) { 	return q->front = q->rear; }  void EnQueue(Queue *q, ElementType e) { 	if ((q->rear + 1) % MaxSize == q->front) { 		printf("队列满了"); 		return; 	} 	q->rear = (q->rear + 1) % MaxSize; 	q->Data[q->rear] = e; }  ElementType DeQueue(Queue* q) { 	if (q->front == q->rear) { 		printf("队列空了！"); 		return NULL; 	} 	q->front = (q->front + 1) % MaxSize; 	return q->Data[q->front]; }     #include <stdio.h> #include <malloc.h>  extern LinkStack; extern Queue; extern Queue * InitQueue(); extern LinkStack * InitStack();  typedef int ElementType; typedef struct TreeNode BinTree; typedef struct TreeNode { 	ElementType Data; 	BinTree *Left; 	BinTree *Right; };  /**  * 层序生成二叉树  */ BinTree *CreateBinTree() { 	ElementType Data; 	BinTree *BT, *T; 	Queue *Q = InitQueue(); 	scanf("%d", &Data); 	if (Data) { 		BT = (BinTree *) malloc(sizeof(BinTree)); 		BT->Data = Data; 		EnQueue(Q, BT); 	} 	while (!IsQueueEmpty(Q)) { 		T = DeQueue(Q); 		scanf("%d", &Data); 		if (Data) { 			T->Left = (BinTree *) malloc(sizeof(BinTree)); 			T->Left->Data = Data; 			EnQueue(Q, T->Left); 		} else { 			T->Left = NULL; 		} 		scanf("%d", &Data); 		if (Data) { 			T->Right = (BinTree *) malloc(sizeof(BinTree)); 			T->Right->Data = Data; 			EnQueue(Q, T->Right); 		} else { 			T->Right = NULL; 		} 	} 	return BT; }  /**  * 遍历二叉树 递归算法  */ void PreOrderTraversal(BinTree *BT) { 	if (BT) { //BT != NULL 		printf("%d", BT->Data); 		PreOrderTraversal(BT->Left); 		PreOrderTraversal(BT->Right); 	} }  void InOrederTraversal(BinTree *BT) { 	if (BT) { 		InOrederTraversal(BT->Left); 		printf("%d", BT->Data); 		InOrederTraversal(BT->Right); 	} }  void PostOrderTraversal(BinTree *BT) { 	if (BT) { 		PostOrderTraversal(BT->Left); 		PostOrderTraversal(BT->Right); 		printf("%d", BT->Data); 	} }  /**  * 遍历二叉树 非递归算法  */ void PreOrderTraversal_Iter(BinTree *BT) { 	BinTree *T; 	LinkStack *S = InitStack(); 	T = BT;  	while (T || !IsStackEmpty(S)) { 		while (T) { 			printf("%d", T->Data); 			Push(S, T); 			T = T->Left; 		} 		if (!IsStackEmpty(S)) { 			T = Pop(S); 			T = T->Right; 		} 	} }  void InOrderTraversal_Iter(BinTree *BT) { 	BinTree *T; 	LinkStack *S = InitStack(); 	T = BT;  	while (T || !IsStackEmpty()) { 		while (T) { 			Push(S, T); 			T = T->Left; 		} 		if (!IsStackEmpty()) { 			T = Pop(S); 			printf("%d ", T->Data); 			T = T->Right; 		} 	} }  void PostOrderTraversal_Iter1(BinTree *BT) { 	BinTree *T, *p, *b; 	LinkStack *S = InitStack(); 	T = BT; 	int flag = 0; 	do { 		while (T) { 			Push(S, T); 			T = T->Left; 		} 		flag = 1; 		p = NULL; 		while (!IsStackEmpty() && flag) { 			b = getTop(S); 			if (p == b->Right) { 				printf("%d ", b->Data); 				p = Pop(S); 			} else { 				T = T->Right; 				flag = 0; 			} 		} 	} while (!IsStackEmpty(S)); }  void PostOrderTraversal_Iter2(BinTree *BT) { 	BinTree *p, *cur; 	LinkStack *S = InitStack(); 	p = NULL; 	cur = NULL; 	Push(S, BT); 	while (!IsStackEmpty()) { 		cur = getTop(S); 		if ((!cur->Left && !cur->Right)   //NULL==cur->Left && NULL==cur->Right 		|| (p && (p == cur->Left || p == cur->Right))) { 			printf("%d ", cur->Data); 			p = Pop(S); 		} else { 			if (cur->Right) { 				Push(S, cur->Left); 			} 			if (cur->Left) { 				Push(S, cur->Right); 			} 		} 	} }  void LevelOrderTraversal(BinTree *BT) { 	BinTree *T; 	T = BT; 	if (!T) { 		return; 	} 	Queue *Q = InitQueue(); 	EnQueue(Q, T); 	while (!IsQueueEmpty()) { 		T = DeQueue(Q); 		printf("%d ", T->Data); 		if (T->Left) 			EnQueue(T->Left); 		if (T->Right) 			EnQueue(T->Right); 	} }  void getOrderPrintLeaves(BinTree *BT) { 	while (BT) { 		if (!BT->Left && !BT->Right) { 			printf("%d", BT->Data); 		} 		getOrderPrintLeaves(BT->Left); 		getOrderPrintLeaves(BT->Right); 	} }  void getOrderPrintLeaves_Iter(BinTree *BT) { 	BinTree *T; 	LinkStack *S = InitStack(); 	T = BT;  	while (T || !IsStackEmpty(S)) { 		while (T) { 			Push(S, T); 			T = T->Left; 		} 		if (!IsStackEmpty(S)) { 			T = Pop(S); 			if (!BT->Left && !BT->Right) { 				printf("%d", BT->Data); 				T = Pop(S); 			} 			T = T->Right; 		} 	} }  int PostOrderGetHeight(BinTree *BT) { 	int LH, RH, MaxH; 	if (BT) { 		LH = PostOrderGetHeight(BT->Left); 		RH = PostOrderGetHeight(BT->Right); 		MaxH = (LH > RH ? LH : RH); 		return MaxH + 1; 	} 	return 0; }  int PostOrderGetHeight_Iter(BinTree *BT) { 	return 0; }