#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;//为了树中放任意类型元素方便
typedef struct node_t
{//第一步:
ElemType c;
struct node_t *pright;
struct node_t *pleft;
}Node_t, *pNode_t;
//queue[kju]
//辅助队列数据结构,每个元素放的树中元素节点地址值,能快速定树中节点
typedef struct queue_t{
pNode_t insertPos;
struct queue_t *pNext;
}Queue_t, *pQueue_t;
void preOrder(pNode_t);
void buildBinaryTree(pNode_t *treeRoot, pQueue_t *queHead, pQueue_t *queTail, ElemType val){
pNode_t treeNew = (pNode_t)calloc(1, sizeof(Node_t));
pQueue_t queNew = (pQueue_t)calloc(1,sizeof(Queue_t));
pQueue_t queCur = *queHead;
treeNew->c = val;
queNew->insertPos = treeNew;
if (NULL == *treeRoot){
*treeRoot = treeNew;
*queHead = queNew;
*queTail = queNew;
}
else{
(*queTail)->pNext = queNew;
*queTail = queNew;
if (NULL == queCur->insertPos->pleft){
queCur->insertPos->pleft = treeNew;
}
else if(NULL == queCur->insertPos->pright){//漏了if(NULL == queNew->insertPos->pright)
queCur->insertPos->pright = treeNew;
*queHead = queCur->pNext;
free(queCur);
queCur = NULL;
}
}
}
int main(){
ElemType val;
pNode_t treeRoot = NULL;
pQueue_t queHead = NULL, queTail = NULL;
//写成 (scanf"%d",&val) != EOF
//漏了eof
while (scanf("%c", &val) != EOF ){
if (val=='\n')
{
break;
}
buildBinaryTree(&treeRoot, &queHead, &queTail, val);
}
printf("前序打印:\n");
preOrder(treeRoot);
}
void preOrder(pNode_t p){
if (p != NULL)
{
putchar(p->c);
preOrder(p->pleft);
preOrder(p->pright);
}
}
C语言实现:qsort排序指针数组间接完成对链表的排序
最新推荐文章于 2022-11-26 12:41:09 发布