-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse using linked list.cpp
More file actions
73 lines (73 loc) · 1.06 KB
/
sparse using linked list.cpp
File metadata and controls
73 lines (73 loc) · 1.06 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
#include<stdio.h>
#include<stdlib.h>
typedef struct sparse
{
int data;
int i;
int j;
struct sparse *col;
struct sparse *row;
}sparse;
void initsparse(sparse *sp)
{
sp->col=NULL;
sp->row=NULL;
}
void createcol(sparse **start,int j)
{
sparse *nd,*last;
int i;
for(i=0;i<j;i++)
{
nd=(sparse*)malloc(sizeof(sparse));
if(nd==NULL)
{
printf("out of space");
exit(1);
}
nd->j=i+1;
nd->i=0;
if(*start==NULL)
*start=nd;
else
last->col=nd;
last=nd;
}
last->col=*start;
}
void createrow(sparse **start,int j)
{
sparse *nd,*last;
int i;
for(i=0;i<j;i++)
{
nd=(sparse*)malloc(sizeof(sparse));
if(nd==NULL)
{
printf("out of space");
exit(1);
}
nd->i=i+1;
nd->j=0;
if(*start=NULL)
*start=nd;
else
last->row=nd;
last=nd;
}
last->row=*start;
}
void createsparse(sparse *header)
{
sparse *sp;
printf("how many rows and columns?-");
scanf("%d%d",&header->i,&header->j);
createcol(&header->col,header->j);
createrow(&header->row,header->i);
}
int main()
{
sparse s;
initsparse(&s);
createsparse(&s);
}