矩陣相加(十字鍊錶)

2021-07-10 17:54:12 字數 1349 閱讀 4048

描述

輸入兩個稀疏矩陣a和b,用十字鍊錶實現a=a+b,輸出它們相加的結果。

輸入第一行輸入四個正整數,分別是兩個矩陣的行m、列n、第乙個矩陣的非零元素的個數t1和第二個矩陣的非零元素的個數t2,接下來的t1+t2行是三元組,分別是第乙個矩陣的資料和第二個矩陣的資料, 三元組的第乙個元素表示行,第二個元素表示列,第三個元素是該元素的值。

輸出輸出相加後的矩陣三元組。

輸入樣例

3 4 3 2

1 1 1

1 3 1

2 2 2

1 2 1

2 2 3

輸出樣例

1 1 1

1 2 1

1 3 1

2 2 5

#include #include #include #include #define elemtype int

using namespace std;

struct olnode

;typedef struct olnode olnode, *olink;

typedef struct

crosslist;

void createlist(crosslist &m, int m, int n, int t)

else

p->down = null;

if(m.chead[j] == null || m.chead[j]->i > i)

else

p->right = null;

}}void destroylist(crosslist &m)

}m.mu = 0;

m.nu = 0;

m.tu = 0;

}void insertnode(crosslist &m, olink &p)

else

//對應結點相加

if(q!=null && q->j == p->j)

else

plused = 1;

}//插入

if(!deleted && !plused)

//else

else

m.tu++;}}

if(m.chead[j] == null)

m.chead[j] = p;

else

if(deleted)

}if(!plused)

else}}

}void addlist(crosslist &m, crosslist &n)

}}void printlist(crosslist &m)}}

//cout << endl;

}int main()

稀疏矩陣相加(十字鍊錶儲存)

標頭檔案 linklist.h inte ce for the linklist class.if defined afx linklist h 0f0cca3e d836 4bb4 9030 b548558fc57e included define afx linklist h 0f0cca3e ...

十字鍊錶 稀疏矩陣

include includetypedef struct olnode olnode,olink typedef struct 行和列煉表頭指標向量基址,由creatsmatrix ol 分配 crosslist 初始化m crosslist型別的變數必須初始化,否則建立 複製矩陣將出錯 bool...

稀疏矩陣的建立 十字鍊錶

一 概念 既然要用鍊錶節點來模擬矩陣中的非零元素,肯定需要如下5個元素 row,col,val,down,right 其中 row 矩陣中的行。col 矩陣中的列。val 矩陣中的值。right 指向右側的乙個非零元素。down 指向下側的乙個非零元素。現在我們知道單個節點該如何表示了,那麼矩陣中同...