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

上传人:b****5 文档编号:6486698 上传时间: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

数据结构第三章到第九章练习题答案

第三章栈和队列

3.10借助栈实现单链表上的逆置运算。

//stdafx.h

#include

#include

#include"time.h"

usingnamespacestd;

//结点类

structLinkNode{

intdata;

LinkNode*link;

LinkNode(constint&item,LinkNode*ptr=NULL)

{data=item;link=ptr;}

LinkNode(LinkNode*ptr=NULL){link=ptr;}

};

//list.h

#pragmaonce

//带附加头结点的单链表

classList

{

public:

List(void);//构造函数

List(constint&x);//构造函数

~List(void);//析构函数

voidinput(intn);//输入

voidoutput(void);//输出

voidmakeEmpty(void);//清空

voidinverse(void);//逆转函数

protected:

LinkNode*first;//链表头指针

};

//stack.h

#pragmaonce

#include".\list.h"

classstack

{

public:

stack(void);

~stack(void);

protected:

LinkNode*top;

friendclassList;

public:

voidPush(constint&x);

boolPop();

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

true:

false;};

};

//list.cpp

#include"StdAfx.h"

#include".\list.h"

#using

List:

:

List(constint&x)

{

first=newLinkNode(x);

}

List:

:

List(void)

{

first=newLinkNode;

}

List:

:

~List(void)

{

makeEmpty();

}

voidList:

:

input(intn)

{

LinkNode*newNode;

first=newLinkNode;

for(inti=0;i

{

intx=rand()%100;

newNode=newLinkNode(x);

newNode->link=first->link;

first->link=newNode;

}

}

voidList:

:

output(void)

{

LinkNode*current=first->link;

while(current!

=NULL)

{

cout<data<<"";

current=current->link;

}

cout<

}

voidList:

:

makeEmpty(void)

{

LinkNode*p;

while(first->link!

=NULL){

p=first->link;

first->link=p->link;

deletep;

}

}

//逆转函数

voidList:

:

inverse(void)

{

stacks;

LinkNode*p=first->link,*q;

while(p!

=NULL)

{

s.Push(p->data);

p=p->link;

}

p=first->link;

while(!

s.IsEmpty())

{

q=s.top;

p->data=q->data;

p=p->link;

s.Pop();

}

}

//Stack.cpp

#include"StdAfx.h"

#include".\stack.h"

#using

stack:

:

stack(void)

:

top(NULL)

{

}

stack:

:

~stack(void)

{

}

//入栈

voidstack:

:

Push(constint&x)

{

top=newLinkNode(x,top);

}

//出栈

boolstack:

:

Pop()

{

if(IsEmpty()==true)returnfalse;

LinkNode*p=top;

top=top->link;

deletep;

returntrue;

}

//Main.cpp

#include"stdafx.h"

#include".\list.h"

#using

usingnamespaceSystem;

int_tmain()

{

srand(time(NULL));

Listl;

//调用输入函数

l.input(10);

//调用输出函数

l.output();

cout<<"调用逆转函数"<

l.inverse();

//调用输出函数

l.output();

return0;

}

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

//stdafx.h

#include

#include

usingnamespacestd;

structLinkNode{

intdata;

LinkNode*link;

LinkNode(constint&item,LinkNode*ptr=NULL)

{data=item;link=ptr;}

LinkNode(LinkNode*ptr=NULL){link=ptr;}

};

//stack.h

#pragmaonce

classstack

{

public:

stack(void);

~stack(void);

voidPush(constint&x);

boolPop(int&x);

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

true:

false;};

protected:

LinkNode*top;

};

//stack.cpp

#include"StdAfx.h"

#include".\stack.h"

#using

stack:

:

stack(void)

:

top(NULL)

{

}

stack:

:

~stack(void)

{

}

voidstack:

:

Push(constint&x)

{

top=newLinkNode(x,top);

}

boolstack:

:

Pop(int&x)

{

if(IsEmpty()==true)returnfalse;

LinkNode*p=top;

top=top->link;

x=p->data;

deletep;

returntrue;

}

//main.cpp

#include"stdafx.h"

#include".\stack.h"

#using

usingnamespaceSystem;

int_tmain()

{

intn,m,temp,yu;

cout<<"输入十进制数:

";

cin>>n;

cout<<"输入基数:

";

cin>>m;

stackl;

while(n!

=0&&m!

=0)

{

temp=n%m;

n=n/m;

l.Push(temp);

}

while(!

l.IsEmpty())

{

l.Pop(yu);

cout<

}cout<

return0;

}

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

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

其递归定义为:

//stdafx.h

#include

#include

#include

usingnamespacestd;

intf(intn1,intn2);

//main.cpp

#include"stdafx.h"

#using

usingnamespaceSystem;

int_tmain()

{

intn1,n2;

cout<<"Enterthefirstpositivenumber:

"<

cin>>n1;

cout<<"Enterthesecondpositivenumber:

"<

cin>>n2;

if(n1>0&&n2>0)

{

cout<

"<

}

else

cout<<"非法数据"<

return0;

}

intf(intn1,intn2)

{

intt;

while(n2!

=0)

{

t=n1%n2;

n1=n2;

n2=t;

}

returnn1;

}

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

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

//stdafx.h

#include

#include

#include

usingnamespacestd;

//SeqQueue.h

#pragmaonce

classSeqQueue

{

public:

SeqQueue(intsize=10);

~SeqQueue(void);

protected:

intrear,front,tag;

int*element;

intmaxSize;

public:

boolEnQueue(constint&x);

boolDeQueue(void);

boolIsEmpty(void){return(front==rear&&tag==0)?

true:

false;}

boolIsFull(void){return(front==rear&&tag==1)?

true:

false;}

friendostream&operator<<(ostream&os,SeqQueue&Q);

};

//SeqQueue.cpp

#include"StdAfx.h"

#include".\SeqQueue.h"

#using

SeqQueue:

:

SeqQueue(intsize)

:

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

{

element=newint[maxSize];

}

SeqQueue:

:

~SeqQueue(void)

{

delete[]element;

}

boolSeqQueue:

:

EnQueue(constint&x)

{

if(IsFull()==true)returnfalse;

element[rear]=x;

rear=(rear+1)%maxSize;

tag=1;

returntrue;

}

boolSeqQueue:

:

DeQueue(void)

{

if(IsEmpty()==true)returnfalse;

front=(front+1)%maxSize;

tag=0;

returntrue;

}

ostream&operator<<(ostream&os,SeqQueue&Q)

{

intnum;

if(Q.front==Q.rear&&Q.tag==1)

num=Q.maxSize;

else

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

for(inti=0;i

os<<(i+Q.front)%Q.maxSize<<":

"<

returnos;

}

//main.cpp

#include"stdafx.h"

#include".\SeqQueue.h"

#using

usingnamespaceSystem;

int_tmain()

{

srand(time(NULL));

SeqQueueq;

cout<<"CalltheEnQueuefunction"<

for(inti=0;i<9;i++)

{

q.EnQueue(rand()%100);

}

cout<

cout<<"CalltheDeQueuefunction"<

q.DeQueue();

q.DeQueue();

cout<

return0;

}

第四章数组、串与广义表

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

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

//stdafx.h

#include

#include

usingnamespacestd;

//main.cpp

#include"stdafx.h"

#include

#using

usingnamespaceSystem;

int_tmain()

{

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

srand(time(NULL));

for(row=0;row<10;row++)

{

for(column=0;column<10;column++)

{

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

cout<

}

}

for(rsize=0;rsize<10;rsize++)

{

minD=0;

min=A[rsize][0];

for(column=1;column<10;column++)

{

if(min>A[rsize][column])

{

minD=column;

min=A[rsize][minD];

}

}

maxC=0;

max=A[0][minD];

for(row=1;row<10;row++)

{

if(max

{

maxC=row;

max=A[row][minD];

}

}

if(max==min)

cout<<"Array["<

}

return0;

}

时间复杂度分析:

第二个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即是回文。

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

//stdafx.h

#include

#include

#include

usingnamespacestd;

//结点类

#ifndefstdafx_h

#definestdafx_h

template

structLinkNode

{

Tdata;

LinkNode*link;

LinkNode(constT&item,LinkNode*ptr=NULL)

{data=item;link=ptr;}

LinkNode(LinkNode*ptr=NULL){link=ptr;}

};

#endif

//stack.h

#pragmaonce

template

classCCStack

{

public:

CCStack(void);

~CCStack(void);

protected:

LinkNode*top;

public:

voidPush(constT&x);

boolPop();

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

true:

false;};

TgetTop();

};

//stack.cpp

#include"StdAfx.h"

#include".\cstack.h"

#using

template

CCStack:

:

CCStack(void)

:

top(NULL)

{

}

template

CCStack:

:

~CCStack(void)

{

}

template

voidCCStack:

:

Push(constT&x)

{

top=newLinkNode(x,top);

}

template

boolCCStack:

:

Pop()

{

if(IsEmpty()==true)returnfalse;

LinkNode*p=top;

top=top->link;

deletep;

returntrue;

}

template

TCCStack:

:

getTop()

{

Tx;

if(IsEmpty()==true)returnNULL;

x=top->data;

returnx;

}

//main.cpp

#include"stdafx.h"

#include".\cstack.cpp"

#using

usingnamespaceSystem;

int_tmain()

{

stringa;

booltemp=true;

inti=0;

CCStackst;

cin>>a;

while(a[i]!

='\0')

{

st.Push(a[i]);

i++;

}

i=0;

while(a[i]!

='\0')

{

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

{

st.Pop();

i++;

}

else

{

temp=false;

break;

}

}

if(temp)

cout<<"此字符是回文"<

else

cout<<"此字符不是回文"<

return0;

}

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

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

//stdafx.h

#include

#include

#include

usingnamespacestd;

//main.cpp

#include"stdafx.h"

#using

usingnamespaceSystem;

int_tmain()

{

stringa;

intk,*num;

char*A;

cin>>a;

k=a.length();

A=newchar[k];

num=newint[k];

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

for(inti=0;i

{

cout<

"<

}

delete[]A;

delete[]num;

return0;

}

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

{

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

A[0]=s[0];C[0]=1;

//种类数

k=1;

for(i=1;i

C[i]=0;

for(i=1;i

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

当前位置:首页 > 工程科技 > 能源化工

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

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