数据结构c语言描述课后答案殷人昆.docx

上传人:b****7 文档编号:8549246 上传时间:2023-01-31 格式:DOCX 页数:16 大小:21.03KB
下载 相关 举报
数据结构c语言描述课后答案殷人昆.docx_第1页
第1页 / 共16页
数据结构c语言描述课后答案殷人昆.docx_第2页
第2页 / 共16页
数据结构c语言描述课后答案殷人昆.docx_第3页
第3页 / 共16页
数据结构c语言描述课后答案殷人昆.docx_第4页
第4页 / 共16页
数据结构c语言描述课后答案殷人昆.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

数据结构c语言描述课后答案殷人昆.docx

《数据结构c语言描述课后答案殷人昆.docx》由会员分享,可在线阅读,更多相关《数据结构c语言描述课后答案殷人昆.docx(16页珍藏版)》请在冰豆网上搜索。

数据结构c语言描述课后答案殷人昆.docx

数据结构c语言描述课后答案殷人昆

 

数据结构c语言描述课后答案殷人昆

【篇一:

数据结构实验代码(清华殷人昆)】

 

序表:

seqlist.h,seqlist.cpp

1、seqlist.h

#ifndefseqlist_h_

#defineseqlist_h_

#includeiostream

usingnamespacestd;

constintdefaultsize=100;

templateclasst

classseqlist{

private:

t*data;//存放数组

intmaxsize;//最大可容纳表项的项数

intlast;//当前已存表项数

public:

seqlist(intsz=defaultsize);//构造函数

~seqlist(){delete[]data;}//析构函数

intlength()const{returnlast+1;}//计算表长度

voidmakeempty(){last=-1;}//把表置空

intsearch(tx)const;//在表中搜索x的位置,返回位置序号

boolinsert(inti,tx);//插入

t*getdata(constinti)const;

voidoutput()const;

};

#includeseqlist.cpp

#endif

2、seqlist.cpp

#ifndefseqlist_cpp_

#defineseqlist_cpp_

#includeiostream

usingnamespacestd;

#includeseqlist.h

templateclasst

seqlistt:

:

seqlist(intsz){//构造函数

if(sz0){

 

maxsize=sz;

last=-1;

data=newt[maxsize];//创建表存储数组if(data==null){//动态分配失败cerr存储分配错误!

endl;

exit

(1);

}

}

}

templateclasst

intseqlistt:

:

search(tx)const{//在表中搜索

序号for(inti=0;i=last;i++)//顺序搜索

if(data[i]==x)

returni+1;//表项序号和表项位置差1

return0;//搜索失败

}

templateclasst

boolseqlistt:

:

insert(inti,consttx){//插入

if(last==maxsize-1)//表满

returnfalse;

if(i0||ilast+1)//参数i不合理

returnfalse;

for(intj=last;j=i;j--)//依次后移

data[j+1]=data[j];

data[i]=x;last++;//插入

 

x的位置,返回位置

returntrue;//插入成功

}

templateclasst

voidseqlistt:

:

output()const{

cout元素个数为last+1:

endl;

for(inti=0;i=last;i++)

cout#i+1:

data[i];;

coutendl;

}

templateclasst

inlinet*seqlistt:

:

getdata(constinti)const{

return(i0i=last+1)?

data[i-1]:

null;

}

 

#endif

二、单链表:

list.h

,list.cpp

1、list.h

#ifndeflist_h_

#definelist_h_

#includeiostream

#includecstdlib

usingnamespacestd;

templateclasst

structlinknode{//

链表结点类的定义

tdata;//数据域

linknodet*link;//

链指针域

linknode(){link=null;}//

构造函数

linknode(titem,linknodet*ptr=null){//

构造函数

data=item;

link=ptr;

}

booloperator==(tx){returndata==x;}//

重载函数,判相等

booloperator!

=(tx){returndata!

=x;}

};

templateclasst

classlist{//单链表类定义,不用继承也可实现

private:

linknodet*first;//

表头指针

public:

list();//构造函数

list(consttx);

list(listtl);//复制构造函数

~list(){makeempty();deletefirst;}//

析构函数

voidmakeempty();//将链表置为空表

intlength()const;//

计算链表的长度

linknodet*search(consttx)const;//

搜索含x元素

linknodet*locate(constinti)const;//

定位第i个元素

t*getdata(constinti)const;//

取出第i元素值

boolsetdata(constinti,consttx);//

更新第i元素值

boolinsert(constinti,consttx);//

在第i元素后插入

boolremove(constinti,tx);//

删除第i个元素

 

boolisempty()const{returnfirst-link==null;}//

linknodet*gethead()const{returnfirst;}

voidsethead(linknodet*f){first=f;}

//voidsort();//排序

voidoutput();

 

判表空否

listtoperator=(listtl){

linknodet*srcptr=l.first;

linknodet*destptr=first=newlinknodet(srcptr-data);if(first==null){

cerr存储分配失败!

endl;

exit

(1);

}

while(srcptr-link!

=null){destptr-link=newlinknodet(srcptr-link-data);if(destptr-link==null){

cerr存储分配失败!

endl;

exit

(1);

}

destptr=destptr-link;

srcptr=srcptr-link;

}

destptr-link=null;

return*this;

}

};

#includelist.cpp

#endif

2、list.cpp

#ifndeflist_cpp_

#definelist_cpp_

#includeiostream

#includecstdlib

usingnamespacestd;

#includelist.h

templateclasst

listt:

:

list(){

first=newlinknodet;

if(first==null){

cerr存储分配失败!

endl;

 

exit

(1);

}

}

templateclasst

listt:

:

list(consttx){

first=newlinknodet(x);

if(first==null){

cerr存储分配失败!

endl;

exit

(1);

}

}

templateclasst

listt:

:

list(listtl){

linknodet*srcptr=l.first;

linknodet*destptr=first=newlinknodet(srcptr-data);if(first==null){

cerr存储分配失败!

endl;

exit

(1);

}

while(srcptr-link!

=null){

destptr-link=newlinknodet(srcptr-link-data);if(destptr-link==null){

cerr存储分配失败!

endl;

exit

(1);

}

destptr=destptr-link;

srcptr=srcptr-link;

}

destptr-link=null;

}

templateclasst

voidlistt:

:

makeempty(){

linknodet*q;

while(first-link!

=null){

q=first-link;//保存被删结点

first-link=q-link;//从链上摘下该结点

deleteq;//删除

}

}

 

templateclasst

intlistt:

:

length()const{

linknodet*p=first-link;

intcount=0;

while(p!

=null){//逐个结点检测

p=p-link;

count++;

}

returncount;

}

templateclasst

linknodet*listt:

:

search(consttx)const{

//在表中搜索含数据x的结点,搜索成功时函数返回该结点地址;否

则返回null。

linknodet*current=first-link;

while(current!

=nullcurrent-data!

=x)//沿着链找含x结点

current=current-link;

returncurrent;

}

templateclasst

linknodet*listt:

:

locate(constinti)const{

//函数返回表中第i个元素的地址。

若i0或i超出表中结点个数,

则返回null。

if(i0)returnnull;//i

不合理

linknodet*current=first;

intk=0;

while(current!

=nullki){

【篇二:

《数据结构》第四章习题参考答案】

 

1、kmp算法的特点是在模式匹配时指示主串的指针不会变小。

2、串是一种数据对象和操作都特殊的线性表。

(√)

5、使用三元组表示稀疏矩阵的非零元素能节省存储空间。

6、插入与删除操作是数据结构中最基本的两种操作,因此这两种操

 

(√

 

√)

 

作在数组中

7、若采用三元组表存储稀疏矩阵,只要把每个元素的行下标和列下

标互换(错

二、单项选择题

1.下面关于串的的叙述中,哪一个是不正确的?

(b)

a.串是字符的有限序列b.空串是由空格构成的串(空串是长度为零的串)

 

c.模式匹配是串的一种重要运算d.串既可以采用顺序存储,也可以采用链式存储

2.有串s1=’abcdefg,’s2=’pqrst,’假设函数con(x,y)返回x和y串的连接串,subs(s,i,j)

返回串s的从序号i的字符开始的j个字符组成的子串,len(s)返回

s

的长度,则

con(subs(s1,2,len(s2)),subs(s1,len(s2),2))

的结果串是(

d)。

a.bcdefb.bcdefgc.bcpqrstd

3、串的长度是指(b)

.cdefgfg

a.串中所含不同字母的个数c.串中所含不同字符的个数

b.串中所含字符的个数

d.串中所含非空格字符的个数

三、填空题

1、串是一种特殊的线性表,其特殊性表现在作集也不同__;串的两种最基本的存储方式是

_数据元素为字符,操

_顺序存储_、__链式

存储_;两个串相等的

充分必要条件是__两串的长度相等且两串中对应位置的字符也相等__。

2、设正文串长度为n,模式串长度为m,则串匹配的kmp算法的时间复杂度为

_o(m+n)__。

3、模式串p=‘abaabcac’的next函数值序列为___。

4、已知数组a[0..9,0..9]的每个元素占5个存储单元,将其按行优先次序存储

在起始地址为1000的连续的内存单元中,则元素a[6,8]的地址为__1340___。

四、综合题

1、kmp算法较brute-force算法有哪些改进?

【解答】

朴素的模式匹配(brute-force)时间复杂度是o(m*n),kmp算法有一定改进,时间复杂度达到o(m+n)。

kmp算法主要优点是主串指针不回溯。

当主串很大不能一次读入内存且经常发生部分匹配时,kmp算法的优点更为突出。

2、课本p1834.1题

【解答】

a[2][2]=644+2*n+2=676

a[3][3]=644+3*n+3=692

 

3、课本p1844.7题

【解答】

三元组表:

row=6,col=7,terms=9{(0,0,12),(0,2,11),(0,5,13),(1,6,14),(2,1,-4),(2,5,3)(3,3,8),(5,1,-9),(5,4,2)}

行指针数组[0,3,4,6,-1,7]二元组

{(0,12),(2,11),(5,13),(6,14),(1,-4),(5,3),(3,8),(1,-9),(4,2)}4、课本p1844.8题

【解答】

s:

next[-1,0,0,1]

t:

next[-1,0,0,0,1,2,1]

r:

next[-1,0,0,0,0,1,1,2,0,1,2,3,1,2,1,1,0,0,1,0,0]

5、课本p1844.9题

【解答】略

【篇三:

《数据结构》第五章习题参考答案】

 

二、单项选择题

1.具有10个叶结点的二叉树中有(b)个度为

b.9c.10d.11

2.树的后根遍历序列等同于该树对应的二叉树的

a.先序序列b.中序序列c.后序序列

 

2的结点。

 

(b)。

 

a.8

3、二叉树的先序遍历和中序遍历如下:

先序遍历:

efhigjk;中序

遍历:

hfiejkg。

该二叉树根的右子树的根是:

(c)a.eb.fc.gd.

h0

4、在下述结论中,正确的是(d)。

①具有n个结点的完全二叉树的深度k必为┌log2(n+1)┐;②二叉

树的度为2;③二叉树的左右子树可任意交换;④一棵深度为k(k≥1)

且有2k-1个结点的二叉树称为满二叉树。

a.①②③b.②③④

c.①②④d.①④

5、某二叉树的后序遍历序列与先序遍历序列正好相反,则该二叉

 

树一定是(d)。

a.空或只有一个结点b.完全二叉树c.二叉排序树d.高度等于其结点数

三、填空题

1、对于一棵具有n个结点的二叉树,对应二叉链接表中指针总数为个,其中个用于指向孩子结点,个指针空闲着。

2、一棵深度为k(k≥1)的满二叉树有k-1______个叶子结点。

 

3、在完全二叉树中,编号为i和j的两个结点处于同一层的条件是

「_22_。

4、某二叉树有20(n0)个叶子结点,有30个结点仅有一个孩子

(n1),则该二

叉树的总结点数为。

(n=n0+n1+n2)(而n0=n2+1)

5、完全二叉树中,结点个数为n,则编号最大的分支结点的编号为

______。

6、已知二叉树前序为abdegcf,中序为dbgeacf,则后序一定是

___。

四、综合题

1、设二叉树采用二叉链表存储结构,结点的数据域

data

为字符类

型。

阅读下列算法,并回答问题:

(1)对于如图所示的二叉树,写出执行函数function的输出结果;

(2)简述函数function的功能。

voidfunction(bintreet){

stackbintreenode*s;bintreenode*p=t.getroot();bintreenode*q;if(p==null)return;do{while(p!

=null){

s.push(p);

if(p-leftchild!

=null)p=p-leftchild;elsep=p-rightchild;}while(!

s.isempty()q=s.gettop()q-rightchild==p){p=s.pop();coutp-data;}

if(!

s.isempty()){

q=s.gettop();p=q-rightchild;}

}while(!

s.isempty());}

1

(1)dbfgeca

(2)函数function的功能是对二叉树进行后序遍历。

2、课本p2465.2题

【解答】

3、课本p2465.3题

【解答】结点个数为n时,深度最小的树的深度为2;它有n-1个

叶结点,1个分支结点;深度最大的树的深度为n;它有1个叶结点,

n-1个分支结点。

4、课本p2465.4题

【解答】

总结点数n=n0+n1+n2+?

+nm

 

总分支数e=n-1=n0+n1+n2+?

+nm-1=m*nm+(m-1)*nm-1+?

+2*n2+n1

m

?

则有n0?

?

?

?

(i?

1)ni?

?

1

?

i?

2

?

5、课本p2465.5题【解答】略

6、课本p2465.6题

【解答】

(1)二叉树的前序序列与中序序列相同:

空树或缺左子树的单支树;

(2)二叉树的中序序列与后序序列相同:

空树或缺右子树的单支树;

(3)二叉树的前序序列与后序序列相同:

空树或只有根结点的二叉树。

7、课本p2465.7题

8、课本p2465.8题

9、课本p2475.14题

【解答】略

10、课本p2475.17题

11、课本p2485.18题

12、课本p2485.19题

5-19答

5-20huffman树

13、课本p2485.20题

各字母的huffman编码:

c1:

0110c2:

10c3:

0000c4:

0111c5:

001c6:

010c7:

11

c8:

0001

14、课本p2485.23题

【解答】

(1)统计二叉树中叶结点个数

intbinarytreetype:

:

leaf(bintreenodetype*ptr){}if(ptr==null)return0;

elseif(ptr-leftchild==nullptr-rightchild==null)return1;elsereturnleaf(ptr-leftchild)+leaf(ptr-rightchild);

(2)交换每个结点的左子女和右子女

voidbinarytreetype:

:

exchange(bintreenodetype*ptr)

{bintreenodetype*temp;

 

if(ptr-leftchild!

=null||ptr-rightchild!

=null){

}

temp=ptr-leftchild;

ptr-leftchild=ptr-rightchild;ptr-rightchild=temp;exchange(ptr-leftchild);exchange(ptr-rightchild);}

15、课本p2485.24题

【解答】

templateclasstype

etype*ptr){//私有函数:

将用t[n]顺序存储的完全二叉树,以i为

根的子树转换成为用二叉链表表示的//以ptr为根的完全二叉树。

用引用型参数ptr将形参的值带回实参。

if(i=n)ptr=null;else{

ptr=newbintreenodetype(t[i]);//建立根结点constructtree(t,

n,2*i+1,ptr-leftchild);constructtree(t,n,2*i+2,ptr-rightchild);}}

16、课本p2495.29题

【解答】

templateclasstypevoid

binarytreetype:

:

fullbintree2array(type*

t){queuebintreenodetype*q;bintreenod

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 历史学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1