画二叉树:先序: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);
}