1> 二叉树的创建、遍历自己实现一遍
bitree.h
#ifndef BITREE_H
#define BITREE_H
#include<myhead.h>
typedef char datatype;
typedef struct Node
{
datatype data;
struct Node *L;
struct Node *R;
}Node,*Bitree;
//创建
Bitree tree_create();
//先序遍历
void prio_show(Bitree B);
//中序遍历
void in_show(Bitree B);
//后序遍历
void post_show(Bitree B);
#endif
bitree.c
#include"bitree.h"
Bitree tree_create()
{
char data = 0;
scanf(" %c",&data);
if(data == '#')
{
return NULL;
}
Bitree B = (Bitree)malloc(sizeof(Node));
if(NULL == B)
{
printf("创建失败\n");
return NULL;
}
B->data = data;
B->L = tree_create();
B->R = tree_create();
return B;
}
//先序遍历
void prio_show(Bitree B)
{
if(B != NULL)
{
printf("%c\t",B->data);
prio_show(B->L);
prio_show(B->R);
}
}
//中序遍历
void in_show(Bitree B)
{
if(B != NULL)
{
prio_show(B->L);
printf("%c\t",B->data);
prio_show(B->R);
}
}
//后序遍历
void post_show(Bitree B)
{
if(B != NULL)
{
prio_show(B->L);
prio_show(B->R);
printf("%c\t",B->data);
}
}
main.c
#include"bitree.h"
#include<myhead.h>
int main(int argc, const char *argv[])
{
Bitree B = tree_create();
if(NULL == B)
{
return -1;
}
printf("创建成功\n");
prio_show(B);
putchar(10);
in_show(B);
putchar(10);
post_show(B);
putchar(10);
return 0;
}
2> 把所有的排序算法自己实现一遍
冒泡排序
void bubble_sort(int *arr,int n)
{
int flag = 0;
for( int i=1;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
if(arr[j]>arr[j+1])
{
flag = 1;
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
if(flag == 0)
{
break;
}
}
}
选择排序
void select_sort(int *arr, int n)
{
int mini = 0;
for(int i=0;i<n;i++)
{
mini = i;
for(int j=i+1;j<n;j++)
{
if(arr[mini]>arr[j])
{
mini=j;
}
}
if(mini != i)
{
int temp = arr[mini];
arr[mini] = arr[i];
arr[i] = temp;
}
}
}
插入排序
void insert_sort(int *arr, int n)
{
int mini =0,i = 0,j = 0;
for(i = 1;i<n;i++)
{
mini = arr[i];
for(j=i-1; mini<=arr[j]&&j>=0; j--)
{
arr[j+1]=arr[j];
}
arr[j+1]=mini;
}
}
快速排序
int part(int *arr ,int low,int high)
{
int X = arr[low];
while(high > low)
{
while(X<arr[high]&&high>low)
{
high--;
}
arr[low] = arr[high];
while(X>arr[low]&&high>low)
{
low++;
}
arr[high] = arr[low];
}
arr[low]=X;
return low;
}
void quick_sort(int *arr ,int low,int high)
{
if(low>=high)
{
return;
}
int mid = part(arr, low, high);
quick_sort(arr, low, mid-1);
quick_sort(arr, mid+1, high);
}
总代码
#include<myhead.h>
void bubble_sort(int *arr,int n)
{
int flag = 0;
for( int i=1;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
if(arr[j]>arr[j+1])
{
flag = 1;
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
if(flag == 0)
{
break;
}
}
}
int part(int *arr ,int low,int high)
{
int X = arr[low];
while(high > low)
{
while(X<arr[high]&&high>low)
{
high--;
}
arr[low] = arr[high];
while(X>arr[low]&&high>low)
{
low++;
}
arr[high] = arr[low];
}
arr[low]=X;
return low;
}
void quick_sort(int *arr ,int low,int high)
{
if(low>=high)
{
return;
}
int mid = part(arr, low, high);
quick_sort(arr, low, mid-1);
quick_sort(arr, mid+1, high);
}
void select_sort(int *arr, int n)
{
int mini = 0;
for(int i=0;i<n;i++)
{
mini = i;
for(int j=i+1;j<n;j++)
{
if(arr[mini]>arr[j])
{
mini=j;
}
}
if(mini != i)
{
int temp = arr[mini];
arr[mini] = arr[i];
arr[i] = temp;
}
}
}
void insert_sort(int *arr, int n)
{
int mini =0,i = 0,j = 0;
for(i = 1;i<n;i++)
{
mini = arr[i];
for(j=i-1; mini<=arr[j]&&j>=0; j--)
{
arr[j+1]=arr[j];
}
arr[j+1]=mini;
}
}
void output(int *arr,int n)
{
for(int i = 0;i<n;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
}
int main(int argc, const char *argv[])
{
int arr[]={3,8,2,4,5,9,6,1,};
int len = sizeof(arr)/sizeof(arr[0]);
insert_sort(arr,len);
//select_sort(arr,len);
//quick_sort(arr,0,len);
//bubble_sort(arr,len);
output(arr,len);
return 0;
}