Program work 5. 用链表实现矩阵及实现矩阵转置
2014-11-16 17:02
260 查看
用链表实现一个2维方阵
用一个指针数组记录每一行的头节点. 每一行建一条长度为n的链表, n行链表就构成一个了二维矩阵.
实现矩阵的转置
在对角线上的元素无需进行转置, 所以只要扫面矩阵的上三角, 然后将其跟对应的下三角的元素进行交换即可,
Program environment:
Operation System: Ubuntu 14.04,
Ide: Eclipse,
Language: c++
Compiler: g++;
Method:
As we all know, there is no index when using link list. Therefore, to simplify some work, i use a pointer array to store the header pointer of each row, and a number as counter to keep track the “index”.
About transpose:The diagonal (row index == column index) divide the matrix into two triangle. One is upper triangle, the other is lower triangle.Therefore, we only need to scan the data of upper triangle, and swap the symmetrical data of the lower triangle.
Specifically: A. Define a index j for row tracker, i for column tracker, and begin scanning from header[0], for each traversal i++. Read the node’s data for each i > j and ready for swap.
B. Before swap, we need to locate the symmetrical data. It’s obviously that the i is the row number of the symmetrical data, and j is the column number of the symmetrical data. Thus, we can use Header[i] to locate the row, the then do the row traversal j times to reach that data.
C. Do the swap, and back to the process A, and do the next i > j, then repeat B, C.
Input & Output:
Input:
the size of a square matrix: n,
then following n * n numbers which are the data of the matrix.
Output:
Print out the matrix before transpose and after transpose.
用一个指针数组记录每一行的头节点. 每一行建一条长度为n的链表, n行链表就构成一个了二维矩阵.
实现矩阵的转置
在对角线上的元素无需进行转置, 所以只要扫面矩阵的上三角, 然后将其跟对应的下三角的元素进行交换即可,
Program environment:
Operation System: Ubuntu 14.04,
Ide: Eclipse,
Language: c++
Compiler: g++;
Method:
As we all know, there is no index when using link list. Therefore, to simplify some work, i use a pointer array to store the header pointer of each row, and a number as counter to keep track the “index”.
About transpose:The diagonal (row index == column index) divide the matrix into two triangle. One is upper triangle, the other is lower triangle.Therefore, we only need to scan the data of upper triangle, and swap the symmetrical data of the lower triangle.
Specifically: A. Define a index j for row tracker, i for column tracker, and begin scanning from header[0], for each traversal i++. Read the node’s data for each i > j and ready for swap.
B. Before swap, we need to locate the symmetrical data. It’s obviously that the i is the row number of the symmetrical data, and j is the column number of the symmetrical data. Thus, we can use Header[i] to locate the row, the then do the row traversal j times to reach that data.
C. Do the swap, and back to the process A, and do the next i > j, then repeat B, C.
Input & Output:
Input:
the size of a square matrix: n,
then following n * n numbers which are the data of the matrix.
Output:
Print out the matrix before transpose and after transpose.
//============================================================================ // Name : Sicily.cpp // Author : Reid Chan // Version : // Copyright : Your copyright notice //============================================================================ #include <iostream> #include <iomanip> #include <cctype> using namespace std; struct Element { int data; Element *next; }; void create_row(Element *header, int sz) { Element *last; last = header; for (int i = 0; i < sz; ++i) { Element *current = new Element; cin >> current->data; last->next = current; last = current; last->next = NULL; } } void transpose(Element *header[], int sz) { Element *trv1, *trv2; for (int i = 0; i < sz; ++i) { trv1 = header[i]->next; int j = 0, tmp; while (trv1 != NULL) { if (j > i) { trv2 = header[j]->next; int k = 0; while (k != i) { trv2 = trv2->next; k++; } tmp = trv1->data; trv1->data = trv2->data; trv2->data = tmp; } j++; trv1 = trv1->next; } } } void free_memory(Element *header[], int sz) { Element *t1, *t2; for (int i = 0; i < sz; ++i) { t1 = header[i]->next; while (t1 != NULL) { t2 = t1; t1 = t1->next; delete t2; } delete header[i]; } cout << "Memory free~" << endl << endl; } void print_out(Element *header[], int sz) { Element *out; for (int i = 0; i < sz; ++i) { out = header[i]->next; while (out != NULL) { cout << setw(4) << out->data << ' '; out = out->next; } cout << endl; } } int main() { while (true) { int n; cout << "Input matrix size (n*n): "; cin >> n; cout << "Input element:" << endl; Element *header ; for (int k = 0; k < n; ++k) { header[k] = new Element; create_row(header[k], n); // create a row with a link list } cout << "Before transpose:" << endl; print_out(header, n); // for matrix print out cout << "After transpose:" << endl; transpose(header, n); // transpose a matrix print_out(header, n); cout << "If continue(y/n): "; char ch; cin >> ch; if (tolower(ch) == 'n') { break; } free_memory(header, n); // free memory. } return 0; }
相关文章推荐
- 稀疏矩阵的压缩存储及转置,快速转置法,C++代码实现
- 数据结构稀疏矩阵的实现及转置
- C语言 动态生成矩阵,并实现其录入转置
- 快速排序实现稀疏矩阵的转置
- 数据结构与实现——数组、矩阵、链表、队列、栈、对象、二叉树和红黑树
- MMX实现的矩阵转置操作
- 三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述)
- 双线程实现超大型3000*3000矩阵的转置
- 利用C++实现矩阵的相加/相称/转置/求鞍点
- 稀疏矩阵的十字链表实现:行列链表
- C/C++实现矩阵的转置
- 指针实现矩阵转置
- 链表转置实现
- java和js实现普通矩阵和稀疏矩阵(非满矩阵)的转置
- C++实现矩阵的相加/相称/转置/求鞍点
- c语言实现矩阵的三元组表示 + 矩阵的转置
- c#实现矩阵的转置,相乘等
- C语言学习之用二维数组实现矩阵转置
- 第 2 章 第 7 题 矩阵的转置问题 排序法实现
- C++实现矩阵原地转置算法