网上科普有关“数据结构status函数声明(c语言status的用法) ”话题很是火热,小编也是针对数据结构status函数声明(c语言status的用法)寻找了一些与之相关的一些信息进行分析 ,如果能碰巧解决你现在面临的问题,希望能够帮助到您 。
给你完全调好了,一切正常运行:
#include "stdio.h"
#include "stdlib.h"
typedef int status; //C中没有status类型,所以想使用这个类型你必须定义它
#define OK 0
#define ERROR -1
#define OVERFLOW -2 //OK 、OVERLFLOW、ERROR这些宏的定义头文件中是没有的 ,所以你必须自己定义它们
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct QNode{
BiTNode *data;
struct QNode *next; //马虎。少个字符Q
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
void CreateBiTree(BiTree &T)
{
char ch;
/*printf("Input the char\n"); //你把输出语句放到递归的函数里它会输出N多遍,所以,还是放到主函数里吧 */
scanf("%c",&ch); //你忘了取地址符了
/*if(ch == '#')T==NULL;*/ if(ch == '#')T=NULL;//是将T指针赋值为空 ,而不是T==NULL;
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))exit(OVERFLOW);
/**T.data=ch;*/T->data=ch; //楼主要仔细研究一下指向运算符"->"和结构体成员运算符"."的区别,此程序中N多错误都是因为没有区分它们引起的
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
status DLR(BiTree root) //void类型是不能返回值的,所以你可以把函数改成status类型;函数参数不用引用。因为没有改变参数值 ,只是使用
{
if(root!=NULL)
{
printf("%c",root->data);
DLR(root->lchild);
DLR(root->rchild); //这一点属于严重错误,说明你没有弄清递归遍历的过程。是先根,再左 ,再右 。下面还有三个同样的错误
}
return OK;
}
status LDR(BiTree root) //函数参数不用引用。因为没有改变参数值,只是使用
{
if(root!=NULL)
{
LDR(root->lchild);
printf("%c",root->data);
LDR(root->rchild); //同上
}
return OK;
}
status LRD(BiTree root) //函数参数不用引用。因为没有改变参数值,只是使用
{
if(root!=NULL)
{
LRD(root->lchild);
LRD(root->rchild); //同上
printf("%c",root->data);
}
return OK;
}
int i=0;
int Found_SUM(BiTree root)
{
if(root!=NULL)
{
i++;
Found_SUM(root->lchild);
Found_SUM(root->rchild);//递归求结点数 ,和遍历函数有什么关系?
}
return i;
}
int j=0;
int FOUND_LEAVES(BiTree root)
{
if(root!=NULL)
{
if(!root->lchild&&!root->rchild)j++;
FOUND_LEAVES(root->lchild);
FOUND_LEAVES(root->rchild);
}
return j;
}
status InitQueue(LinkQueue &Q) //这儿应该是初始化队列,而不是销毁队列吧 。。。汗 。。。你原来的步骤是用来销毁队列Q的啊
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
status EnQueue(LinkQueue &Q,BiTree e)
{
QueuePtr p=NULL;//p的定义 。。
p=(QueuePtr)malloc(sizeof(QNode));//马虎,少个右括号
if(!p) exit(OVERFLOW);
p->data=e;p->next=NULL;//马虎,两个语句要用";"分开
Q.rear->next=p;
Q.rear=p;
return OK;
}
status DeQueue(LinkQueue &Q,BiTree &e) //此处添加一个引用参数e较为方便 ,你原来的方法太复杂了
{
QueuePtr p;
if(Q.front==Q.rear)return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)Q.rear=Q.front;
free(p);
return OK;
}
status LSCAN(BiTree root)
{
LinkQueue Q;
BiTree e=NULL;
InitQueue(Q);
EnQueue(Q,root);
while(Q.front<Q.rear) //如果队列非空
{
DeQueue(Q,root);
printf("%c",root->data);
if(root->lchild)EnQueue(Q,root->lchild);
if(root->rchild)EnQueue(Q,root->rchild);
}
return OK;
}
void main()
{
BiTree T=NULL;//未定义T
T=(BiTree)malloc(sizeof(BiTNode));
int a,b;
printf("Input the char\n");
CreateBiTree(T);
printf("\n");
printf("先序遍历二叉树:\n");
DLR(T);
printf("\n");
printf("中序遍历二叉树:\n");
LDR(T);
printf("\n");
printf("后序遍历二叉树:\n");
LRD(T);
printf("\n");
printf("层序遍历二叉树:\n");
LSCAN(T);
/*LSCAN(T); // 不知你为啥要用两遍这个函数,给你注释掉了一个*/
a=Found_SUM(T);
b=FOUND_LEAVES(T);
printf("叶子结点数目%d",b);printf("\n");
printf("结点总数%d",a);
printf("\n");
}
关于“数据结构status函数声明(c语言status的用法)”这个话题的介绍,今天小编就给大家分享完了 ,如果对你有所帮助请保持对本站的关注!
本文来自作者[雁白]投稿,不代表易网号立场,如若转载,请注明出处:https://www.ibb4.com/cshi/202508-1616.html
评论列表(4条)
我是易网号的签约作者“雁白”!
希望本篇文章《数据结构status函数声明(c语言status的用法)》能对你有所帮助!
本站[易网号]内容主要涵盖:国足,欧洲杯,世界杯,篮球,欧冠,亚冠,英超,足球,综合体育
本文概览:网上科普有关“数据结构status函数声明(c语言status的用法)”话题很是火热,小编也是针对数据结构status函数声明(c语言status的用法)寻找了一些与之相关的一...