数据结构 day5

思维导图

1.链式队列

头文件

#ifndef _LINKQUEUE_
#define _LINKQUEUE_
#include <stdio.h>
#include <stdlib.h>
typedef struct T
{
	struct node *front;
	struct node *rear;
}T,*T_p;
typedef struct node
{
	int data;
	struct node *next;
}node,*node_p;
T_p create_linkque();
node_p create_node(int data);
int empty(T_p Q);
void push_que(T_p Q,int data);
void show(T_p Q);
void pop_que(T_p Q);
#endif

主函数

#include "linkque.h"
int main(int argc, const char *argv[])
{
	T_p Q=create_linkque(0);
	push_que(Q,1);
	push_que(Q,2);
	push_que(Q,3);
	push_que(Q,4);
	push_que(Q,5);
	show(Q);
	pop_que(Q);
	pop_que(Q);
	show(Q);
	return 0;
}

自定义函数

#include "linkque.h"
T_p create_linkque(int data)
{
	node_p H=(node_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	H->data=data;
	H->next=NULL;
	T_p Q=(T_p)malloc(sizeof(T));
	if(Q==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	Q->front=H;
	Q->rear=H;
	return Q;
}
node_p create_node(int data)
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	new->data=data;
	return new;
}
int empty(T_p Q)
{
	if(Q==NULL)
	{
		printf("apply fail\n");
		return -1;
	}
	return Q->front==Q->rear?1:0;
}
void push_que(T_p Q,int data)
{
	if(Q==NULL)
	{
		printf("apply fail\n");
		return;
	}
	node_p new=create_node(data);
	new->data=data;
	new->next=NULL;
	Q->rear->next=new;
	Q->rear=new;
}
void show(T_p Q)
{
	if(Q==NULL)
	{
		printf("apply fail\n");
		return;
	}
	node_p p=Q->front;
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	putchar(10);
}
void pop_que(T_p Q)
{
	if(Q==NULL)
	{
		printf("apply fail\n");
		return;
	}
	node_p p=Q->front;
	if(empty(Q))
	{
		printf("queue is empty\n");
		return;
	}
	printf("pop %d\n",p->data);
	Q->front=p->next;
	free(p);
}

功能实现

2.二叉树

头文件

#ifndef _TREE_
#define _TREE_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
	char data;
	struct node *left;
	struct node *right;
}node,*node_p;
node_p create_node(char data);
node_p create_btree();
void pri_show(node_p T);
void mid_show(node_p T);
void post_show(node_p T);
#endif

主函数

#include "tree.h"
int main(int argc, const char *argv[])
{
	node_p T=create_btree();
	printf("pri order traversal:");
	pri_show(T);
	putchar(10);
	printf("mid order traversal:");
	mid_show(T);
	putchar(10);
	printf("mid order traversal:");
	post_show(T);
	putchar(10);
	return 0;
}

自定义函数

#include "tree.h"
node_p create_btree()
{
	char data='\0';
	scanf("%c ",&data);
	if(data=='#')
	{
		return NULL;
	}
	node_p new=create_node(data);
	new->left=create_btree();
	new->right=create_btree();
	return new;
}
node_p create_node(char data)
{
	node_p T=(node_p)malloc(sizeof(node));
	if(T==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	T->data=data;
	T->left=NULL;
	T->right=NULL;
	return T;
}
void pri_show(node_p T)
{
	if(T==NULL)
	{
		return;
	}
	printf("%c ",T->data);
	pri_show(T->left);
	pri_show(T->right);
}
void mid_show(node_p T)
{
	if(T==NULL)
	{
		return;
	}
	mid_show(T->left);
	printf("%c ",T->data);
	mid_show(T->right);
}
void post_show(node_p T)
{
	if(T==NULL)
	{
		return;
	}
	post_show(T->left);
	post_show(T->right);
	printf("%c ",T->data);
}

功能实现

3.画二叉树:

先序:FCADBEHGM

中序:ACBDFHEMG

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值