十字链表的实现
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct OList{
int row, col, data;
//当前行列及数据
OList *next_row, *next_col;
} OList, *OLpointer;
typedef struct OLhead{
OLpointer row_head, col_head;
//row_head,col_head行列链的头
int row_max, col_max, data_num;
//row_max, col_max最大行列下标 data_num非零元数量
// 创建一个row_create_size大小的行头和一个col_create_size大小的列头
void create(const int row_create_size, const int col_create_size) {
row_head = (OLpointer) malloc (row_create_size * sizeof(OList));
// 行头默认指向空地址
for(int i = 0; i < row_create_size; i++) {
row_head[i].col = -1;
row_head[i].next_col = NULL;
}
col_head = (OLpointer) malloc (col_create_size * sizeof(OList));
// 列头默认指向空地址
for(int i = 0; i < col_create_size; i++) {
col_head[i].row = -1;
col_head[i].next_row = NULL;
}
row_max = row_create_size;
col_max = col_create_size;
// 初始数据全为零元素
data_num = 0;
}
// 再增加increase_size行
void row_increase(const int increase_size) {
int i = row_max;
row_head = (OLpointer) realloc (row_head, (row_max+increase_size) * sizeof(OList));
row_max += increase_size;
// 新增行头默认指向空地址
for(; i < row_max; i++) {
row_head[i].col = -1;
row_head[i].next_col = NULL;
}
}
// 行增加increase_size列
void col_increase(const int increase_size) {
int i = col_max;
col_head = (OLpointer) realloc (col_head, (col_max+increase_size) * sizeof(OList));
col_max += increase_size;
// 新增列头默认指向空地址
for(; i < col_max; i++) {
col_head[i].row = -1;
col_head[i].next_row = NULL;
}
}
void insert(const int row, const int col, const int data) {
//数据为零
if(!data) {
if(row > row_max || col > col_max) return;
OLpointer row_pointer = &row_head[row-1];
while(row_pointer->next_col && row_pointer->next_col->col < col) row_pointer = row_pointer->next_col;
if(!row_pointer->next_col || row_pointer->next_col->col != col) return;
//数据位置为非零,需要删除
OLpointer zero_data = row_pointer->next_col;
OLpointer col_pointer = &col_head[col-1];
while(col_pointer->next_row && col_pointer->next_row->row < row) col_pointer = col_pointer->next_row;
row_pointer->next_col = zero_data->next_col;
col_pointer->next_row = zero_data->next_row;
free(zero_data); //释放空间
data_num--; //非零数据--
return;
}
//数据非零
if(row > row_max) row_increase(row-row_max); //数据所在行超过最大行
OLpointer row_pointer = &row_head[row-1];
while(row_pointer->next_col && row_pointer->next_col->col <= col) row_pointer = row_pointer->next_col;
if(row_pointer->col == col) {
row_pointer->data = data;
return;
}
OLpointer new_data = (OLpointer) malloc (sizeof(OList));
new_data->row = row;
new_data->col = col;
new_data->data = data;
new_data->next_col = row_pointer->next_col;
row_pointer->next_col = new_data;
if(col > col_max) col_increase(col-col_max); //数据所在列超过最大列
OLpointer col_pointer = &col_head[col-1];
while(col_pointer->next_row && col_pointer->next_row->row <= row) col_pointer = col_pointer->next_row;
new_data->next_row = col_pointer->next_row;
col_pointer->next_row = new_data;
data_num++; //非零数据++
}
int getdata(const int row, const int col) {
if(row > row_max || col > col_max) return 0;
OLpointer row_pointer = &row_head[row-1];
while(row_pointer->next_col && row_pointer->next_col->col <= col) row_pointer = row_pointer->next_col;
return ((row_pointer->col == col)? row_pointer->data: 0);
}
} OLhead;
int main() {
OLhead head;
head.create(100, 99);
head.insert(1, 1, 3);
head.insert(2, 2, 2);
head.insert(3, 1, 2);
head.insert(1, 4, 5);
cout << head.getdata(1, 1) << endl;
cout << head.getdata(1, 2) << endl;
cout << head.getdata(2, 2) << endl;
head.insert(2, 2, -1);
cout << head.getdata(2, 2) << endl;
head.insert(2, 2, 0);
cout << head.getdata(2, 2) << endl;
return 0;
}