CrossList.h #ifndef CROSSLIST_H_H #define OK 1 #define ERROR 0 typedef int ElemType; typedef struct OLNode { ElemType data; int i, j; struct OLNode *right, *down; }OLNode; typedef struct { OLNode *rhead, *chead; int mu, nu, tu; }CrossList; int init_crossList(CrossList*); int make_node(OLNode**); int create_crossList(CrossList*); int print_row_crossList(CrossList*); int print_col_crossList(CrossList*); int find_value(CrossList*, ElemType, int*, int*); int destroy_crossList(CrossList*); int insert_crossList(CrossList*, ElemType, int, int); int delete_crossList(CrossList*, int, int); int add_crossList(CrossList*, CrossList*); #endif CrossList.cpp #include<stdio.h> #include<stdlib.h> #include"CrossList.h" int init_crossList(CrossList *clist) { clist->rhead = clist->chead = NULL; clist->mu = clist->nu = clist->tu = 0; return OK; } int make_node(OLNode **node) { if(!((*node) = (OLNode*)malloc(sizeof(OLNode)))) return ERROR; (*node)->right = (*node)->down = NULL; return OK; } int create_crossList(CrossList *clist) { if(clist->rhead) { free(clist->rhead); clist->rhead = NULL; return ERROR; } if(clist->chead) { free(clist->chead); clist->chead = NULL; return ERROR; } int mu, nu, tu; int i, j, e; OLNode *node = NULL; mu = nu = tu = 0; printf("please input mu,nu,tu: "); scanf("%d,%d,%d", &mu, &nu, &tu); //生成行头和列头指针 if(mu > 0 && nu > 0) { if(!(clist->rhead = (OLNode*)malloc(sizeof(OLNode) * mu))) return ERROR; if(!(clist->chead = (OLNode*)malloc(sizeof(OLNode) * nu))) return ERROR; } clist->mu = mu; clist->nu = nu; clist->tu = tu; //初始化行头和列头指针 for(i = 0 ; i < mu ; i++) { clist->rhead[i].right = NULL; } for(i = 0 ; i < nu ; i++) { clist->chead[i].down = NULL; } //输入数据 while(tu-- > 0) { printf("input i, j, e: "); scanf("%d,%d,%d", &i, &j, &e); //生成新的结点保存元素 if(!make_node(&node)) return ERROR; node->data = e; node->i = i; node->j = j; //搜索元素在行中指定的位置,然后插入链表(操作right指针) if(clist->rhead[i].right == NULL || clist->rhead[i].j > j) { node->right = clist->rhead[i].right; clist->rhead[i].right = node; } else { //因为十字链表本身是有序插入的,所以当 //插入的位置不是在开头的时候,需要从第 //i个位置开始搜索,当搜索到了之后就相当 //于单链表的操作那样插入元素。 OLNode *tmp = NULL; OLNode *tmp1 = NULL; /*for(tmp = clist->rhead[i].right ; tmp && tmp->j < j ; tmp = tmp->right);*/ tmp = &clist->rhead[i]; while(tmp && tmp->j < j) { tmp1 = tmp; tmp = tmp->right; } node->right = tmp1->right; tmp1->right = node; } //搜索元素在列中指定的位置,然后插入链表(操作down指针) if(clist->chead[j].down == NULL || clist->chead[j].i > i) { node->down = clist->chead[j].down; clist->chead[j].down = node; } else { OLNode *tmp = NULL; OLNode *tmp1 = NULL; /*for(tmp = clist->chead[j].down ; tmp && tmp->i < i ; tmp = tmp->down);*/ tmp = &clist->chead[j]; while(tmp && tmp->i < i) { tmp1 = tmp; tmp = tmp->down; } node->down = tmp1->down; tmp1->down = node; } } return OK; } int find_value(CrossList *clist, ElemType value, int *row, int *col) { if(!clist->rhead || !clist->chead) return ERROR; int i = 0; OLNode *tmp = clist->rhead[i].right; OLNode *tmp1 = NULL; while(tmp && i < clist->mu) { tmp1 = tmp; while(tmp1) { if(tmp1->data == value) { *row = tmp1->i; *col = tmp1->j; return OK; } tmp1 = tmp1->right; } tmp = clist->rhead[++i].right; } return OK; } int destroy_crossList(CrossList *clist) { if(!clist->rhead || !clist->chead) return ERROR; int i = 0; OLNode *tmp = clist->rhead[i].right; OLNode *tmp1 = NULL; OLNode *tmp2 = NULL; while(tmp && i < clist->mu) { tmp1 = tmp; while(tmp1) { tmp2 = tmp1; tmp1 = tmp1->right; free(tmp2); } tmp = clist->rhead[++i].right; } free(clist->rhead); free(clist->chead); clist->rhead = clist->chead = NULL; clist->mu = clist->nu = clist->tu = 0; return OK; } int insert_crossList(CrossList *clist, ElemType value, int row, int col) { if(!clist->rhead || !clist->chead) return ERROR; if(row < 0 || row > clist->mu - 1 || col < 0 || col > clist->nu - 1) return ERROR; //搜索一遍链表,看是否有相同位置的元素 //如果出现相同位置,则相加。 OLNode *tmp = NULL; OLNode *tmp1 = NULL; tmp = clist->rhead[row].right; while(tmp) { if(tmp->i == row && tmp->j == col) { tmp->data += value; if(tmp->data == 0) { //元素相加之后,搜索值为0的元素,并删除 delete_crossList(clist, tmp->i, tmp->j); } return OK; } tmp = tmp->right; } OLNode *node = NULL; if(!make_node(&node)) return ERROR; node->data = value; node->i = row; node->j = col; tmp = &clist->rhead[row]; while(tmp && tmp->j < col) { tmp1 = tmp; tmp = tmp->right; } node->right = tmp1->right; tmp1->right = node; tmp = &clist->chead[col]; tmp1 = NULL; while(tmp && tmp->i < row) { tmp1 = tmp; tmp = tmp->down; } node->down = tmp1->down; tmp1->down = node; clist->tu++; return OK; } int delete_crossList(CrossList *clist, int row, int col) { if(!clist->rhead || !clist->chead) return ERROR; OLNode *tmp = NULL; OLNode *tmp1 = NULL; OLNode *tmp2 = NULL; OLNode *t = NULL; OLNode *t1 = NULL; OLNode *t2 = NULL; //删除一个结点需要修改行结点指针和列结点指针 tmp = &clist->rhead[row]; t = &clist->chead[col]; //行遍历元素 while(tmp) { tmp1 = tmp; tmp = tmp->right; if(tmp && tmp->i == row && tmp->j == col) { tmp2 = tmp; tmp1->right = tmp2->right; break; } } //列遍历元素 while(t) { t1 = t; t = t->down; if(t && t->i == row && t->j == col) { t2 = t; t1->down = t2->down; break; } } free(tmp2); clist->tu--; return OK; } //相加后的矩阵为clist int add_crossList(CrossList *clist, CrossList *clist1) { if(!clist->rhead || !clist->chead || !clist1->rhead || !clist1->chead) return ERROR; if(clist->mu != clist1->mu && clist->nu != clist1->nu) return ERROR; OLNode *tmp = NULL; OLNode *tmp1 = NULL; int i = 0; tmp = clist1->rhead[i].right; //遍历元素进行相加 while(i < clist1->mu) { tmp1 = tmp; while(tmp1) { insert_crossList(clist, tmp1->data, tmp1->i, tmp1->j); tmp1 = tmp1->right; } tmp = clist1->rhead[++i].right; } return OK; } //行打印 int print_row_crossList(CrossList *clist) { if(!clist->rhead || !clist->chead) return ERROR; int i; OLNode *tmp = clist->rhead[0].right; OLNode *tmp1 = NULL; i = 0; while(i < clist->mu) { tmp1 = tmp; if(!tmp1) { tmp = clist->rhead[++i].right; continue; } while(tmp1) { printf("%5d", tmp1->data); tmp1 = tmp1->right; } printf("/n"); tmp = clist->rhead[++i].right; } return OK; } //列打印 int print_col_crossList(CrossList *clist) { if(!clist->rhead || !clist->chead) return ERROR; OLNode *tmp; OLNode *tmp1; int j = 0; tmp = clist->chead[j].down; while(j < clist->nu) { tmp1 = tmp; if(!tmp1) { tmp = clist->chead[++j].down; continue; } while(tmp1) { printf("%5d", tmp1->data); tmp1 = tmp1->down; } printf("/n"); tmp = clist->chead[++j].down; } return OK; }