链式队列实现,二叉树的中序及后序遍历

链式队列:

fun.c

#include"head.h"
qup create_queue(){
	qup q=(qup)malloc(sizeof(struct queue));
	if(NULL==q){
		printf("申请空间失败\n");
		return NULL;
	}

	node head=(node)malloc(sizeof(struct node));
	if(NULL==head){
		printf("空间申请失败");
		return NULL;
	}

	head->len=0;
	head->next=NULL;

	q->front=head;
	q->rear=head;

	return q;
}
node create_node(datatype key){
	node new=(node)malloc(sizeof(struct node));
	if(NULL==new){
		printf("申请空间失败");
		return NULL;
	}
	new->data=key;
	new->next=NULL;
	return new;
}
int input_queue(qup q,datatype key){
	if(NULL==q){
		printf("输入参数无效");
		return -1;
	}
	node rear=q->front;
	while(rear->next!=NULL){
		rear=rear->next;
	}
	rear->next=create_node(key);
	q->front->len++;
	q->rear=rear->next;
	return q->front->len;
}
int output_queue(qup q){
	if(NULL==q){
		printf("输入参数无效");
		return -1;
	}
	node L=q->front;
	node p=L->next;
	L->next=p->next;
	printf("%d->",p->data);
	free(p);
	p=NULL;
	L->len--;
	return L->len;
}
int output_entir_queue(qup q){
	if(NULL==q){
		printf("输入参数无效");
		return -1;
	}
	printf("front->");
	while(output_queue(q)!=0);
	printf("rear\n");
	return 0;
}

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node{
	union{
		datatype data;
		int len;
	};
	struct node* next;
}*node;
typedef struct queue{
	node front;
	node rear;
}*qup;
qup create_queue();
node create_node(datatype key);
int input_queue(qup q,datatype key);
int output_queue(qup q);
int output_entir_queue(qup q);

#endif

main.c

#include"head.h"
int main(int argc, const char *argv[])
{
	qup q=create_queue();
	for(int i=0;i<10;i++){
		input_queue(q,i);
	}
	output_entir_queue(q);
	return 0;
}

终端结果: 

 

ubuntu@ubuntu:seq_looplist$ gcc *.c
ubuntu@ubuntu:seq_looplist$ ./a.out
front->0->1->2->3->4->5->6->7->8->9->rear

二叉树:

void mid_output(btree tree){
	if(tree==NULL)return;
	mid_output(tree->left);
	printf("%c\t",tree->data);
	mid_output(tree->right);
}
void end_output(btree tree){
	if(tree==NULL)return;
	end_output(tree->left);
	end_output(tree->right);
	printf("%c\t",tree->data);
}

  • 18
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值