关于#c语言#的问题:自己写的c语言二叉树的遍历无法输出结果用devc++调试发现进行不了先序遍历#include <iostream>

自己写的c语言二叉树的遍历无法输出结果
用devc++调试发现进行不了先序遍历

#include <iostream>
#include<stdlib.h>
using namespace std;
#define MaxSize 100
//#include "btree.h" //包含二叉树的存储结构的说明  主函数和子函数分开的时候用 

typedef char ElemType;

typedef struct node 
{    
    ElemType data;            // 数据元素
    struct node *lchild;    // 指向左孩子节点
    struct node *rchild;    // 指向右孩子节点
} BTNode;



void CreateBTree(BTNode * &b, char *str)    // 创建二叉树
{
    BTNode*St[MaxSize],*p;
    int top=-1,k,j=0;
    char ch;
    b=NULL;
    ch=str[j];
    while(ch!='\0')
    {
        switch(ch)
        {
            case'(':top++;St[top]=p;k=1;break;
            case')':top--;break;
            case',':k=2;break;
            default:p=(BTNode*)malloc(sizeof(BTNode));
                    p->data=ch;
                    p->lchild=p->rchild=NULL;
                    if(b=NULL)
                    {
                        b=p;
                    }
                    else
                    {
                        switch(k)
                        {
                            case1:St[top]->lchild=p;break;
                            case2:St[top]->rchild=p;break;
                        }
                    }
        }
        j++;
        ch=str[j];
    }
}

// 先序遍历的递归算法
void PreOrder(BTNode *b)
{
    if(b!=NULL)
    {
        printf("%c ",b->data);//访问根节点 
        PreOrder(b->lchild);
        PreOrder(b->rchild);
    }
}

// 中序遍历的递归算法
void InOrder(BTNode *b)
{
    if(b!=NULL)
    {
        PreOrder(b->lchild);
        printf("%c ",b->data); 
        PreOrder(b->rchild);
    }
}

// 后序遍历的递归算法
void PostOrder(BTNode *b)
{
    if(b!=NULL)
    {
        PreOrder(b->lchild);
        PreOrder(b->rchild);
        printf("%c ",b->data); 
    }

    
}

// 以下主函数用做调试
int main()
{
    BTNode *b;
    char str[101];
    cin >> str;
    PreOrder(b);
    printf("\n");
    InOrder(b);
    printf("\n");
    PostOrder(b);
    printf("\n");
    return 0;
}

如下

BTNode *b = new BTNode;
  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7804678
  • 除此之外, 这篇博客: 首次适应算法 动态分区分配方式的模拟 C语言——课程设计实习中的 C语言源程序——建议使用Devc ++ 运行 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #include<stdio.h>
    #include<stdlib.h>
    struct nodespace{
    	int teskid;   // 作业号 
    	int begin;    // 开始地址 
    	int size;     // 大小 
    	int status;   // 状态 0代表占用,1代表空闲 
    	struct nodespace *next;  // 后指针 
    };
    void initNode(struct nodespace *p){
    	if(p == NULL){	//如果为空则新创建一个 
    		p = (struct nodespace*)malloc(sizeof(struct nodespace));
    	}
    	p->teskid = -1;
    	p->begin = 0;
    	p->size = 640;
    	p->status = 1;
    	p->next =NULL; 
    }  
    void myMalloc1(int teskid,int size,struct nodespace *node){
    	while(node != NULL){
    		if(node->status == 1){  //空闲的空间 
    			if(node->size > size){  //当需求小于剩余空间充足的情况 
    				//分配后剩余的空间 
    				struct nodespace *p = (struct nodespace*)malloc(sizeof(struct nodespace));
    				p->begin = node->begin + size;
    				p->size = node->size - size;
    				p->status = 1;
    				p->teskid = -1;
    				//分配的空间 
    				node->teskid = teskid; 
    				node->size = size;
    				node->status = 0;
    				//改变节点的连接 
    				p->next = node->next; 
    				node->next = p;
    				printf("==================================分配内存成功!==================================\n");
    				break; 
    			}else if(node->size == size){ //需求空间和空闲空间大小相等时 
    				node->teskid = teskid; 
    				node->size = size;
    				node->status = 0;
    				printf("==================================分配内存成功!==================================\n");
    				break;
    			}	
    		}
    		if(node->next == NULL){
    			printf("===============================分配失败,没有足够的空间!=============================\n");
    			break;
    		}
    		node = node->next;
    	}
    } 
    void myFree(int teskid,struct nodespace *node){
    	if(node->next == NULL && node->teskid == -1){
    		printf("================================您还没有分配任何作业!================================\n");
    	}
    	
    	while(node != NULL){
    		if(node->status == 1 && node->next->status ==0 && node->next->teskid == teskid){
    			
    			struct nodespace *q = node->next;
    			node->next = node->next->next;
    			free(q);
    			printf("==================================释放内存成功!==================================\n");
    			if(node->next->status == 1){ //下一个空间是空闲空间时 
    				node->size = node->size + node->next->size;
    				struct nodespace *q = node->next;
    				node->next = node->next->next;
    				free(q);
    				printf("==================================释放内存成功!==================================\n");
    			}
    			break;
    		}else if(node->status == 0 && node->teskid == teskid){  //释放空间和空闲空间不连续时  
    			node->status = 1;
    			node->teskid = -1;
    			if(node->next != NULL && node->next->status == 1){ //下一个空间是空闲空间时 
    				node->size = node->size + node->next->size;
    				struct nodespace *q = node->next;
    				node->next = node->next->next;
    				free(q);
    			}
    			printf("==================================释放内存成功!==================================\n");
    			break;
    		}else if(node->next == NULL){  //作业号不匹配时 
    			printf("==================================没有此作业!!==================================\n");
    			break;
    		}
    		node = node->next;
    	}
    	
    	 
    } 
    void printNode(struct nodespace *node){
    	printf("                        内存情况                        \n"); 
    	printf(" -------------------------------------------------------\n");
    	printf("| 起始地址\t结束地址\t大小\t状态\t作业号\t|\n");
    	while(node != NULL){
    		if(node->status==1){
    			printf("| %d\t\t%d\t\t%dKB\tfree\t 无\t|\n", node->begin + 1, node->begin+node->size, node->size);
    		}else{
    			printf("| %d\t\t%d\t\t%dKB\tbusy\t %d\t|\n", node->begin + 1, node->begin+node->size, node->size, node->teskid);
    		}
    		node = node->next;
    	}
    	printf(" -------------------------------------------------------\n");
    }
    void destory(struct nodespace *node){
    	struct nodespace *q = node;
    	while(node != NULL){
    		node = node->next;
    		free(q);
    		q = node;
    	}
    } 
    void menu(){
    	printf("\n"); 
    	printf("\t\t\t\t   ╭═════════════════════════════════○●○●═══╮\n");
    		printf("\t\t\t\t   │    首次适应算法的动态分区分配方式模拟      │\n");
    		printf("\t\t\t\t   ╰═══○●○●═════════════════════════════════╯\n");
    		printf("\t\t\t\t   ┌───────────────────────────────────────────-┐\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   │                 1. 申请内存                │\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   │                 2. 回收内存                │\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   │                 3. 查看内存情况            │\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   │                 4. 退出                    │\n");
    		printf("\t\t\t\t   │                                            │\n");
    		printf("\t\t\t\t   └────────────────────────────────────────────┘\n");
    		printf("\t\t\t\t\t\t  请您选择(1-4):\t");
    }
     
    int main(){
    	// node为整个空间 
    	system("color 0f");
    	//system("mode con cols=120 lines=50");
    	struct nodespace *init = (struct nodespace*)malloc(sizeof(struct nodespace));
    	struct nodespace *node = NULL;
    	initNode(init);			//初始化主链 
    	node = init; 			//指向链表头 
    	int option; 
    	int teskid;
    	int size;
    	while(1){
    		menu();		//打印想要进行的操作
    		scanf("%d",&option);
    		if(option == 1){
    			printf("请输入作业号;");
    			scanf("%d",&teskid);
    			printf("此作业申请的空间大小(KB):");
    			scanf("%d",&size);
    			myMalloc1(teskid,size,node);
    			printf("\n"); 
    			printNode(node);
    		}else if(option == 2){
    			printf("请输入作业号:");
    			scanf("%d",&teskid);
    			myFree(teskid,node);
    			printf("\n"); 
    			printNode(node);
    		}else if(option == 3){
    			printNode(node);
    		}else if(option == 4){
    			destory(node);
    			initNode(init);
    			node = init;
    			break;
    		}else{
    			printf("===========================您的输入有误,请重新输入!============================\n");
    			continue;
    		}
    	}
    return 0;
    }