forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sparse_Matrix.c
98 lines (77 loc) · 2.51 KB
/
Sparse_Matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Implementation of a Sparse Matrix with Singly Linked List with standard interface methods*/
#include<stdio.h>
#include<stdlib.h> // for malloc
struct Node
{
int value;
int row_position;
int column_postion;
struct Node *next;
}typedef Node;
struct List
{
Node *start ; // pointer to the first Node of the list
}typedef List;
int insert_end( List *list, int val, int r, int c)
{
Node * newNode = (Node*)malloc(sizeof(Node));
if( newNode == NULL)// not sufficient memory in the heap
return 1;// denotes failure to insert node
// assign values to components of Node
newNode->value = val;
newNode->row_position =r;
newNode->column_postion=c;
newNode->next = NULL; // this is going be the last node of list
if( list->start == NULL)// if the list is empty
{
list->start = newNode;
return 0;
}
// list is not empty
Node * temp= list->start;// iterator starts with first node
while(temp->next != NULL)// reach the last node
temp = temp->next;
temp->next = newNode; // last node now points to the new node
return 0;
}//end of insert_end
void display_list(List list)
{
if(list.start == NULL) // No nodes in the list
{
printf("\nEmpty List\n");
return ;
}
Node *temp = list.start;// initialize an iterator with start
while( temp != NULL ) // iterate over all the nodes of the list
{
printf("[%d,%d,%d] ", temp->value, temp->row_position, temp->column_postion); // print the info part of the current node
temp = temp->next;
}//end of while
}//end of display_list
int main()
{
int sparseMatrix[4][5] =
{
{0 , 0 , 3 , 0 , 4 },
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 6 , 0 , 0 }
};
List sparse_mat_list;
sparse_mat_list.start = NULL;
printf("\nSparse Matrix \n");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
printf("%5d", sparseMatrix[i][j]);
printf("\n") ;
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
// Pass only those values which are non - zero
if (sparseMatrix[i][j] != 0)
insert_end(&sparse_mat_list,sparseMatrix[i][j],i,j );
printf("\n List representation of Sparse Matrix [Row,Col,Value] \n");
display_list(sparse_mat_list);
return 0;
}//end of main