网友您好, 请在下方输入框内输入要搜索的题目:
题目内容
(请给出正确答案)
设一棵二叉树T以二叉链表为存储结构,试编写一个函数int LeafCount(BiNode *T),求二叉树中叶子结点的个数。 typedef struct Node {int data; struct Node *lchild,*rchild; } BiNode;
参考答案和解析
B
更多 “设一棵二叉树T以二叉链表为存储结构,试编写一个函数int LeafCount(BiNode *T),求二叉树中叶子结点的个数。 typedef struct Node {int data; struct Node *lchild,*rchild; } BiNode;” 相关考题
考题
有以下程序段typedef struct node { int data; struct node *next; } *NODE;NODE p;以下叙述正确的是A)p 是指向 struct node 结构变量的指针的指针B)NODE p ;语句出错C)p 是指向 struct node 结构变量的指针D)p 是 struct node 结构变量
考题
函数 main() 的功能是 : 在带头结点的单链表中查找数据域中值最小的结点 . 请填空#include stdio.hstruct node{ int data;struct node *next;};int min(struct node *first)/* 指针 first 为链表头指针 */{ strct node *p; int m;p=first-next; m=p-data;p=p-next;for(;p!=NULL;p= _[20]_______ )if(p-datam) m=p-data;return m;}
考题
设计递归算法,判断二叉树t是否满足小根堆的特点。二叉链表的类型定义如下:
typedef int datatype;//结点的数据类型,假设为inttypedef struct NODE *pointer;//结点指针类型struct NODE {datatype data;pointer lchild,rchild;};typedef pointer bitree;//根指针类型
考题
有以下程序段 typedef struct node { int data; struct node *next; } *NODE; NODE p; 以下叙述正确的是( )。A.p是指向struct node结构变量的指针的指针B.NODE p;语句出错C.p是指向struct node结构变量的指针D.p是struct node结构变量
考题
●试题三阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明3.1】假设以带头结点的单循环链表作非递减有序线性表的存储结构。函数deleteklist(LinkList head)的功能是删除表中所有数值相同的多余元素,并释放结点空间。例如:链表初始元素为:(7,10,10,21,30,42,42,42,51,70)经算法操作后变为:(7,10,21,30,42,51,70)【函数3.1】void deleteklist(LinkList head){LinkNode*p,*q;p=head-next;while(p!=head){q=p-next;while( (1) ){(2) ;free(q);q=p-next;}p=p-next;}}【说明3.2】已知一棵完全二叉树存放于一个一维数组T[n]中,T[n]中存放的是各结点的值。下面的程序的功能是:从T[0]开始顺序读出各结点的值,建立该二叉树的二叉链表表示。【函数3.2】#includeistream.htypedef struct node {int data;stuct node leftChild,rightchild;}BintreeNode;typedef BintreeNode*BinaryTree;void ConstrncTree(int T[],int n,int i,BintreeNode*&ptr){if(i=n) (3) ;∥置根指针为空else{ptr=-(BTNode*)malloc(sizeof(BTNode))ptr-data=T[i];ConstrucTree(T,n,2*i+1, (4) );ConstrucTree(T,n, (5) ,ptr-rightchild);}}main(void){/*根据顺序存储结构建立二叉链表*/Binarytree bitree;int n;printf("please enter the number of node:\n%s";n);int*A=(int*)malloc(n*sizeof(int));for(int i=0;i<n;i++)scanf("%d,A+i);/*从键盘输入结点值*/for(int i=0;i<n;i++)printf("%d",A[i]);ConstructTree(A,n,0,bitree);}
考题
以下程序把三个NODETYPE型的变量链接成一个简单的链表,并在while循环中输出链表结点数据域中的数据,请填空#include stdio.hstruct node{int data; struct node *next;};typedef struct node NODETYPE;main(){NODETYPE a,b,c,*h,*p;a. data=10;b.data=20;c.data=30;h=a;b. next=b;b.next=c;c.next=’\0’;p=h;while(p){printf(“d”,p-data);【15】;}}
考题
已知一棵二叉树用二叉链表存储,t指向根节点,P指向树中任一节点。下列算法为输出从t到P之问路径上的节点。[C程序]define MaxSize 1000typedef struct node {TelemType data ;struct node *ichiid,*rchiid;}BiNode,*BiTree;void Path(BiTree t,BiNode *P){BiTree *stack[Maxsize],*stackl[Maxsize],*q;int tag[Maxsize],top=0,topl;q=t;/*通过先序遍历发现P*/do{while(q!=NULL &&q!=p)/*扫描左孩子,_日.相应的节点不为P*/{ (1) ;stack[top]=q;tag[top]=0;(2) ;}if(top>0){ if(stack[top]=P) break; /*找到P,栈底到栈顶为t到P*/if(tag[top]==1)top--;else { q=stack[top];q=q->rchiid;tag[top]=1;}}} (3) ;top--;topl=0;while(top>0) {q=stack[top]; /*反向打印准备*/topl++;(4) ;top--;}while( (5) ){ /*打印栈的内容*/q=stackl[topl]jprintf(q->data);topl--;}}
考题
以下程序的功能是:建立一个带布头结点的单向链表,并将存储在数组中的字符依次存储到链表的各个结点中,请从与下划线处号码对应的一组选项中选择出正确的选项#include <stdlib.h>struct node{char data; struct node *next;};(48) CreatList(char*s),{struct node *h,*p,*q;h=(struct node*)malloc(sizeof(struct node));p=q=h;while(*s!="\0"){ p=(struct node*)malloc(sizeof(struct node));p->data= (49) ;q->next=p;q= (50) ;s++;}p->next="\0";return h;}main(){ char str[]="link list";struct node*head;head=CreatList(str);…}(1)A.char*B.struct nodeC.struct node*D.char
考题
阅读下列C函数和函数说明,将应填入(n)处的字句写在对应栏内。【说明】函数DeleteNode (Bitree *r, int e)的功能是:在树根结点指针为r的二叉查找(排序)树上删除键值为e的结点,若删除成功,则函数返回0,否则函数返回-1。二叉查找树结点的类型定义为:typedef struct Tnode{int data; /*结点的键值*/struct Tnode *Lchild, *Rchild; /*指向左、右子树的指针*/}*Bitree:在二叉查找树上删除一个结点时,要考虑3种情况:①若待删除的结点p是叶子结点,则直接删除该结点;②若待删除的结点p只有一个子结点,则将这个子结点与待删除结点的父结点直接连接,然后删除结点p;③若待删除的结点p有两个子结点,则在其左子树上,用中序遍历寻找关键值最大的结点s,用结点s的值代替结点p的值,然后删除结点s,结点s必属于上述①、②情况之一。【函数】int DeleteNode (Bitree *r,int e) {Bitree p=*r,pp,s,c;while ( (1) ){ /*从树根结点出发查找键值为e的结点*/pp=p;if(e<p->data) p=p->Lchild;else p=p->Rchild;}if(!P) return-1; /*查找失败*/if(p->Lchild p->Rchild) {/*处理情况③*/s=(2);pp=pwhile (3) {pp=s;s=s->Rchild;}p->data=s->data; p=s;}/*处理情况①、②*/if ( (4) ) c=p->Lchild;else c=p->Rchild;if(p==*r) *r=c;else if ( (5) ) pp->Lchild=c;else pp->Rchild=c;free (p);return 0;}
考题
试题四阅读下列函数说明和C函数,将应填入 (n) 处的字句写在答题纸的对应栏内。[函数说明]函数DeleteNode(Bitree *r,int e)的功能是:在树根结点指针为r的二叉查找(排序)树上删除键值为e的结点,若删除成功,则函数返回0,否则函数返回-1。二叉查找树结点的类型定义为:typedef struct Tnode{int data;struct Tnode *Lchild,*Rchild;}*Bitree;在二叉查找树上删除一个结点时,要考虑三种情况:1若待删除的结点p是叶子结点,则直接删除该结点;2若待删除的结点p只有一个子结点,则将这个子结点与待删除结点的父结点直接连接,然后删除结点p;3若待删除的结点p有两个子结点,则在其左子树上,用中序遍历寻找关键值最大的结点s,用结点s的值代替结点p的值,然后删除结点s,结点s必属于上述1、2情况之一。[函数代码]int DeleteNode(Bitree *r,int e) {Bitreep = *r, pp, s, c;while( (1) ) { /*从树根结点出发查找键值为e的结点*/pp = p;if ( e p-data) p = p - Lchild;else p = p-Rchild;}if(!p) return –1; /* 查找失败 */if(p-Lchild p-Rchild) { /* 处理情况3 */s = (2);pp = p;while ( (3) ) { pp = s; s = s- Rchild; }p-data = s -data; p = s;}/*处理情况1、2*/if( (4) ) c = p - Lchild;elsec = p - Rchild;if(p == *r) *r = c;elseif ( (5) ) pp - Lchild = c;elsepp-Rchild = c;free(p);return 0;}
考题
以下程序中函数fun的功能是:构成一个如图所示的带头结点的单词链表,在结点的数据域中放入了具有两个字符的字符串。函数disp的功能是显示输出该单链表中所有结点中的字符串。请填空完成函数disp。[*]include<stdio.h>typedef struct node /*链表结点结构*/{char sub[3];struct node *next;}Node;Node fun(char s) /*建立链表*/{ … }void disp(Node *h){ Node *
考题
阅读下列函数说明和C函数,将应填入(n)处。【函数3说明】函数DeleteNode(Bitree * r,int e)的功能是:在树根结点指针为r的二叉查找(排序)树上删除键值为e的结点,若删除成功,则函数返回0,否则函数返回-1。二叉查找树结点的类型定义为:typedef struct Tnode{int data; /*结点的键值*/struct Tnode * Lchild,*Rchild; /*指向左、右子树的指针*/} * Bitree;在二叉查找树上删除一个结点时,要考虑三种情况:①若待删除的结点p是叶子结点,则直接删除该结点;②若待删除的结点p只有一个子结点,则将这个子结点与待删除结点的父结点直接连接,然后删除结点P;③若待删除的结点p有两个子结点,则在其左子树上,用中序遍历寻找关键值最大的结点s,用结点s的值代替结点p的值,然后删除结点s,结点s必属于上述①、②情况之一。【函数3】int DeleteNode(Bitree * r,int e){Bitree p=*r,pp,s,c;while((1)){ /*从树根结点出发查找键值为e的结点*/pp=p;if(e<p->data)p=p->Lchild;else p=p->Rchild;{if(!p)return-1; /*查找失败*/if(p->Lchild p->Rchild){/*处理情况③*/s=(2); pp=p;while((3)){pp=s;s=s->Rchild;}p->data=s->data;p=s;}/*处理情况①、②*/if((4))c=p->Lchild;else c=p->Rchild;if(p==*r)*r=c;else if((5))pp->Lchild=c;else pp->Rchild=c;free(p);return 0;}
考题
阅读以下说明和C语言函数,将应填入(n)处的字句写在对应栏内。[说明]完成以下中序线索化二叉树的算法。[函数]Typedef int datatype;Typedef struct node {Int ltag, rtag;Datatype data;*lchild,* rchild;}bithptr;bithptr pre;void inthread ( p );{if{inthread ( p->lchild );if ( p->lchild==unll ) (1);if ( P->RCHILD=NULL) p->rtag=1;if (2){if (3) pre->rchild=p;if ( p->1tag==1 )(4);}INTHREAD ( P->RCHILD );(5);}}
考题
在C语言中,可以用typedef声明新的类型名来代替已有的类型名,比如有学生链表结点: typedef struct node{ int data; struct node * link; }NODE, * LinkList; 下述说法正确的是______。A.NODE是结构体struct node的别名B.* LinkList也是结构体struct node的别名C.LinkList也是结构体struct node的别名D.LinkList等价于node*
考题
函数min()的功能是:在带头结点的单链表中查找数据域中值最小的结点。请填空includestruc
函数min()的功能是:在带头结点的单链表中查找数据域中值最小的结点。请填空include <stdio.h>struct node{ int data;struct node *next;};int min(struct node *first)/*指针first为链表头指针*/{ struct node *p; int m;p=first->next; re=p->data; p=p->next;for( ;p!=NULL;p=【 】)if(p->data<m ) re=p->data;return m;}
考题
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。[说明]已知一棵二叉树用二叉链表存储,t指向根结点,p指向树中任一结点。下列算法为输出从t到P之间路径上的结点。[C程序]define Maxsize 1000typedef struct node{TelemType data;struct node*1child,*rchild;}BiNode,*BiTree;void Path(BiTree t,BiNode*P){ BiTree*stack[Maxsize],*stackl[Maxsize],*q;int tag[Maxsize],top=0,topl;q=t;/*通过先序遍历发现P*/do(while(q!=NULL q!=p)/*扫描左孩子,且相应的结点不为P*/{ (1);stack[top]=q;tag[top]=0;(2);}if(top>0){ if(stack[top]==P) break; /*找到P,栈底到栈顶为t到P*/if(tag[top]==1)top--;else{q=stack[top];q=q->rchild;tag[top]=1;}}} (3);top--; topl=0;while(top>0){q=stack[top]; /*反向打印准备*/topl++;(4);top--;}while((5)){ /*打印栈的内容*/q=stackl[topl];printf(q->data);topl--;}}
考题
以下程序把三个NODEIYPE型的变量链接成—个简单的链表,并在while循环中输出链表结点数据域中的数据。请填空。include<stdio.h>struct node{ int data;struct node*next;);typedef struct node NODETYPE;main(){ NODETYPEa,b,c,*h,*p;a.data=10;b.data=20;c.data=30;h=a;anext=b;b.next=c;c,next='\0';p=h;while(p){printf("%d,",p->data):【 】;}printf("\n");}
考题
阅读以下说明和C语言函数,将应填入(n)处的语句写在对应栏内。【说明】下面的程序构造一棵以二叉链表为存储结构的二叉树。【函数】BitTree *createbt(BitTree *bt){BitTree *q;struct node *s[30];int j,i;char x;printf("i,x=");scant("%d,%c",i,x);while(i!=0 x!='$'){q=(BitTree *}malloc(sizeof(BitTree));//生成一个结点(1);q->lchild=NULL;q->rchild=NULL;(2) ;if ((3)){j=i/2; // j为i的双亲结点if(i%2==0)(4); //i为j的左孩子else(5); //i为j的右孩子}printf("i,x=");scanf("%d,%c",i,x);}return s[i];}
考题
阅读以下说明和C语言函数,将应填入(n)。【说明】已知包含头结点(不存储元素)的单链表的元素已经按照非递减方式排序,函数 compress(NODE*head)的功能是去掉其中重复的元素,使得链表中的元素互不相同。处理过程中,当元素重复出现时,保留元素第一次出现所在的结点。图2-1(a)、(b)是经函数compress()处理前后的链表结构示例图。链表的结点类型定义如下:typedef struct Node{int data;struct Node *next;}NODE;【C语言函数】void compress(NODE *head){ NODE *ptr,*q;ptr= (1); /*取得第一个元素结点的指针*/while( (2) ptr->next) {q=ptr->next;while(q(3)) { /*处理重复元素*/(4)q->next;free(q);q=ptr->next;}(5) ptr->next;}/*end of while */}/*end of compress*/
考题
阅读以下说明和C函数,将应填入(n)处的字句写在对应栏内。【说明】已知某二叉树的非叶子结点都有两个孩子结点,现将该二叉树存储在结构数组Ht中。结点结构及数组Ht的定义如下:define MAXLEAFNUM 30struct node{char ch; /*当前结点表示的字符,对于非叶子结点,此域不用*/char *pstr; /*当前结点的编码指针,非叶子结点不用*/int parent; /*当前结点的父结点,为0时表示无父结点*/int lchild,rchild;/*当前结点的左、右孩子结点,为0时表示无对应的孩子结点*/};struct node Ht[2*MAXLEAFNUM]; /*数组元素Ht[0]不用*/该二叉树的n个叶子结点存储在下标为1~n的Ht数组元素中。例如,某二叉树如果其存储结构如下图所示,其中,与叶子结点a对应的数组元素下标为1,a的父结点存储在Ht[5],表示为Ht[1].parent=5。Ht[7].parent=0表示7号结点是树根,Ht[7].child=3、Ht[7].rchild=6分别表示7号结点的左孩子是3号结点、右孩子是6号结点。如果用0或1分别标识二叉树的左分支和右分支(如上图所示),从根结点开始到叶子结点为止,按所经过分支的次序将相应标识依次排列,可得到一个0、1序列,称之为对应叶子结点的编码。例如,上图中a,b,c,d的编码分别是100,101,0,11。函数LeafCode(Ht[],n)的功能是:求解存储在Ht中的二叉树中所有叶子结点(n个)的编码,叶子结点存储在Ht[1]~Ht[n]中,求出的编码存储区由对应的数组元素pstr域指示。函数LeafCode从叶子到根逆向求叶子结点的编码。例如,对上图中叶子结点a求编码的过程如下图所示。typedef enum Status {ERROR,OK} Status;【C函数】Status LeafCode(struct node Ht[], int n){int pc, pf; /*pc用于指出树中的结点,pf则指出pc所对应结点的父结点*/int i,start;char tstr[31] = {'\0'}; /*临时存储给定叶子结点的编码,从高下标开始存入*/for(i = 1;(1); i++){ /*对所有叶子结点求编码,i表示叶结点在HT数组中的下标*/start = 29;pc = i; pf = Ht[i].parent;while (pf !=(2)) { /*没有到达树根时,继续求编码*/if ((3).lchild == pc ) /*pc所表示的结点是其父结点的左孩子*/tstr[--start] = '0';elsetstr[--start] = '1';pc =(4); pf = Ht[pf].parent; /*pc和pf分别向根方向回退一层*/}/* end of while */Ht[i].pstr = (char *) malloc(31-start);if (!Ht[i].pstr) return ERROR;strcpy(Ht[i].pstr,(5));}/* end of for */return OK;}/* and of LeafCode */
考题
链表题:一个链表的结点结构struct Node{int data ;Node *next ;};typedef struct Node Node ;(1)已知链表的头结点head,写一个函数把这个链表逆序( Intel)
考题
阅读下列函数说明和C函数,将应填入(n)处的字句写在对应栏内。[说明]二叉树的二叉链表存储结构描述如下:lypedef struct BiTNode{ datatype data;street BiTNode *lchiht, *rchild; /*左右孩子指针*/ } BiTNode, *BiTree;下列函数基于上述存储结构,实现了二叉树的几项基本操作:(1) BiTree Creale(elemtype x, BiTree lbt, BiTree rbt):建立并返回生成一棵以x为根结点的数据域值,以lbt和rbt为左右子树的二叉树;(2) BiTree InsertL(BiTree bt, elemtype x, BiTree parent):在二叉树bt中结点parent的左子树插入结点数据元素x;(3) BiTree DeleteL(BiTree bt, BiTree parent):在二叉树bt中删除结点parent的左子树,删除成功时返回根结点指针,否则返回空指针;(4) frceAll(BiTree p):释放二叉树全体结点空间。[函数]BiTree Create(elemtype x, BiTree lbt, BiTree rbt) { BiTree p;if ((p = (BiTNode *)malloc(sizeof(BiTNode)))= =NULL) return NULL;p->data=x;p->lchild=lbt;p->rchild=rbt;(1);}BiTree InsertL(BiTree bt, elemtype x,BiTree parent){ BiTree p;if (parent= =NULL) return NULL;if ((p=(BiTNode *)malloc(sizeof(BiTNode)))= =NULL) return NULL;p->data=x;p->lchild= (2);p->rchild= (2);if(parent->lchild= =NULL) (3);else{p->lchild=(4);parent->lchild=p;}return bt;}BiTree DeleteL(BiTree bt, BiTree parent){ BiTree p;if (parent= =NULL||parent->lchild= =NULL) return NULL;p= parent->lchild;parent->lchild=NULL;freeAll((5));return bt;
考题
下面程序的功能是建立一个有 3 个 结 点的单向循环链表,然后求各个 结 点数值域 data 中数据的和。请填空。include stdio.hinclude stdlib.hstruct NODE{ int data;struct NODE *next;};main(){ struct NODE *p,*q,*r;int sum=0;p=(struct NODE*)malloc(sizeof(struct NODE));q=(struct NODE*)malloc(sizeof(struct NODE));r=(struct NODE*)malloc(sizeof(struct NODE));p-data=100; q-data=200; r-data=200;p- next =q; q- next =r; r- next =p;sum=p-data+p-next-data+r-next-next 【 19 】 ;printf("%d\n",sum);}
考题
问答题下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数fun()的功能是:将单向链表结点(不包括头结点)数据域为偶数的值累加起来,并作为函数值返回。 请改正函数fun中的错误,使它能得出正确的结果。 注意:部分源程序在文件MODII.C中,不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序:#include #include #include typedef struct aa{ int data; struct aa *next;}NODE;int fun(NODE *h){ int sum=0; NODE *p; p=h-next; /*********found*********/ while(p-next) { if(p-data%2==0) sum+=p-data; /*********found*********/ p=h-next; } return sum;}NODE *creatlink(int n){ NODE *h,*p,*s; int i; h=p=(NODE *)malloc(sizeof(NODE)); for(i=1;idata=rand()%16; s-next=p-next; p-next=s; p=p-next; } p-next=NULL; return h;}outlink(NODE *h){ NODE *p; p=h-next; printf("The LIST: HEAD"); while(p) { printf("-%d",p-data); p=p-next; } printf("");}main(){ NODE *head; int sum; system("CLS"); head=creatlink(10); outlink(head); sum=fun(head); printf("SUM=%d",sum);}
考题
问答题设某带头结头的单链表的结点结构说明如下:typedef struct nodel{int data struct nodel*next;}node;试设计一个算法:void copy(node*headl,node*head2),将以head1为头指针的单链表复制到一个不带有头结点且以head2为头指针的单链表中。
考题
填空题设线性链表的存储结构如下: struct node {ELEMTP data; /*数据域*/ struct node *next; /*指针域*/ } 试完成下列在链表中值为x的结点前插入一个值为y的新结点。如果x值不存在,则把新结点插在表尾的算法。 void inserty(struct node *head,ELEMTP x,ELEMTP y) {s=(struct node *)malloc(sizeof(struct node)); (); if(){s-nexr=head;head=s;} else { q=head;p=q-next; while(p-dqta!=xp-next!=NULL){q=p;()} if(p-data= = x){q-next=s;s-next=p;} else{p-next=s;s-next=NULL;} } }
考题
填空题设线性链表的存储结构如下: struct node {ELEMTP data; /*数据域*/ struct node *next; /*指针域*/ } 试完成下列建立单链表的算法。 creat() {char var; head=(struct node *)malloc(sizeof(struct node)); head-next= () ; while((var=getchar())!=‘/n’){ ptr=( struct node *)malloc(sizeof(struct node)); ptr-data= var ;ptr-next=head-next; head-next= ptr ; } }
热门标签
最新试卷