作业要求:
- 链队的实现(可以只当作链表实现,或者一个链表一个包含指向队头和队尾的结构体)看上课画的图
- 二叉树的中序和后序遍历
- 整理思维导图
- 已知序列,画出二叉树:
先序: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;
}