2024-02-27 作业

作业要求:

  1. 链队的实现(可以只当作链表实现,或者一个链表一个包含指向队头和队尾的结构体)看上课画的图
  2. 二叉树的中序和后序遍历
  3. 整理思维导图
  4. 已知序列,画出二叉树:

        先序:ABCDEFGH 中序:BDCEAFHG

        中序:ABCDEFGHI 后序:ACEDBHIGF

1. 链队的实现(可以只当作链表实现,或者一个链表一个包含指向队头和队尾的结构体)看上课画的图

运行代码:
main.c
#include "link_que.h"
int main()
{
	que_p Q=creat_head();
	push_queue(Q,1);
	push_queue(Q,2);
	push_queue(Q,3);
	push_queue(Q,4);
	push_queue(Q,5);
	push_queue(Q,6);
	push_queue(Q,7);
	print(Q);
	pop_queue(Q);
	pop_queue(Q);
	print(Q);
	free_q(&Q);
	return 0;
}
link_que.c
#include "link_que.h"
que_p creat_head()
{
	que_p Q = (que_p)malloc(sizeof(que_Q));
	if(NULL==Q){
		printf("申请失败\n");
		return NULL;
	}
	link_p T=(link_p)malloc(sizeof(link_que));
	if(NULL==T){
		printf("申请失败\n");
		return NULL;
	}
	Q->rear=T;
	Q->front=Q->rear;
	T->next=NULL;
	T->len=0;
	return Q;
}
link_p creat_node(datatype data)
{
	link_p new=(link_p)malloc(sizeof(link_que));
	if(NULL==new){
		printf("节点申请失败\n");
		return NULL;
	}
	new->data=data;
	return new;	
}
int empty(que_p Q)
{
	if(NULL==Q){
		printf("入参失败\n");
		return -1;
	}
	return (Q->rear==Q->front)?1:0;
}
void push_queue(que_p Q,datatype data)
{
	if(Q==NULL){
		printf("入参失败\n");
		return;
	}
	link_p new=creat_node(data);
	if(empty(Q)){
		Q->front=new;
	}
	new->next=Q->rear->next;
	Q->rear->next=new;
	Q->rear->len++;
}
void pop_queue(que_p Q)
{
	if(NULL==Q){
		printf("入参失败\n");
		return;
	}
	//判空
	if(empty(Q)){
		printf("队列为空\n");	
		return;
	}
	
	link_p p=Q->rear;
	while(p->next!=Q->front){
		p=p->next;
	}
	//p->next=p->front;
	Q->front=p;
	free(p->next);
	Q->front->next=NULL;
	Q->rear->len--;
}
#if 1
void print(que_p Q)
{
	if(NULL==Q){
//	pop_queue(Q);
		printf("入参失败\n");
		return;
	}
	if(empty(Q)){
		printf("队列为空\n");	
		return;
	}
	link_p p=Q->rear;
	do
	{
		p=p->next;
		printf("%d ",p->data);
	}while(p!=Q->front);
	putchar(10);
}
#endif
void free_q(que_p *Q)
{
	if(Q==NULL||*Q==NULL){
		return;
	}
	link_p del=(*Q)->rear;
	link_p p=(*Q)->rear;
	while(p!=NULL){
		del=p;
		p=p->next;
		free(del);
		del=NULL;
	}
	free(*Q);
	*Q=NULL;

}
link_que.h
#ifndef __LINK_QUE_H__
#define __LINK_QUE_H__
#include <stdio.h>
#include <stdlib.h>

typedef int datatype;
typedef struct link_que
{
	datatype data;
	struct link_que *next;
}link_que,*link_p;
typedef struct que_Q
{
	int len;
	link_p front;
	link_p rear;
}que_Q,*que_p;

que_p creat_head();
link_p creat_node(datatype data);
//判空
int empty(que_p Q);
//判满
//入队 头插
void push_queue(que_p Q,datatype data);
//出队 尾删
void pop_queue(que_p Q);
//打印
void print(que_p Q);
//销毁
void free_q(que_p *Q);
#endif
运行截图:

2.二叉树的中序和后序遍历

运行代码:
tree.c
#include "tree.h"

tree_p creat_node(datatype data)
{
	tree_p new=(tree_p)malloc(sizeof(tree));
	if(new==NULL){
		printf("申请失败\n");
	}
	new->data=data;
	return new;
}
tree_p creat_tree()
{
	datatype data=0;
	printf("请输入>>>");
	scanf("%c",&data);
	getchar();
	tree_p new=creat_node(data);
	if(data=='#'){
		return NULL;
	}
	new->lchild=creat_tree();
	new->rchild=creat_tree();
	return new;
}
//前序
void show(tree_p T)
{
	if(T==NULL){
		return;
	}
	printf("%c->",T->data);
	show(T->lchild);
	show(T->rchild);
}
//中序
void show_zhong(tree_p T)
{
	if(T==NULL){
		return;
	}
	if(T!=NULL){
		show_zhong(T->lchild);
		printf("%c->",T->data);
		show_zhong(T->rchild);
	}

}
//后列
void show_hou(tree_p T)
{
	if(NULL==T){
		return;
	}
	if(NULL!=T){
		show_hou(T->lchild);
		show_hou(T->rchild);
		printf("%c->",T->data);
	}
}
tree.h
#ifndef __TREE_H__
#define __TREE_H__
#include <stdio.h>
#include <stdlib.h>
typedef char datatype;
typedef struct tree
{
	datatype data;
	struct tree *lchild;
	struct tree *rchild;
}tree,*tree_p;
tree_p creat_node(datatype data);
tree_p creat_tree();
void show(tree_p T);
void show_zhong(tree_p T);
void show_hou(tree_p T);
#endif
main.c
#include "tree.h"
int main(void)
{
	tree_p T=creat_tree();
	show_hou(T);
	show_zhong(T);
	show(T);
	putchar(10);
	return 0;
}
运行截图:
中列:

后列:

4.

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值