用链表实现一个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.
//============================================================================
// Name : Sicily.cpp
// Author : Reid Chan
// Version :
// Copyright : Your copyright notice
//============================================================================
#include
#include
#include
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[n];
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;
}