`

【算法导论】指针作为形式参数时 改变指针所指的值(二叉排序树)

 
阅读更多
例题一:让原本指向空的两个指针,赋值
<wbr><span style="font-size:18px"><strong>#include"stdio.h"</strong></span> <div><span style="font-size:18px"><strong>#include"malloc.h"</strong></span></div> <div><span style="font-size:18px"><strong>#include"string.h"</strong></span></div> <div><span style="font-size:18px"><strong>void TestFunction(char** ptr1, char*&amp; ptr2)//我经常喜欢用 *&amp;ptr2</strong></span></div> <div><span style="font-size:18px"><strong>{</strong></span></div> <div><span style="font-size:18px"><strong>*ptr1 = "abc";</strong></span></div> <div><span style="font-size:18px"><strong>ptr2 = (char*)malloc(6);</strong></span></div> <div><span style="font-size:18px"><strong>strcpy(ptr2, "abc");</strong></span></div> <div><span style="font-size:18px"><strong>}</strong></span></div> <div><span style="font-size:18px"><br><strong></strong></span></div> <div><span style="font-size:18px"><strong>int main()</strong></span></div> <div><span style="font-size:18px"><strong>{</strong></span></div> <div><span style="font-size:18px"><strong>char* ptr1 = NULL, *ptr2 = NULL;</strong></span></div> <div><span style="font-size:18px"><strong>TestFunction(&amp;ptr1, ptr2);</strong></span></div> <div><span style="font-size:18px"><strong>printf("%s\n", ptr1);</strong></span></div> <div><span style="font-size:18px"><strong>printf("%s\n", ptr2);</strong></span></div> <div><span style="font-size:18px"><strong>free(ptr2);</strong></span></div> <div><span style="font-size:18px"><strong>}</strong></span></div> </wbr>
例题二:二叉排序树的实现
#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);
return 0;
}
分享到:
评论

相关推荐

    二叉排序树插入

    二叉排序树插入

    数据结构课程设计二叉排序树的实现

    二叉排序树的实现 二叉排序补充概念(也可以参考书上第九章第二节) 左子树的数据总是小于根和右子树的数据,这种就叫做二叉排序树,简单一点,二叉排序树左边的数据小于右边. 1)编程实现二叉排序树, 包括生成、...

    二叉排序树的建立、插入、查找和删除

    代码里有二叉排序树插入操作递归算法,二叉排序树插入操作非递归算法,二叉排序树删除操作,创建二叉排序树,二叉排序树查找递归算法,二叉排序树查找非递归算法

    数据结构 二叉排序树算法

    /*用函数实现如下二叉排序树算法: (1) 插入新结点 (2) 前序、中序、后序遍历二叉树 (3) 中序遍历的非递归算法 (4) 层次遍历二叉树 (5) 在二叉树中查找给定关键字(函数返回值为成功1,失败0) (6) ...

    数据结构实验-二叉排序树算法

    掌握二叉排序树的存储方法,实现二叉排序树的创建、查找、插入、删除、平均查找长度等基本操作。 三、实验内容及要求 1、构造二叉排序树的存储结构。 2、实现创建、查找、插入、删除、平均查找长度等操作。

    数据结构课程设计 二叉排序树与平衡二叉排序树

    用二叉链表作存储结构,编写程序实现二叉排序树上的基本操作:以回车('\n')为输入结束标志,输入数列L,生成二叉排序树T......

    二叉排序树与退圈问题

    第一个程序:实现将两个二叉排序树合并为一个二叉排序树的算法; 第二个程序:N个人围成一圈,从第一个人开始计数,凡是数到1,2,4,8,也就是2的N次方的退出圈子。编写程序,把这些人退出的顺序输出,要求用链表。

    实现二叉排序树的各种算法

    SCAU数据结构的综合实验报告 实现二叉排序树的各种算法 因为里面有源程序和报告文档,所以压缩成一个rar文件,只需要解压后即可使用。 包括: 实验的源代码 根据代码写出来的实验报告

    二叉排序树查找算法

    C++编写的查找算法,用二叉排序树查找,是在VC++6.0上实现的

    数据结构___二叉排序树

    一、实验目的 (1)理解动态查找表的动态生成过程; (2)任意给出一组数(不少于10个),建立对应二叉排序树;...(2)实现二叉排序树的插入算法与查找算法,并建立二叉排序树; (3)进行数据查找和建树过程的比较。

    二叉排序树的查找与建立

    老师给的资源,对于数据结构入门的学生很有帮助的。

    C/C++:二叉排序树.rar(含完整注释)

    例如,经过上述操作后,(1)中所得的二叉排序树变为如下形式。 输出该二叉树的中序序列,结果为:70 60 55 53 45 40 37 24 23 12 (6)设计算法统计并输出(1)中所得的二叉排序树中只有一个孩子结点的结点个数。...

    二叉排序树的创建、删除、插入等操作

    二叉排序树的创建、删除、插入等操作

    平衡二叉排序树

    数据结构作业,平衡二叉排序树

    二叉排序树

    二叉树基本操作,包括实现二叉排序树的查找、插入、构造和删除等算法,使用二叉排序树。

    C语言实现二叉排序树生成算法

    输入:待排序数据序列 功能要求:输出平衡的二叉排序树的形态或输出二叉树的三种遍历序列

    写一算法,判断一棵二叉树是否是一棵二叉排序树。

    写一算法,判断一棵二叉树是否是一棵二叉排序树。

    平衡二叉排序树的算法实现

    平衡二叉排序树的算法实现

    二叉查找排序树的实现代码

    最近在研究数据结构这本书,自己动手实现的一个二叉查找排序树的类BinSortTree,实现数据的插入,查找,删除,层序遍历,中序遍历等操作,熟悉数据结构的朋友都知道,根据二叉排序树的定义,中序遍历后得到的序列...

    二叉排序树及其应用

    二叉排序树及其应用(算法与数据课程设计)

Global site tag (gtag.js) - Google Analytics