十字链表的实现

十字链表的实现

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值