数据结构第三章到第九章练习题答案文档格式.docx

上传人:b****5 文档编号:19500481 上传时间:2023-01-06 格式:DOCX 页数:39 大小:516.72KB
下载 相关 举报
数据结构第三章到第九章练习题答案文档格式.docx_第1页
第1页 / 共39页
数据结构第三章到第九章练习题答案文档格式.docx_第2页
第2页 / 共39页
数据结构第三章到第九章练习题答案文档格式.docx_第3页
第3页 / 共39页
数据结构第三章到第九章练习题答案文档格式.docx_第4页
第4页 / 共39页
数据结构第三章到第九章练习题答案文档格式.docx_第5页
第5页 / 共39页
点击查看更多>>
下载资源
资源描述

数据结构第三章到第九章练习题答案文档格式.docx

《数据结构第三章到第九章练习题答案文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构第三章到第九章练习题答案文档格式.docx(39页珍藏版)》请在冰豆网上搜索。

数据结构第三章到第九章练习题答案文档格式.docx

LinkNode*first;

//链表头指针

//stack.h

#include"

.\list.h"

classstack

stack(void);

~stack(void);

LinkNode*top;

friendclassList;

voidPush(constint&

boolPop();

boolIsEmpty(void){return(top==NULL)?

true:

false;

//list.cpp

StdAfx.h"

#using<

mscorlib.dll>

List:

:

List(constint&

x)

first=newLinkNode(x);

List(void)

first=newLinkNode;

~List(void)

makeEmpty();

voidList:

input(intn)

LinkNode*newNode;

first=newLinkNode;

for(inti=0;

i<

n;

i++)

{

intx=rand()%100;

newNode=newLinkNode(x);

newNode->

link=first->

link;

first->

link=newNode;

}

output(void)

LinkNode*current=first->

while(current!

=NULL)

cout<

<

current->

data<

"

"

;

current=current->

cout<

endl;

makeEmpty(void)

LinkNode*p;

while(first->

link!

=NULL){

p=first->

link=p->

deletep;

inverse(void)

stacks;

LinkNode*p=first->

link,*q;

while(p!

s.Push(p->

data);

p=p->

p=first->

while(!

s.IsEmpty())

q=s.top;

p->

data=q->

data;

s.Pop();

//Stack.cpp

.\stack.h"

stack:

stack(void)

top(NULL)

~stack(void)

//入栈

voidstack:

Push(constint&

top=newLinkNode(x,top);

//出栈

boolstack:

Pop()

if(IsEmpty()==true)returnfalse;

LinkNode*p=top;

top=top->

deletep;

returntrue;

//Main.cpp

stdafx.h"

usingnamespaceSystem;

int_tmain()

srand(time(NULL));

Listl;

//调用输入函数

l.input(10);

//调用输出函数

l.output();

调用逆转函数"

l.inverse();

return0;

3.12编写一个算法,将一个非负的十进制整数N转换为另一个基为B的B进制数。

//stack.h

voidPush(constint&

boolPop(int&

//stack.cpp

}

Pop(int&

x=p->

//main.cpp

intn,m,temp,yu;

输入十进制数:

cin>

>

输入基数:

m;

stackl;

while(n!

=0&

&

m!

=0)

temp=n%m;

n=n/m;

l.Push(temp);

l.IsEmpty())

l.Pop(yu);

yu;

}cout<

3.21试编写一个算法,求解最大公因数问题:

在求两个正整数m和n的最大公因数常常使用辗转相除法,反复计算直到余数为零为止。

其递归定义为:

math.h>

intf(intn1,intn2);

intn1,n2;

Enterthefirstpositivenumber:

n1;

Enterthesecondpositivenumber:

n2;

if(n1>

0&

n2>

0)

n1<

和"

n2<

的最大公约数是:

f(n1,n2)<

else

非法数据"

intf(intn1,intn2)

intt;

while(n2!

t=n1%n2;

n1=n2;

n2=t;

}

returnn1;

3.23假设以数组Q[m]存放循环队列中的元素,同时设置一个标志tag,以tag==0和tag==1来区别在对头指针(front)和对尾指针(rear)相等时,队列状态为“空”还是“满”。

试编写与此结构相应的插入(EnQueue)和删除(DeQueue)算法。

//stdafx.h

#include<

time.h>

//SeqQueue.h

classSeqQueue

SeqQueue(intsize=10);

~SeqQueue(void);

intrear,front,tag;

int*element;

intmaxSize;

boolEnQueue(constint&

boolDeQueue(void);

boolIsEmpty(void){return(front==rear&

tag==0)?

boolIsFull(void){return(front==rear&

tag==1)?

friendostream&

operator<

(ostream&

os,SeqQueue&

Q);

//SeqQueue.cpp

.\SeqQueue.h"

SeqQueue:

SeqQueue(intsize)

rear(0),front(0),tag(0),maxSize(size)

element=newint[maxSize];

~SeqQueue(void)

delete[]element;

boolSeqQueue:

EnQueue(constint&

if(IsFull()==true)returnfalse;

element[rear]=x;

rear=(rear+1)%maxSize;

tag=1;

DeQueue(void)

front=(front+1)%maxSize;

tag=0;

ostream&

Q)

intnum;

if(Q.front==Q.rear&

Q.tag==1)

num=Q.maxSize;

num=(Q.rear-Q.front+Q.maxSize)%Q.maxSize;

num;

os<

(i+Q.front)%Q.maxSize<

Q.element[(i+Q.front)%Q.maxSize]<

'

\t'

returnos;

srand(time(NULL));

SeqQueueq;

CalltheEnQueuefunction"

9;

q.EnQueue(rand()%100);

q<

CalltheDeQueuefunction"

q.DeQueue();

第四章数组、串与广义表

4.12若矩阵Am*n中的某一元素A[i][j]是第i行中的最小值,同时又是第j列中的最大值,则称此元素为该矩阵的一个鞍点。

假设以二维数组存放矩阵,试编写一个函数,确定鞍点在数组中的位置(若鞍点存放在时),并分析该函数的时间复杂度。

intA[10][10],rsize,row,column,maxC,minD,max,min;

for(row=0;

row<

10;

row++)

{

for(column=0;

column<

column++)

{

A[row][column]=rand()%(100/(10-row)+100/(10-column));

cout<

A[row][column]<

}

for(rsize=0;

rsize<

rsize++)

minD=0;

min=A[rsize][0];

for(column=1;

if(min>

A[rsize][column])

{

minD=column;

min=A[rsize][minD];

}

maxC=0;

max=A[0][minD];

for(row=1;

if(max<

A[row][minD])

maxC=row;

max=A[row][minD];

if(max==min)

Array["

rsize+1<

]["

minD+1<

]是鞍点"

时间复杂度分析:

第二个for的嵌套循环是寻找鞍点的主要程序,假设数组A[m][n],则第一、二个内嵌循环时间代价分别为t1(n)=O(f(n))和t2(m)=O(g(m)),外循环的时间复杂度为t(m)=O(F(m))则整个程序段的时间复杂度为T=t(m)×

(t1(n)+t2(m))

因为for语句里if后面语句被执行的平均次数为(n-1)+(n-2)…+1+0=n/2;

所以f(n)=n/2*2+n*3+2=n*4+2;

则t1(n)=O(f(n))=O(n),

同理得t2(m)=O(g(m))=O(m),

而最后的if后面语句被执行的平均次数为(m+1)/2

则T=O((2+2+t1+2+t2+1)×

m+2+(m+1)/2)=O(max{m×

n,m×

m})

4.13所谓回文,是指从前向后顺读和从后向前读都一样的不含空白字符的串。

例如did,madamimadam,pop即是回文。

试编写一个算法,以判断一个串是否是回文。

string>

#ifndefstdafx_h

#definestdafx_h

template<

classT>

structLinkNode

Tdata;

LinkNode(constT&

#endif

classCCStack

CCStack(void);

~CCStack(void);

LinkNode<

T>

*top;

voidPush(constT&

TgetTop();

.\cstack.h"

CCStack<

CCStack(void)

~CCStack(void)

voidCCStack<

Push(constT&

top=newLinkNode<

(x,top);

boolCCStack<

*p=top;

typenameT>

TCCStack<

getTop()

Tx;

if(IsEmpty()==true)returnNULL;

x=top->

returnx;

.\cstack.cpp"

stringa;

booltemp=true;

inti=0;

CCStack<

char>

st;

a;

while(a[i]!

='

\0'

st.Push(a[i]);

i++;

i=0;

if(a[i]==st.getTop())

{

st.Pop();

i++;

}

else

temp=false;

break;

if(temp)

此字符是回文"

此字符不是回文"

4.15编写一个算法frequency,统计在一个输入字符串中各个不同字符出现的频度。

用适当的测试数据来验证这个算法。

intk,*num;

char*A;

k=a.length();

A=newchar[k];

num=newint[k];

frequency(a,A,num,k=0);

for(inti=0;

i<

k;

i++)

A[i]<

num[i]<

个"

delete[]A;

delete[]num;

voidfrequency(strings,charA[],intC[],int&

k)

inti,j,len=s.length();

A[0]=s[0];

C[0]=1;

//种类数

k=1;

for(i=1;

len;

C[i]=0;

len

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

当前位置:首页 > 党团工作 > 党团建设

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

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