-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparsematrix.c
More file actions
64 lines (58 loc) · 1.33 KB
/
Copy pathsparsematrix.c
File metadata and controls
64 lines (58 loc) · 1.33 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
int row;
int col;
struct node* next;
};
struct node* insert(struct node *element, int r, int c, int val)
{
struct node *temp;
if(element==NULL)
{
element=(struct node*)malloc(sizeof(struct node));
element->row=r;
element->col=c;
element->data=val;
element->next=NULL;
}
else
{
temp=element;
while(temp->next!=NULL)
temp=temp->next;
temp->row=r;
temp->col=c;
temp->data=val;
temp->next=NULL;
}
return (element);
}
voidprintlist(struct node*element)
{
printf("thr resultant sparse matrix is:");
while(element->next!=NULL)
{
printf("%d%d%d->",element->row, element->col,element->data);
element=element->next;
}
printf("%d%d%d->",element->row, element->col,element->data);
}
int main()
{
int i,n;
printf("enter the number of non zero elements:");
scanf("%d",&n);
int r,c,val;
struct node* element=NULL;
for(i=1;i<=n;i++)
{
printf("enter the row and column and data");
scanf("%d%d%d",&r,&c,&val);
struct node* insert(element,r,c,val);
}
printlist(element);
return 0;
}