数据结构--数组--稀疏矩阵的建立
2012-07-31 15:38
531 查看
稀疏矩阵的建立
稀疏矩阵的删除
#include "stdio.h" #define MAX_SIZE 50 /* 最大的稀疏矩阵 */ typedef enum {head, entry} tagfield; //行链表的头指针和列链表的头指针 struct entry_node { //非零元节点 int row; //非零元所在行的下标 int col; //非零元所在列的下标 int value; //非零元的数值 }; typedef struct Matrix { //矩阵 struct Matrix * down; //非零元所在列表的后继区域 struct Matrix * right; //非零元所在行表的后继区域 tagfield tag; //行列头指针 union { struct Matrix * next; struct entry_node entry; } u; }matrix_node; typedef matrix_node * matrix_pointer; matrix_pointer hdnode [ MAX_SIZE ]; matrix_pointer new_node(void) //定义新node,并分配存储空间 { matrix_pointer temp; temp = (matrix_pointer)malloc(sizeof(matrix_node)); if (temp==NULL) { printf("The memory is full\n"); exit(1); } return temp; } matrix_pointer Create(void) { int num_rows, num_cols, num_terms, num_heads, i,current_row; int col,value,row; matrix_pointer node,temp,last; printf("Enter the number of rows, columns and number of nonzero terms: "); scanf("%d%d%d",&num_rows,&num_cols,&num_terms); num_heads = (num_cols > num_rows) ? num_cols :num_rows; /* 建立新结点 */ node = new_node(); node->tag = entry; node->u.entry.row = num_rows; node->u.entry.col = num_cols; if ( !num_heads ) node->right = node; else { for ( i = 0; i<num_heads; i++ ) { temp = new_node(); hdnode[i] = temp; hdnode[i]->tag = head; hdnode[i]->right = temp; hdnode[i]->u.next = temp; } current_row = 0; last = hdnode[0]; for ( i = 0; i < num_terms; i++ ) { printf("Enter row, column and value: "); scanf("%d%d%d",&row,&col,&value); if ( row > current_row ) { /* 转到row所在行去*/ last->right = hdnode[current_row]; current_row = row; last = hdnode[row]; } temp = new_node(); temp->tag = entry; temp->u.entry.row = row; temp->u.entry.col = col; temp->u.entry.value = value; last->right = temp; last = temp; /*链接到列结点上 */ hdnode[col]->u.next->down = temp; hdnode[col]->u.next = temp; } /* 结束上一行结点 */ last->right = hdnode[current_row]; /*结束所有行结点*/ for ( i=0; i<num_cols; i++ ) hdnode[i]->u.next->down= hdnode[i]; /*链接所有的头结点 */ for ( i=0; i<num_heads-1; i++ ) hdnode[i]->u.next = hdnode[i+1]; hdnode[num_heads-1]->u.next = node; node->right = hdnode[0]; } return node; } void main() { matrix_pointer matric = Create(); }
稀疏矩阵的删除
#include "stdio.h" #define MAX_SIZE 50 /* 最大的稀疏矩阵 */ typedef enum {head, entry} tagfield; struct entry_node { int row; int col; int value; }; typedef struct Matrix { struct Matrix * down; struct Matrix * right; tagfield tag; union { struct Matrix * next; struct entry_node entry; } u; }matrix_node; typedef matrix_node * matrix_pointer; matrix_pointer hdnode [ MAX_SIZE ]; matrix_pointer new_node(void) { matrix_pointer temp; temp = (matrix_pointer)malloc(sizeof(matrix_node)); if (temp==NULL) { printf("The memory is full\n"); exit(1); } return temp; } matrix_pointer Create(void) { int num_rows, num_cols, num_terms, num_heads, i,current_row; int col, value,row; matrix_pointer node,temp,last; printf("Enter the number of rows, columns and number of nonzero terms: "); scanf("%d%d%d",&num_rows,&num_cols,&num_terms); num_heads = (num_cols > num_rows) ? num_cols :num_rows; /* 建立新结点 */ node = new_node(); node->tag = entry; node->u.entry.row = num_rows; node->u.entry.col = num_cols; if ( !num_heads ) node->right = node; else { /*初始化头结点如图5-5-4*/ for ( i = 0; i<num_heads; i++ ) { temp = new_node(); hdnode[i] = temp; hdnode[i]->tag = head; hdnode[i]->right = temp; hdnode[i]->u.next = temp; } current_row = 0; last = hdnode[0]; for ( i = 0; i < num_terms; i++ ) { printf("Enter row, column and value: "); scanf("%d%d%d",&row,&col,&value); if ( row > current_row ) { /* 转到row所在行去*/ last->right = hdnode[current_row]; current_row = row; last = hdnode[row]; } temp = new_node(); temp->tag = entry; temp->u.entry.row = row; temp->u.entry.col = col; temp->u.entry.value = value; /* 链接到行结点上如图5-5-5所示 */ last->right = temp; last = temp; /*链接到列结点上 */ hdnode[col]->u.next->down = temp; hdnode[col]->u.next = temp; } /* 结束上一行结点 */ last->right = hdnode[current_row]; /*结束所有行结点*/ for ( i=0; i<num_cols; i++ ) hdnode[i]->u.next->down= hdnode[i]; /*链接所有的头结点 */ for ( i=0; i<num_heads-1; i++ ) hdnode[i]->u.next = hdnode[i+1]; hdnode[num_heads-1]->u.next = node; node->right = hdnode[0]; } return node; } void erase(matrix_pointer *node) { matrix_pointer x,y,head = (*node)->right; int i; /* 遍历每行,删除元素结点和头结点 */ for ( i = 0; i< (*node)->u.entry.row; i++ ) { y = head->right; while ( y != head ) { x = y; y = y->right; free(x); } x = head; head = head->u.next; free(x); } /* 删除剩余的头结点*/ y = head; while ( y != *node ) { x = y; y = y->u.next; free(x); } free(*node); *node = NULL; } void main() { matrix_pointer matric=Create(); erase(&matric); }
相关文章推荐
- 数据结构学习 第一章数组和稀疏矩阵
- c语言版数据结构(奇迹冬瓜)-数组和广义表(稀疏矩阵的转置算法一)
- 数据结构 数组操作 稀疏矩阵采用三元组存储,设计算法求两个矩阵的和。
- 2015年大二上-数据结构-数组与广义表(3)-2.稀疏矩阵的三元组表示
- 数据结构_数组与广义表_矩阵的十字链表存储稀疏矩阵相加
- 第九周 数据结构实践项目——数组和广义表【项目3.2-- 稀疏矩阵相加】
- 第九周 数据结构实践项目——数组和广义表【项目3.1-稀疏矩阵的三元组表示的实现及应用】
- c语言版数据结构(奇迹冬瓜)-数组和广义表(十字链表存贮稀疏矩阵的行列值)
- 5-3-行逻辑链接的顺序表(稀疏矩阵)-数组和广义表-第5章-《数据结构》课本源码-严蔚敏吴伟民版
- 数据结构 稀疏矩阵 重写函数fast_transpose,使其不用两个数组而仅用一个数组存放row_terms和starting_pos
- 数据结构2-数组和稀疏矩阵
- 数据结构Java实现——④数组——>稀疏矩阵三元组顺序存储
- 数据结构-数组-稀疏矩阵三元组
- 5-2-三元组顺序表(稀疏矩阵)-数组和广义表-第5章-《数据结构》课本源码-严蔚敏吴伟民版
- 5-4-十字链表(稀疏矩阵)-数组和广义表-第5章-《数据结构》课本源码-严蔚敏吴伟民版
- c语言版数据结构(奇迹冬瓜)-数组和广义表(稀疏矩阵的乘法)
- 2015年大二上-数据结构-数组与广义表(3)-1.稀疏矩阵的三元组表示
- 数据结构Java实现——④数组—>稀疏矩阵三元组顺序存储-->矩阵的倒置
- 数据结构学习笔记——数组和稀疏矩阵
- 数据结构 【实验6 数组的建立和使用】