Linux C语言编程基础
二叉树
项目目录tree:
模块代码:
点击查看代码
/* main.c */
#include<stdio.h>
#include<stdlib.h>
#include "20191324linkedlist.h"
#define N 7
int nodeValue[N] = {50, 30, 20, 40, 70, 60, 80};
NODE *new_node(int key)
{
NODE *node = (NODE *)malloc(sizeof(NODE));
node->key = key;
node->left = node->right = NULL;
return node;
}
NODE *insert(NODE *node, int key)
{
if(node == NULL)
return new_node(key);
if(key < node->key)
node->left = insert(node->left, key);
else if(key > node->key)
node->right = insert(node->right, key);
return node;
}
int main()
{
int i;
NODE *root = NULL;
root = insert(root, nodeValue[0]);
for (i=1; i<N; i++)
{
insert(root, nodeValue[i]);
}
Pre(root);
printf("\n");
In(root);
printf("\n");
Post(root);
}
点击查看代码
/* Pre */
先序
#include<stdio.h>
#include<stdlib.h>
#include"20191324linkedlist.h"
void Pre(NODE *T){
if(T){
printf("%d ",T->key);
Pre(T->left);
Pre(T->right);
}
}
点击查看代码
/* In */
中序
#include<stdio.h>
#include<stdlib.h>
#include"20191324linkedlist.h"
void In(NODE *T){
if(T){
In(T->left);
printf("%d ",T->key);
In(T->right);
}
}
点击查看代码
/* Post */
后序
#include<stdio.h>
#include<stdlib.h>
#include"20191324linkedlist.h"
void Post(NODE *T){
if(T){
Post(T->left);
Post(T->right);
printf("%d ",T->key);
}
}
点击查看代码
/* 20191324linkedlist.h */
头文件
void Pre(NODE *T);
void In(NODE *T);
void Post(NODE *T);
#endif
gcc练习:
库生成及调用(打印顺序先序,中序,后序)
生成:
静态库调用:
动态库调用:
makefile
点击查看代码
o=libs/tree.o libs/tree2.o libs/tree3.o
All:bin/test libs/libtree.a libs/tree.so
bin/test:src/main.c libs/libtree.a
gcc src/main.c -static -Iinclude -Llibs -lstree -o bin/test
libs/libtree.a:$(o)
ar rcs libs/libtree.a $(o)
libs/tree.so:$(o)
gcc -shared -o libs/tree.so $(o)
libs/tree2.o:src/tree2.c include/20191324linkedlist.h
gcc -c -fPIC -Iinclude src/tree2.c -o libs/tree2.o
libs/tree.o:src/tree.c include/20191324linkedlist.h
gcc -c -fPIC -Iinclude src/tree.c -o libs/tree.o
libs/tree3.o:src/tree3.c include/20191324linkedlist.h
gcc -c -fPIC -Iinclude src/tree3.c -o libs/tree3.o
cgbd
输入gcc src/main.c -static -Iinclude -Llib -lstree -o bin/test
cgdb bin/test
进入调试界面: