树
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char datatype;
typedef struct Node{
//数据域
datatype data;
//指针域
struct Node *left;
struct Node *right;
}*BTree;
BTree creat();
void first(BTree T);
void mid(BTree T);
void last(BTree T);
#endif
main.c
#include"head.h"
int main(int argc,const char *argv[])
{
BTree T=creat();
printf("先序遍历:");
first(T);
printf("\n");
printf("中序遍历:");
mid(T);
printf("\n");
printf("后序遍历:");
last(T);
printf("\n");
return 0;
}
txt.c
#include"head.h"
/*
*function : 树的创建并输入
*@param [in]: 无参数
*@param [out]:
*@return : 成功返回首地址,失败返回NULL
*/
BTree creat(){
datatype e;
printf("请输入一个字符:");
scanf("%c",&e);
getchar();
if(e=='#')
return NULL;
BTree T=(BTree)malloc(sizeof(struct Node));
if(T==NULL)
return NULL;
T->data=e;
T->left=creat();
T->right=creat();
return T;
}
/*
*function : 先序遍历
*@param [in]: 树
*@param [out]:
*@return : 无返回值
*/
void first(BTree T){
if(T==NULL)
return ;
printf("%c\t",T->data);
first(T->left);
first(T->right);
}
/*
*function : 中序遍历
*@param [in]: 树
*@param [out]:
*@return : 无返回值
*/
void mid(BTree T){
if(T==NULL)
return ;
mid(T->left);
printf("%c\t",T->data);
mid(T->right);
}
/*
*function : 后序遍历
*@param [in]: 树
*@param [out]:
*@return : 无返回值
*/
void last(BTree T){
if(T==NULL)
return ;
last(T->left);
last(T->right);
printf("%c\t",T->data);
}
快速排序和直接插入排序
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Output(int *p,int n);
void direct_insert_sort(int *p,int n);
int one_sort(int *p,int low,int high);
void quick_sort(int *p,int low,int high);
void Bubble(int *p,int n);
void easy_sort(int *p,int n);
#endif
main.c
#include "head.h"
int main(int argc,const char *argv[])
{
int arr[]={4,2,6,4,9,1,6,5};
int len=sizeof(arr)/sizeof(arr[0]);
Output(arr,len);
// direct_insert_sort(arr,len);
// quick_sort(arr,0,len-1);
// Bubble(arr,len);
easy_sort(arr,len);
Output(arr,len);
return 0;
}
txt.c
#include "head.h"
/*
*function : 遍历数组
*@param [in]: 数组,数组长度
*@param [out]:
*@return : 无返回值
*/
void Output(int *p,int n){
for(int i=0;i<n;i++){
printf("%d\t",*(p+i));
}
printf("\n");
}
/*
*function : 直接插入排序,升序
*@param [in]: 数组,数组长度
*@param [out]:
*@return : 无返回值
*/
void direct_insert_sort(int *p,int n){
int j;
for(int i=1;i<n;i++){
int temp=*(p+i);
for(j=i-1;j>=0&&temp<*(p+j);j--){
*(p+j+1)=*(p+j);
}
*(p+j+1)=temp;
}
}
/*
*function : 快排的一次排序
*@param [in]: 数组,最小下标,最大下标
*@param [out]:
*@return : 返回基准值第下标
*/
int one_sort(int *p,int low,int high){
int key=*(p+low);//保存基准值
while(low<high){
while(low<high&&key<=*(p+high)){
high--;
}
*(p+low)=*(p+high);
while(low<high&&key>=*(p+low)){
low++;
}
*(p+high)=*(p+low);
}
*(p+low)=key;
return low;
}
/*
*function : 快速排序,升序
*@param [in]: 数组,最小下标,最大下标
*@param [out]:
*@return : 无返回值
*/
void quick_sort(int *p,int low,int high){
if(low<high){
int mid=one_sort(p,low,high);
//递归左边
quick_sort(p,low,mid-1);
//递归右边
quick_sort(p,mid+1,high);
}
}
/*
*function : 冒泡排序
*@param [in]: 数组,数组长度
*@param [out]:
*@return : 无返回值
*/
void Bubble(int *p,int n){
for(int i=1;i<n;i++){
int count=0;
for(int j=0;j<n-i;j++){
if(*(p+j)>*(p+j+1)){
int temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
count++;
}
}
if(count==0)
break;
}
}
/*
*function : 简单选择排序
*@param [in]: 数组,数组长度
*@param [out]:
*@return : 无返回值
*/
void easy_sort(int *p,int n){
for(int i=0;i<n-1;i++){
int mini=i;
for(int j=i+1;j<n;j++){
if(*(p+j)<*(p+mini)){
mini=j;
}
}
if(mini!=i){
int temp=*(p+mini);
*(p+mini)=*(p+i);
*(p+i)=temp;
}
}
}