linux学习笔记(文件以及树形结构)

1.stdout: 在显示界面输出(类似于printf)   stdin: 从显示界面输入(类似于scanf)
2.相关的printf的输出条件:1.遇到特定的转义字符,如“/n”   2.遇到scanf时 3.使用fflush,stdout时 4.缓存区满时。
3.fprintf:可在特定的文件区域输出。sprintf:可用于字符串的复制
4.fputs:可在指定的文件中输出。 puts:只能在指定的文件流中输出。(stdout)
5.fgets:可在指定的文件中获取。 gets:只能在指定的文件流中获取。(stdin)
6.putc 和 fputc 作用相同,都是可在指定的文件中输出,但是区别在于 putc 是宏定义而并非真正的函数调用。 putchar则只能在指定的文件流中输出。(stdout)
7.getc 和 fgetc 以及 getchar 和上面的putc类似,只是他为获取内容,读取字符。
8.fopen 是打开一个文件,打开失败会返回一个文件空指针。fclose 用于关闭文件 fwrite 向文件中写入相应的字符,注意写入成功后文件再次打开时光标会在最后。此时想改变光标位置就需要借助 fseek(用于移动光标) fread 从文件中读取数据。

9.相关的 open close write read lseek 功能与上述对应的函数相同,只是输入格式不同,具体可参见相关的man手册。

10.关于树形结构的认识,举一个简单的二叉树的程序来说明。二叉树可类比于链表来学习。

//A(B(D,E(G,H)),C(F( ,I)))
#include<stdio.h>
#include<stdlib.h>


#define MAX 20
typedef char ElementType;


struct TreeNode 
{
ElementType value;
struct TreeNode *left;
struct TreeNode *right;
};


void init(struct TreeNode** root);//因为需要改变其中的内容,所以应该是传地址。
void create(struct TreeNode** root, char *string);
void previsit(struct TreeNode* root);
void midvisit(struct TreeNode* root);
void tailvisit(struct TreeNode* root);
int treedepth(struct TreeNode* root);
void printtree(struct TreeNode* root);
void update(struct TreeNode* root, ElementType old_value, ElementType new_value);
void levelvisit(struct TreeNode* root);
void cleartree(struct TreeNode** root);


int main()
{
struct TreeNode *root;
init(&root);
char *string = "A(B(D,E(G,H)),C(F( ,I)))";
create(&root, string);
previsit(root);
printf("\n");
midvisit(root);
printf("\n");
tailvisit(root);
printf("\n");
printf("depth = %d\n", treedepth(root));
printtree(root);
printf("\n");
update(root, 'A', 'Z');
printtree(root);
printf("\n");
levelvisit(root);
printf("\n");
cleartree(&root);
printtree(root);
printf("\n");
return 0;
}


void init(struct TreeNode** root)
{
*root = NULL;
}
//A(B(D,E(G,H)),C(F( ,I)))
void create(struct TreeNode** root, char *string)
{
struct TreeNode *p;//用来存放新创建的节点
int i = 0;//用来遍历字符串的位置参数
int flag = 1;// 1:左树 2:右树
struct TreeNode* s[MAX];//模拟栈
int top = -1;//层数
while(*(string + i) != '\0')//先列举所有可能出现的情形,然后再对出现的情形进行对应程序的编程。
{
switch(*(string + i))
{
case ' ':
break;
case '('://左子树的出现
if(top == MAX - 1)
{
printf("tree is full\n");
exit(1);
}
top++;
s[top] = p;
flag = 1;
break;
case ')'://右子树的结束
if (-1 == top)
{
printf("error in string\n");
exit;
}
top--;
break;
case ',':
flag = 2;//左子树切换到右子树
break;
default://遇到其他字符,代表新的节点出现
p = (struct TreeNode*)malloc(sizeof(struct TreeNode));
if (NULL == p)
{
exit(1);
}
//对value部分赋值
p->value = *(string + i);
//左右子树的处理
p->left = NULL;
p->right = NULL;
//root节点的处理
if (NULL == *root)
{
//暂时还未有root节点,那么新创建的节点成为root
*root = p;
}
else
{
//已经存在root
if (1 == flag)
{
//新建的节点是左树
s[top]->left = p;
}
else
{
//新建的节点是右树
s[top]->right = p;
}
}
break;
}
i++;
}
}


void previsit(struct TreeNode* root)//采用递归调用的形式进行遍历
{
if (root != NULL)
{
printf("%c\t", root->value);
previsit(root->left);
previsit(root->right);
}
}


void midvisit(struct TreeNode* root)
{
if (root != NULL)
{
midvisit(root->left);
printf("%c\t", root->value);
midvisit(root->right);
}
}


void tailvisit(struct TreeNode* root)
{
if (root != NULL)
{
tailvisit(root->left);
tailvisit(root->right);
printf("%c\t", root->value);
}
}


int treedepth(struct TreeNode* root)
{
if (NULL == root)
{
return 0;
}
else
{
int depthleft = treedepth(root->left);
int depthright = treedepth(root->right);
return depthleft > depthright ? ++depthleft : ++depthright;
}
}


void printtree(struct TreeNode* root)
{
if (root != NULL)
{
printf("%c", root->value);
if (NULL != root->left || NULL != root->right)
{
printf("(");
if (NULL == root->left)
{
printf(" ");
}
else
{
printtree(root->left);
}
if (NULL == root->right)
{
printf(", ");
}
else
{
printf(",");
printtree(root->right);
}
printf(")");
}
}
}


void update(struct TreeNode* root, ElementType old_value, ElementType new_value)
{
if (NULL != root)
{
update(root->left, old_value, new_value);
if (root->value == old_value)
{
root->value = new_value;
}
update(root->right, old_value, new_value);
}
}


void levelvisit(struct TreeNode* root)//按层遍历,建立一个队列的模型
{
struct TreeNode* p;
int front = 0;
int rear = 0;
struct TreeNode* array[MAX];

if (root != NULL)
{
//根元素入队
array[rear] = root;
rear = (rear + 1) % MAX;
}
while (front != rear)
{
p = array[front];
front = (front + 1) % MAX;
printf("%c\t", p->value);
if (NULL != p->left)
{
array[rear] = p->left;
rear = (rear + 1) % MAX;
}
if (NULL != p->right)
{
array[rear] = p->right;
rear = (rear + 1) % MAX;
}
}
}


void cleartree(struct TreeNode** root)
{
if (NULL != *root)
{
cleartree(&((*root)->left));
cleartree(&((*root)->right));
free(*root);
*root = NULL;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值