-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoSparseMatrix.c
More file actions
140 lines (125 loc) · 2.93 KB
/
AddTwoSparseMatrix.c
File metadata and controls
140 lines (125 loc) · 2.93 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// ADD TWO SPARSE MATRIX
#include <stdio.h>
#include <stdlib.h>
struct node
{
int r;
int c;
int v;
struct node *next;
};
//----------------CREATE--------------------------
void create(struct node **h)
{
struct node *cur, *ptr;
cur = (struct node *)malloc(sizeof(struct node));
cur->next = NULL;
printf("Enter the no. of row:-");
scanf("%d", &cur->r);
printf("Enter the no. of col:-");
scanf("%d", &cur->c);
printf("Enter the no. of non zero elements:-");
scanf("%d", &cur->v);
*h = ptr = cur;
for (int i = 1; i <= (*h)->v; i++)
{
cur = (struct node *)malloc(sizeof(struct node));
cur->next = NULL;
printf("Enter the row no.:-");
scanf("%d", &cur->r);
printf("Enter the col no.:-");
scanf("%d", &cur->c);
printf("Enter the value:-");
scanf("%d", &cur->v);
ptr->next = cur;
ptr = cur;
}
}
//--------------------------DISPLAY-------------------------------
void display(struct node *h)
{
while (h != NULL)
{
printf("%d\t%d\t%d\n", h->r, h->c, h->v);
h = h->next;
}
}
//---------ADDITION------------------------------------------
void add(struct node *h)
{
struct node *ptr = h->next, *ptr1, *prv;
while (ptr != NULL)
{
prv = ptr;
ptr1 = ptr->next;
while (ptr1 != NULL)
{
if ((ptr->c == ptr1->c) && (ptr->r == ptr1->r))
{
hz->v--;
ptr->v += ptr1->v;
prv->next = ptr1->next;
free(ptr1);
ptr1 = prv;
}
prv = ptr1;
ptr1 = ptr1->next;
}
ptr = ptr->next;
}
}
//-----------------------JOIN FUNCTION----------------
void join(struct node *h1, struct node *h2, struct node **h)
{
if (h1->c != h2->c || h1->r != h2->r)
{
printf("The matrix are not same\n");
return;
}
*h = NULL;
h1->v += h2->v;
struct node *cur, *ptr;
while (h1 != NULL)
{
cur = (struct node *)malloc(sizeof(struct node));
*cur = *h1;
cur->next = NULL;
if (*h == NULL)
{
*h = ptr = cur;
}
else
{
ptr->next = cur;
ptr = cur;
}
h1 = h1->next;
}
h2 = h2->next;
while (h2 != NULL)
{
cur = (struct node *)malloc(sizeof(struct node));
*cur = *h2;
cur->next = NULL;
ptr->next = cur;
ptr = cur;
h2 = h2->next;
}
add(*h);
printf("After addition\n");
display(*h);
}
//------------- MAIN FUNCTION ------------------------
int main()
{
struct node *h1, *h2, *h;
printf("Enter the data for 1st matrix\n");
create(&h1);
printf("Enter the data for 2nd matrix\n");
create(&h2);
printf("The 1st matrix:-\n");
display(h1);
printf("The 2nd matrix:-\n");
display(h2);
join(h1, h2, &h);
}