链式队列代码
#include "list.h"
int main(int argc, const char *argv[])
{
zt_p T=initia();push_que(T,99);
push_que(T,100);
push_que(T,101);
push_que(T,102);
show_que(T);
printf("出队元素:%d\n",pop_que(T));
show_que(T);
des_que(&T);
return 0;
}#ifndef __LIST_H__
#define __LIST_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;
}zt,*zt_p;
//初始化
zt_p initia();
//创建节点
node_p create_node(int data);
//判空
int empty_que(zt_p T);
//入队
void push_que(zt_p T,int data);
//出队
int pop_que(zt_p T);
//输出链式列
void show_que(zt_p T);
//销毁链式列
void des_que(zt_p *T);
#endiflist.c
#include"list.h"
//初始化
zt_p initia()
{
zt_p T=(zt_p)malloc(sizeof(zt));
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_que(zt_p T)
{
if(T==NULL)
{
printf("入参为空\n");
return -1;
}
return T->front==NULL;
}
//入队
void push_que(zt_p T,int data)
{
if(T==NULL)
{
printf("入参为空\n");
return;
}
node_p new=(node_p)malloc(sizeof(node));
if(new==NULL)
{
printf("空间申请失败\n");
return;
}
new->data=data;
new->next=NULL;
if(empty_que(T))
{
T->front=T->rear=new;
}
else
{
T->rear->next=new;
T->rear=new;
}
}
//出队
int pop_que(zt_p T)
{
if(T==NULL)
{
printf("入参为空\n");
return -1;
}
if(empty_que(T))
{
printf("链式列为空\n");
return -2;
}
node_p p=T->front;
int data=p->data;
T->front=T->front->next;
if(T->front==NULL)
{
T->rear=NULL;
}
free(p);
return data;
}
//输出链式列
void show_que(zt_p T)
{
if(T==NULL)
{
printf("入参为空\n");
return;
}
if(empty_que(T))
{
printf("链式列为空\n");
return;
}
node_p p=T->front;
for(;p!=NULL;p=p->next)
{
printf("%-4d",p->data);
}
putchar(10);
}
//销毁链式列
void des_que(zt_p *T)
{
if(empty_que(*T))
{
printf("入参为空\n");
}
if(empty_que(*T))
{
pop_que(*T);
}
free(*T);
*T=NULL;
}
3.二叉树中序遍历,后序遍历代码
main.c
#include "binary_tree.h"
int main()
{
node_p T = create_btree();
//调用先序遍历的函数
pri_show(T);
putchar(10);
cen_show(T);
putchar(10);
lad_show(T);
putchar(10);
return 0;
}
binary_tree.h
#ifndef __Binary_H__
#define __Binary_H__
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node *lchild; //左子树
struct node *rchild; //右子树
}node,*node_p;
//创建结点的函数
node_p create_node(char data);
//创建二叉树的代码
node_p create_btree();
//二叉树的先序遍历
void pri_show(node_p T);
//二叉树的中序遍历
void cen_show(node_p T);
//二叉树的后序遍历
void lad_show(node_p T);
#endif
binary_tree.c
#include"binary_tree.h"
//创建二叉树的代码
node_p create_btree()
{
//定义一个char类型的变量,接收输入的结点的值
char data = '\0';
//用户终端输入想创建的二叉树(输入结点值)
scanf(" %c ",&data); //scanf需要吸收垃圾字符
//判断用户输入是否是有意义的结点值
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 pri_show(node_p T)
{
//先序遍历:根左右
if(T==NULL)
{
return;
}
printf("%c",T->data);
//先序遍历左子树
pri_show(T->lchild);
//再遍历右子树
pri_show(T->rchild);
}
//二叉树的中序遍历
void cen_show(node_p T)
{
//中序遍历:左根右
if(T==NULL)
{
return;
}
//先遍历左子树
cen_show(T->lchild);
printf("%c",T->data);
//再遍历右子树
cen_show(T->rchild);
}
//二叉树的后序遍历
void lad_show(node_p T)
{
//后序遍历:左右根
if(T==NULL)
{
return;
}
//先遍历左子树
lad_show(T->lchild);
//再遍历右子树
lad_show(T->rchild);
printf("%c",T->data);
}
4.画二叉树:先序:FCADBEHGM
中序:ACBDFHEMG