4.20作业

本文介绍了如何使用C语言实现链式队列的数据结构,包括创建、空栈检查、入栈、出栈以及显示队列内容。同时展示了如何构建B-树,包括节点的创建、先序、中序和后续遍历函数。
摘要由CSDN通过智能技术生成

画二叉树:先序:FCADBEHGM

中序:ACBDFHEMG

1链式队列

#ifndef __QUE_STACK_H__
#define __QUE_STACK_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}node,*node_p;
typedef struct T
{
	node_p front;
	node_p rear;
}T,*T_p;
T_p create_stack();
node_p create_node(int data);
int empty_stack(T_p T);
void push_stack(T_p T,int data);
void show_stack(T_p T);
void pop_stack(T_p T);
void free_que(T_p *T);

#endif

#include "que_stack.h"
T_p create_stack()
{
	T_p T=(T_p)malloc(sizeof(T));
	if(T==NULL)
	{
		printf("内存申请失败\n");
		return NULL;
	}
	T->front=NULL;
	T->rear=NULL;
	return T;
}
node_p create_node(int data)
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("内存申请失败\n");
		return NULL;
	}
	new->next=NULL;
	new->data=data;
	return new;
}
int empty_stack(T_p T)
{
	if(T==NULL)
	{
		printf("入参为空\n");
		return -1;
	}
	return T->rear==T->front&&T->rear==NULL?1:0;
}
void push_stack(T_p T,int data)
{
	if(T==NULL)
	{
		printf("入参为空\n");
		return;
	}
	node_p new=create_node(data);
	if(empty_stack(T))
	{

		T->front=new;
	//	T->rear=T->front;
	//	new->next=T->rear;
		T->rear=new;
		return;

	}
//	new->next=T->rear;
	T->rear->next=new;
	T->rear=new;
}
void show_stack(T_p T)
{
	
	if(T==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(empty_stack(T))
	{
		printf("链栈为空,无需输出\n");
		return;
	}
	printf("front->");
	node_p p=T->front;
	while(p!=NULL)
	{
		printf("%d->",p->data);
		p=p->next;
	}
	printf("front\n");
}
void pop_stack(T_p T)
{
	if(T==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(empty_stack(T))
	{
		printf("链栈为空,无需出栈\n");
		return;
	}
    node_p del=T->front;
	if(del->next!=NULL)
	{
		node_p p=del->next;
		T->front=p;
		free(del);
	    del=NULL;
		return;
	}
    T->rear=NULL;
	T->front=NULL;
	free(del);
	del=NULL;

}
void free_que(T_p *T)
{
	if(T==NULL||*T==NULL)
	{
		printf("参数为空\n");
		return;
	}
	free(*T);
	*T=NULL;
}

#ifndef __BRTREE_H__
#define __BRTREE_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
	char data;
	struct node *lchild;
	struct node *rchild;
}node,*node_p;
node_p create_btree();
node_p create_node(char data);
void show_xian(node_p T);
void show_zhong(node_p T);
void show_hou(node_p T);



#endif
#include "btree.h"
node_p create_btree()
{
	char data='\0';
	scanf("%c",&data);
	getchar();
	if(data=='#')
	{
		return NULL;
	}
	node_p new=create_node(data);
	new->lchild=create_btree();
	new->rchild=create_btree();
	return new;

}
node_p create_node(char data)
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("内存申请失败\n");
		return NULL;
	}
	new->data=data;
	return new;
}
void show_xian(node_p T)
{
	if(T==NULL)
	{
		return;
	}
	printf("%c",T->data);
	show_xian(T->lchild);
	show_xian(T->rchild);
}
void show_zhong(node_p T)
{
	if(T==NULL)
	{
		return;
	}
	show_zhong(T->lchild);
	printf("%c",T->data);
	show_zhong(T->rchild);
}
void show_hou(node_p T)
{
	if(T==NULL)
	{
		return;
	}
	show_hou(T->lchild);
	show_hou(T->rchild);
	printf("%c",T->data);
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值