1.链式队列代码
#include "list.h"
int main()
{
qa_p T=insert();
push_qaz(T,11);
push_qaz(T,22);
push_qaz(T,33);
push_qaz(T,44);
show_qaz(T);
printf("出队元素:%d\n",pop_qaz(T));
wsx_qaz(&T);
return 0;
}
#ifndef __LIST_H__
#define __LIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int data;
struct node *next;
}node,*node_p;
typedef struct T
{
node_p front;
node_p rear;
}qa,*qa_p;
//初始化
qa_p insert();
//创建结点
node_p create_node(int data);
//判空
int empty_qaz(qa_p T);
//入队
void push_qaz(qa_p T,int data);
//出队
int pop_qaz(qa_p T);
//输出链式列
void show_qaz(qa_p T);
//销毁链式列
void wsx_qaz(qa_p *T);
#endif
#include "list.h"
//初始化
qa_p insert()
{
qa_p T=(qa_p)malloc(sizeof(qa));
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_qaz(qa_p T)
{
if(T==NULL)
{
printf("入参为空\n");
return -1;
}
return T->front==NULL;
}
//入队
void push_qaz(qa_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_qaz(T))
{
T->front=T->rear=new;
}
else
{
T->rear->next=new;
T->rear=new;
}
}
//出队
int pop_qaz(qa_p T)
{
if(T==NULL)
{
printf("入参为空\n");
return -1;
}
if(empty_qaz(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_qaz(qa_p T)
{
if(T==NULL)
{
printf("入参为空\n");
return;
}
if(empty_qaz(T))
{
printf("链式列为空\n");
return;
}
node_p p=T->front;
for(;p!=NULL;p=p->next)
{
printf("%-4d",p->data);
}
putchar(10);
}
//销毁链式列
void wsx_qaz(qa_p *T)
{
if(empty_qaz(*T))
{
printf("入参为空\n");
}
if(empty_qaz(*T))
{
pop_qaz(*T);
}
free(*T);
*T=NULL;
}
2.二叉树 先序遍历 中序遍历 后序遍历
#include "btree.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;
}
#ifndef __BTREE_H__
#define __BTREE_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
#include "btree.h"
//创建二叉树的代码
node_p create_btree()
{
//定义一个char类型的变量,接受输入的结点的值
char data='\0';
//用户终端输入想创建的二叉树(输入结点值)
scanf(" %c",&data);
//判断用户输入是否有意义的结点值
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);
}
3.画二叉树:先序:F C A D B E H G M
中序:A C B D F H E M G