数据结构矩阵课后题.docx
《数据结构矩阵课后题.docx》由会员分享,可在线阅读,更多相关《数据结构矩阵课后题.docx(35页珍藏版)》请在冰豆网上搜索。
![数据结构矩阵课后题.docx](https://file1.bdocx.com/fileroot1/2023-7/9/1d996817-726e-4db8-a806-3611a4273ef5/1d996817-726e-4db8-a806-3611a4273ef51.gif)
数据结构矩阵课后题
P-219-29T
template
T**LowerMatrix:
:
operator*(constLowerMatrix&m)const{
if(n!
=m.n)throwSizeMismatch();
T**w=newT*[n];
for(inti=0;iw[i]=newint[n];
}
intct=0,cm=0;
for(inti=1;i<=n;i++){
for(intj=1;j<=n;j++){
Tsum=t[ct]*m.t[cm];
for(intk=j;k
ct++;
cm++;
sum+=t[ct]*m.t[cm];
}
w[i-1][j-1]=sum;
if(iw[i-1][j-1]=0;
}
ct=i*(i-1)/2+j-1;
cm=j;
}
ct=i*(i+1)/2;
cm=0;
}
returnw;
}
函数时间复杂性为O(n^3);
P-219-30T
template
T**UpperMatrix:
:
operator*(constLowerMatrix&m)const{
intfront=0;
if(n!
=m.n)throwSizeMismatch();
T**c=newT*[n];
for(inti=0;ic[i]=newint[n];
}
intct=0,cm=0;
for(inti=0;ifor(intj=0;jc[i][j]=0;
if(i<=j)front=j;
elsefront=i;
for(intk=front;kct=i*(2*n-i)/2+k-i;
cm=k*(k+1)/2+j;
c[i][j]+=t[ct]*m.t[cm];
}
}
}
returnc;
}
函数时间复杂性为O(n^3);
P-237-36T
template
SparseMatrix&SparseMatrix:
:
Store(constint&x,inti,intj){
if(i<1||j<1||i>rows||j>cols)throwOutOfBounds();
if(terms==0){
if(x==0)
return*this;
a[0].row=i;
a[0].col=j;
a[0].value=x;
terms++;
return*this;
}
intlocation=a[0].row*cols+a[0].col;
intother=i*cols+j;
intk=0;
while(klocation){
k++;
if(k!
=terms)
location=a[k].row*cols+a[k].col;
}
if(k==terms){
if(terms==MaxTerms)throwOutOfBounds();
a[k].row=i;
a[k].col=j;
a[k].value=x;
terms++;
}
if(other==location){
a[k].row=i;
a[k].col=j;
a[k].value=x;
}
else{
if(terms==MaxTerms)throwOutOfBounds();
for(intl=k;la[l+1]=a[l];
a[k].row=i;
a[k].col=j;
a[k].value=x;
terms++;
}
return*this;
}
template
TSparseMatrix:
:
Retrieve(inti,intj)const{
if(i<1||j<1||i>rows||j>cols)throwOutOfBounds();
intlocation=a[0].row*cols+a[0].col;
intother=i*cols+j;
intk=0;
while(klocation){
k++;
if(k!
=terms)
location=a[k].row*cols+a[k].col;
}
if(other==location)
returna[k].value;
else
return0;
}
Store函数和Retrieve函数的时间复杂性均为O(terms)
P-237-43
template
SparseMatrix&SparseMatrix:
:
Multiply(SparseMatrix&m,SparseMatrix&n){
if(cols!
=m.rows)throwSizeMismatch();
n.rows=rows;
n.cols=m.cols;
n.terms=0;
int*RowNext,RowSize;
RowSize=newint[m.rows+1];
for(inti=1;i<=m.rows;i++){
RowSize[i]=0;
}
for(inti=0;i<=m.rows;i++)
RowSize[m.a[i].row]++;
RowNext[1]=0;
for(inti=2;i<=m.rows;i++)
RowNext[i]=RowNext[i-1]+RowSize[i-1];
int**t=newint[rows];
for(inti=0;it[i]=newint[m.cols];
}
for(inti=0;ifor(intj=0;jt[i][j]=0;
}
}
for(inti=0;ifor(intj=RowNext[a[i].col];jt[a[i].row][a[j].col]+=a[i].value*m.a[j].value;
}
for(inti=0;ifor(intj=0;jif(t[i][j]){
Termb;
b.row=i;
b.col=j;
b.value=t[i][j];
Append(b);
}
}
}
}
P-247-1T
(1)
template
TStack:
:
Length()const{
returntop+1;
}
(2)
template
voidStack:
:
Input(){
cout<<"PleaseEnterthelengthofthestack"<intlen;
cin>>len;
if(len<0||len-1>MaxTop)throwBadInitializers();
for(inti=0;i{
cin>>stack[i];
top++;
}
}
(3)
template
voidStack:
:
Output()const{
for(inti=0;i<=top;i++){
cout<}
cout<}
P-247-2T
(1)
template
voidStack:
:
Split(Stack&a,Stack&b){
if(top/2>a.MaxTop)throwNoMem();
if(top/2-1>b.MaxTop)throwNoMem();
for(inti=0;i<(top+2)/2;i++)
a.stack[i]=stack[i];
a.top=(top+2)/2-1;
for(inti=0;ib.stack[i]=stack[(top+2)/2+i];
b.top=top/2-1;
}
(2)
template
voidStack:
:
Merge(Stack&a){
if(a.top+top+1>MaxTop)throwOutOfBounds();
for(inti=0;i<=top;i++)
a.stack[a.top+i+1]=stack[i];
a.top+=top+1;
top=-1;
}
P-290-1T
(1)
intLength()const{
if(IsEmpty())throwOutOfBounds();
return(rear+MaxSize-front)%MaxSize;
}
P-291-4T
#include"except.h"
template
classQueue{
public:
Queue(intMaxQueueSize=10);
~Queue(){delete[]queue;}
boolIsEmpty()const{
returnfront==rear&&LastOp==0;}
boolIsFull()const{
returnrear==front&&LastOp==1);
}
intLength()const;
TFirst()const;
TLast()const;
Queue&Add(constT&x);
Queue&Delete(T&x);
private:
intfront;
intrear;
intLastOp;
intMaxSize;
T*queue;
}
template
Queue:
:
Queue(intMaxQueueSize){
MaxSize=MaxQueueSize;
queue=newT[MaxSize];
front=rear=0;
intLastOp=0;//0为空,1为满
}
template
intQueue:
:
Length()const{
inta=rear+MaxSize-front;
if(rear==front){
if(lastop==0)
returnMaxSize;
else
return0;
}
returna%maxsize;
}
template
TQueue:
:
First()const{
if(IsEmpty())throwOutOfBounds();
returnqueue[front];
}
template
TQueue:
:
Last()const{
if(IsEmpty())throwOutOfBounds();
returnqueue[rear];
}
template
Queue&Queue:
:
Add(constT&x){
if(IsFull())throwNoMem();
rear=(rear+1)%MaxSize;
queue[rear]=x;
LastOp=1;
return*this;
}
template
boolQueue:
:
Delete(T&x){
if(IsEmpty())throwBadInitializers();
front=(front+1)%MaxSize;
x=queue[front];
LastOp=0;
returntrue;
}
P-297-7T
#include"except.h"
template
classNode{
friendLinkedQueue;
private:
Tdata;
Node*link;
};
template
classLinkedQueue{
public:
LinkedQueue(){front=rear=0;}
~LinkedQueue(){};
boolIsEmpty()const{
return((front)?
false:
true;)
}
boolIsFull()const;
TFirst()const;
TLast()const;
intLength()const;
voidInput();
voidOutput();
LinkedQueue&Add(constT&x);
LinkedQueue&Delete(T&x);
private:
Node*front;
Node*rear;
};
template
LinkedQueue:
:
~LinkedQueue(){
Node*next;
while(front){
next=front->link;
deletefront;
front=next;
}
}
template
boolLinkedQueue:
:
IsFull()const{
Node*p;
try{p=newNode;
deletep;
returnfalse;
}
catch(NoMem){returntrue;}
}
template
intLinkedQueue:
:
Length()const{
Node*p=front;
inti=0;
for(;p;){
i++;
p=p->link;
}
returni;
}
template
voidLinkedQueue:
:
Input(){
Node*p;
intlen;
cout<<"PleaseenterthelengthoftheQueue:
"<>len;
front=rear=0;
for(inti=0;ip=newNode;
cin>>p->data;
p->link=0;
if(i)
rear->link=p;
else
front=p;
rear=p;
}
}
template
voidLinkedQueue:
:
Output(){
Node*p=front;
for(;p;)
{
cout<data<<"";
p=p->link;
}
cout<}
template
TLinkedQueue:
:
First()const{
if(IsEmpty)throwOutOfBounds();
returnfront->data;
}
template
TLinkedQueue:
:
Last()const{
if(IsEmpty)throwOutOfBounds();
returnrear->data;
}
template
LinkedQueue&LinkedQueue:
:
Add(constT&x){
Node*p=newNode;
p->data=x;
p->link=0;
if(front)rear->link=p;
elsefront=p;
rear=p;
return*this;
}
template
LinkedQueue&LinkedQueue:
:
Delete(T&x){
if(IsEmpty)throwOutOfBounds();
x=front->data;
Node*p=front;
front=front->link;
deletep;
return*this;
}
P-324-17T
可以把堆栈换成队列,但换成队列后效率会降低,时间复杂性增高。
P-331-1T
template
classSortedListNode{
templatefriendclassSortedList;
private:
Eele;
Kkey;
};
template
classSortedList{
public:
SortedList(intMaxListSize=10);
~SortedList(){delete[]element;}
boolIsEmpty()const{returnlength==0;}
boolIsFull()const{returnlength==MaxSize;}
intLength()const{returnlength;}
boolSearch(constK&k,E&e)const;
SortedList&Delete(constK&k,E&e);
SortedList&Insert(constK&k,constE&e);
SortedList&DistinctInsert(constK&k,constE&e);
private:
intlength;
intMaxSize;
SortedListNode*element;
};
classNoMem{
public:
NoMem(){};
};
classBadInput{
public:
BadInput(){};
};
template
SortedList:
:
SortedList(intMaxListSize)
{
MaxSize=MaxListSize;
element=newSortedListNode[MaxListSize];
length=0;
}
template
boolSortedList:
:
Search(constK&k,E&e)const
{
inta=0;
while((aa++;
}
if((element[a].key==k)){
e=element[a].ele;
returntrue;
}
returnfalse;
}
template
SortedList&SortedList:
:
Delete(constK&k,E&e)
{inta=0;
while((aa++;
}
if((element[a].key==k))
{
e=element[a].ele;
for(inti=a;ielement[i]=element[i+1];
length--;
return*this;
}
throwBadInput();
}
template
SortedList&SortedList:
:
Insert(constK&k,constE&e)
{
if(IsFull())throwNoMem();
inta=0;
while((aa++;
}
for(inti=length-1;i>=a;i--)
element[i+1]=element[i];
length++;
element[a].key=k;
element[a].ele=e;
return*this;
}
template
SortedList&SortedList:
:
DistinctInsert(constK&k,constE&e)
{
if(IsFull())throwNoMem();
inta=0;
while((aa++;
}
if((element[a].key==k))throwBadInput