데이터 구조 -- 배열 - 희소 행렬 의 구축
#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
");
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
");
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);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.