4.19作业

二叉树

#include "btree.h"
node_p creat_btree()  //创建二叉树
{
	char data='\0';  //定义一个char类型的变量,接收输入的结点的值
	scanf(" %c",&data);  //用户终端输入想创建的二叉树(输入结点值),scanf需要空格吸收垃圾字符
	if(data=='#')  //判断用户输入是否有意义的结点值
	{
		return NULL;  //如果是#不创建二叉树
	}
	node_p new=creat_node(data);
	new->lchild=creat_btree();
	new->rchild=creat_btree();
	return new;  //把第一个输入的根结点返回
}
node_p creat_node(char data)  //创建结点
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("创建结点失败\n");
		return NULL;
	}
	new->data=data;
	return new;
}
void pri_show(node_p T)  //先序遍历
{
	if(T==NULL)
		return;
	printf("%c",T->data);
	pri_show(T->lchild);
	pri_show(T->rchild);
}
void mid_show(node_p T)  //中序遍历
{
	if(T==NULL)
		return;
	mid_show(T->lchild);
	printf("%c",T->data);
	mid_show(T->rchild);
}
void last_show(node_p T)  //后序遍历
{
	if(T==NULL)
		return;
	last_show(T->lchild);
	last_show(T->rchild);
	printf("%c",T->data);
}

链式队列

#include "link_queue.h"
Tnode_p create_link_queue()  //创建链式队列
{
	Tnode_p T=(Tnode_p)malloc(sizeof(Tnode));
	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->data=data;
	return new;
}
int empty_link_queue(Tnode_p T)  //判空
{
	if(T==NULL)
	{
		printf("入参为空,请检查\n");
		return -1;
	}
	return T->front==NULL && T->rear==NULL;
}
void push_link_queue(Tnode_p T,int data)  //入队
{
	if(T==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	node_p new=create_node(data);
	if(T->front==NULL)
	{
		new->next=T->front;
		T->front=new;
		T->rear=new;
	}
	else
	{
		new->next=T->rear->next;
		T->rear->next=new;
		T->rear=new;
	}
	printf("入队元素为%d\n",new->data);
}
void pop_link_queue(Tnode_p T)  //出队
{
	if(T==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(empty_link_queue(T))
	{
		printf("队列为空,无需出队\n");
		return;
	}
	printf("出队的元素为%d\n",T->front->data);
	node_p del=T->front;
	T->front=del->next;
	free(del);
}
void show_link_queue(Tnode_p T)  //输出
{
	if(T==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(empty_link_queue(T))
	{
		printf("队列为空,无需输出\n");
		return;
	}
	node_p p=T->front;
	while(p!=NULL)
	{
		printf("%-4d",p->data);
		p=p->next;
	}
	putchar(10);
}

 

相关推荐

  1. 作业..........

    2024-04-21 20:40:01       55 阅读
  2. Day41| 416 分割等和子集

    2024-04-21 20:40:01       46 阅读
  3. 419.甲板上的战舰

    2024-04-21 20:40:01       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-21 20:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-21 20:40:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-21 20:40:01       82 阅读
  4. Python语言-面向对象

    2024-04-21 20:40:01       91 阅读

热门阅读

  1. excel文件预览: luckyexcel+luckysheet

    2024-04-21 20:40:01       131 阅读
  2. 大数据:【学习笔记系列】flink和spark的区别

    2024-04-21 20:40:01       37 阅读
  3. MASA Framework系列-核心概念(2)

    2024-04-21 20:40:01       28 阅读
  4. WPF中TextBox失去焦点事件

    2024-04-21 20:40:01       40 阅读
  5. Linux网络实战(一)- DNS配置

    2024-04-21 20:40:01       42 阅读
  6. 计算机网络——应用层(2)FTP,DNS

    2024-04-21 20:40:01       35 阅读
  7. AMEYA360:兆易创新推出GD32L235系列低功耗MCU新品

    2024-04-21 20:40:01       42 阅读
  8. 基于httpd和lvs的dr模式简单测试

    2024-04-21 20:40:01       40 阅读
  9. 2024-4-17-ARM作业

    2024-04-21 20:40:01       29 阅读
  10. 第十五届蓝桥杯C/C++B组题解

    2024-04-21 20:40:01       30 阅读