用自己的方式常见到的是**ptr2 我常用*& ptr2
2012-05-30 11:02
211 查看
1. 让原本指向空的两个指针,赋值
2. 二叉排序树的实现
#include"stdio.h" #include"malloc.h" #include"string.h" void TestFunction(char** ptr1, char*& ptr2)//我经常喜欢用 *&ptr2 { *ptr1 = "abc"; ptr2 = (char*)malloc(6); strcpy(ptr2, "abc"); }
2. 二叉排序树的实现
#include"stdio.h" #include"malloc.h" struct node { int data; node *right; node *left; }; void insert(node *&root,node *&s)//传递的参数是关键 { if(root==NULL) { root=s; } else { if(root->data<s->data)//要插入的数据 data大于节点 则插入右边 insert(root->right,s); else insert(root->left,s); } } void creatTree(node *&root,int a[],int n) { int i; node *s; //root=(node*)malloc(sizeof(node));//不要在这里初始化 或者申请空间之后 root=NULL; for(i=0;i<n;i++) { s=(node *)malloc(sizeof(node)); s->data=a[i]; s->right=NULL; s->left=NULL; insert(root,s); } } void outPut(node *&root) { if(root==NULL) return; else { outPut(root->left); printf("%5d",root->data); outPut(root->right); } } int main() { int a[]={15,2,4,6,7,13,9,3,17,18,20}; node *root; root=(node*)malloc(sizeof(node));//(申请空间大小) root=NULL;//这一句一定不能少 (初始化) creatTree(root,a,11); outPut(root); printf("\n"); return 0; }
相关文章推荐
- 最常用和最简单的方式是用自己网站的英文名称作标志
- 记录一下自己总结出来的,在内网环境下使用maven打包的各种方法,包括各种常用的打包方式(一)
- iOS- 网络访问两种常用方式【GET & POST】实现的几个主要步骤
- android studio自己常用的快捷方式
- Android开发-02-自己常用的一个get和post方式提交类的封装
- iPhone 两种常用的XML解析方式(NSXMLParser & GDataXMLNode)(转载)
- iOS- 网络请求的两种常用方式GET & POST的区别
- 介绍两种常用的XML解析方式(NSXMLParser & GDataXMLNode)
- 记录个人数组、字符串自己常忘记的方法,以及ES常用处理方式
- 常用命令:find & 常用快捷方式
- 两种常用排序方式"插入法","冒泡"
- iOS- 网络请求的两种常用方式【GET & POST】的区别
- dtd与schema的区别&XML文件解析常用方式
- Jquery插件的定义方式:自己总结的两种——(将Date型format成("yyyy年MM月dd日hh小时mm分ss秒")的插件)
- 程序中常用的延时等待处理方式 while((BT_ATSet_Item == BT_ATSet)&& (i--))
- 常用字符集及编码方式(Charset & Encoding)
- IntelliJ IDEA自己常用的快捷键&与eclipse快捷键的对比
- 【转载】JSP常用跳转方式
- Selenium Webdriver元素定位的八种常用方式
- 遍历HashMap的常用两种方式