编译软件袜友(virtual c++ 6.0)望你能学习进步
#include<iostream.h>
#include<stdlib.h>
#include<iomanip.h>
#define MaxRows 100
#define MaxColumns 100
typedef int ElemType
struct CrossNode
{
int row,col
ElemType val
CrossNode *down,*right
}
struct CLMatrix
{
int m,n,t
CrossNode *rv[MaxRows+1]
CrossNode *cv[MaxColumns+1]
}
void InitMatrix(CLMatrix &M)
{
M.m=0M.n=0M.t=0
for(int i=1i<=MaxRowsi++)
M.rv[i]=NULL
for(i=1i<=MaxColumnsi++)
M.cv[i]=NULL
}
void InputMatrix(CLMatrix &M,int m,int n)
void OutputMatrix(CLMatrix &M,int m,int n)
void Transpose(CLMatrix &M,int m,int n)
void main()
{
CLMatrix Q,P,N
InitMatrix(Q)
InitMatrix(P)
InitMatrix(N)
cout<笑好仿<"请您输入矩阵的行数与列数:"
int m,n
cin>>m>>n
cout<<"请以三元组的形碰纤式进行输入:"<<endl
InputMatrix(Q,m,n)
cout<<"您输入的稀疏矩阵为:"<<endl
OutputMatrix(Q,m,n)
cout<<"转置后的稀疏矩阵为:"<<endl
Transpose(Q,m,n)
}
void InputMatrix(CLMatrix &M,int m,int n)
{
M.m=mM.n=n
int row,col,val
int k=0
cin>>row>>col>>val
while(row!=0)
{
k++
CrossNode *cp,*newptr
newptr=new CrossNode
newptr->row=row
newptr->col=col
newptr->val=val
newptr->down=newptr->right=NULL
cp=M.rv[row]
if(cp==NULL)
M.rv[row]=newptr
else
{
while(cp->right!=NULL)
cp=cp->right
cp->right=newptr
}
cp=M.cv[col]
if(cp==NULL)
M.cv[col]=newptr
else
{
while(cp->down!=NULL)
cp=cp->down
cp->down=newptr
}
cin>>row>>col>>val
}
M.t=k
}
void Transpose(CLMatrix &M,int m,int n)
{
CLMatrix S
InitMatrix(S)
S.m=M.mS.n=M.n
for(int i=1i<=S.mi++)
{
for(int j=1j<=S.nj++)
{
int val=0
CrossNode *cp,*newptr
newptr=new CrossNode
newptr->row=i
newptr->col=j
newptr->val=val
newptr->down=newptr->right=NULL
cp=S.rv[i]
if(cp==NULL)
S.rv[i]=newptr
else
{
while(cp->right!=NULL)
cp=cp->right
cp->right=newptr
}
cp=S.cv[j]
if(cp==NULL)
S.cv[j]=newptr
else
{
while(cp->down!=NULL)
cp=cp->down
cp->down=newptr
}
}
}
for(i=1i<=M.mi++)
{
CrossNode *sp1=S.rv[i]
CrossNode *mp1=M.rv[i]
while(mp1!=NULL)
{
while(sp1->col<mp1->col)
sp1=sp1->right
sp1->val=mp1->val
mp1=mp1->right
}
}
for(int x=1x<=S.mx++)
{
while(S.cv[x]!=NULL)
{
cout<<setw(4)<<S.cv[x]->val
S.cv[x]=S.cv[x]->down
}
cout<<endl
}
}
void OutputMatrix(CLMatrix &M,int m,int n)
{
CLMatrix S
InitMatrix(S)
S.m=M.mS.n=M.n
for(int i=1i<=S.mi++)
{
for(int j=1j<=S.nj++)
{
int val=0
CrossNode *cp,*newptr
newptr=new CrossNode
newptr->row=i
newptr->col=j
newptr->val=val
newptr->down=newptr->right=NULL
cp=S.rv[i]
if(cp==NULL)
S.rv[i]=newptr
else
{
while(cp->right!=NULL)
cp=cp->right
cp->right=newptr
}
cp=S.cv[j]
if(cp==NULL)
S.cv[j]=newptr
else
{
while(cp->down!=NULL)
cp=cp->down
cp->down=newptr
}
}
}
for(i=1i<=M.mi++)
{
CrossNode *sp1=S.rv[i]
CrossNode *mp1=M.rv[i]
while(mp1!=NULL)
{
while(sp1->col<mp1->col)
sp1=sp1->right
sp1->val=mp1->val
mp1=mp1->right
}
}
for(int x=1x<=S.mx++)
{
while(S.rv[x]!=NULL)
{
cout<<setw(4)<<S.rv[x]->val
S.rv[x]=S.rv[x]->right
}
cout<<endl
}
}
//稀疏矩阵的十字链表存储表示#include<stdio.h>
#include<stdlib.h>
typedef int ElemType
typedef struct OLNode
{
int i,j //该非零元的行昌袜坦(i)和列(j)下标
ElemType e//该非零元
struct OLNode *right,*down//该耐桐非零元所在行表和列表的后继链域
}OLNode,*OLink
typedef struct
{
OLink *rhead,*chead//行和列链表头指针向量基址CreateSMartix分配
int mu,nu,tu//稀疏矩阵的行数(mu),列数(nu)和非零元数tu
}CrossList
void CreateSMatrix(CrossList *M)
{
int m,n,t//M行,N列,T非零元
int i,j,mi,mj,e
OLink p,q
printf("please enter matrix's row,column,element:\n")
scanf("%d %d %d",&m,&n,&t)//输入行数,列数,非零元个数
M->mu = mM->nu = nM->tu = t
printf("\n")
if(!(M->rhead=(OLink*)malloc((m+1)*sizeof(OLink)))) exit(1)
if(!(M->chead=(OLink*)malloc((n+1)*sizeof(OLink)))) exit(1)
for(i=0i<=m++i) //初始化行,列头指针,使其为空链表
M->rhead[i] = NULL
for(j=0j<=n++j)
M->chead[j] = NULL
//按任好厅意次序输入非零元
for(scanf("%d %d %d",&mi,&mj,&e)mi!=0scanf("%d %d %d",&mi,&mj,&e))
{
if(!(p = (OLink)malloc(sizeof(OLNode)))) exit(1)
p->i = mip->j = mjp->e = e //赋值
if(M->rhead[mi]==NULL)
{
p->right = M->rhead[mi]
M->rhead[mi] = p
}
else
{
//已存在结点和比结点插入小的情况
for(q=M->rhead[mi]q->right &&q->right->j<jq=q->right)
p->right = q->rightq->right = p
}//完成行插入
if(M->chead[mj]==NULL )
{
p->down = M->chead[mj]
M->chead[mj] = p
}
else
{
for(q=M->chead[mj]q->down &&q->down->i<iq=q->down)
p->down = q->downq->down = p
}//完成列插入
}// for
}
void AddSMatrix(CrossList *M,CrossList *N)
{
//M与N矩阵相加结果到M中
OLink pa,pb,hj[10],pre,p//pa,pb指向矩阵M,N,hj,pre作为前驱指针,p是临时结点
int i,j
pre = NULL
for(j=1j<=M->nu++j) //hj取得M每列首地址
hj[j] = M->chead[j]
for(i=1i<=M->mu++i)
{
pa = M->rhead[i]pb = N->rhead[i] //分别指向行首地址
pre = pa
while(pb)
{
//处理行的情况
if(pa==NULL || pa->j >pb->j) //pa的列比pb大时
{
p = (OLink)malloc(sizeof(OLNode))
p->i = pb->ip->j = pb->jp->e = pb->e//p得到N上的值
if(M->rhead[p->i]==NULL)
{
M->rhead[p->i] = pre = pp->right = NULL
}
else
pre->right = p//pa的前驱结点指向新结点p
p->right = pa //新结点指向pa
pre = p
//处理列
if(M->chead[p->j]==NULL) //列首地址为空
{
hj[p->j] = M->chead[p->j] = pp->down = NULL
}
else //列首地址不为空
{
p->down = hj[p->j]->down
hj[p->j]->down = p
hj[p->j] = p
}//处理列
pb = pb->right
}// else if pa
else if(pa->j <pb->j )//当pa的列比pb小时
{
pre = papa = pa->right //printf("pa==null?in pa->j<pb-j\n")
}
else //列相等的情况
{
pa->e += pb->e
if(pa->e==0) //若相加等于0
{
//处理行
p = pa//得到准备删除结点
if(pa->right) //pa有有后继结点则pre后移
{
pre->right = p->right
pa = pa->right
}
else pa = M->rhead[p->i] = NULL//否则头指针置空
//处理列
if(M->chead[p->j]==p)
M->chead[p->j] = hj[p->j] = p->down
else
{
//hj[p->j]->down = p->down
hj[p->j]->down = p->down
}
free(p)
}
pb = pb->right
}
//处理列的情况
}//while pa
}
}
void PrintSMatrix(CrossList *M)
{
int i
OLink dis
printf("i j e\n")
for(i=1i<=M->mu++i)
{
dis = M->rhead[i]
while(dis)
{
printf("%d %d %d\t",dis->i,dis->j,dis->e)
dis = dis->right
}
printf("\n")
}
}
void TransposePrint(CrossList *M)
{
int i
OLink dis
printf("i j e\n")
for(i=1i<=M->nu++i)
{
dis = M->chead[i]
while(dis)
{
printf("%d %d %d\t",dis->j,dis->i,dis->e)
dis = dis->down
}
printf("\n")
}
}
int main(void)
{
CrossList M,N
CreateSMatrix(&M)
printf("Matrix M:\n")
PrintSMatrix(&M)
TransposePrint(&M)
CreateSMatrix(&N)
printf("Matrix N:\n")
PrintSMatrix(&N)
printf("M + N = :\n")
AddSMatrix(&M,&N)
PrintSMatrix(&M)
TransposePrint(&M)
//PrintSMatrix(&M)
return 0
}
输入
3 4 4
1 1 3
1 4 5
2 2 -1
3 1 2
0 0 0
3 4 4
1 3 4
2 2 7
3 1 -2
3 3 8
0 0 0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)