-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparseMatrixwDLL.c
71 lines (60 loc) · 1.3 KB
/
sparseMatrixwDLL.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
#include <stdio.h>
#include <stdlib.h>
struct node{
int ri;
int ci;
int data;
struct node *next;
struct node *prev;
};
typedef struct node *Node;
Node insertend(Node start,int item, int row, int col){
Node temp,cur;
temp=(Node)malloc(sizeof(struct node));
temp->ri=row;
temp->ci=col;
temp->data=item;
temp->next=temp->prev=NULL;
if(start==NULL)
return temp;
else{
cur=start;
while(cur->next!=NULL){
cur=cur->next;
}
cur->next=temp;
temp->prev=cur;
return start;
}
}
void display(Node start,int m,int n){
int i,j;
Node cur=start;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(cur->ri==i && cur->ci==j && cur->data!=0){
printf("%d \t",cur->data);
cur=cur->next;
}
else
printf("0 \t");
}
printf("\n");
}
}
int main(){
int m=4,n=4;
int i,j,item;
Node start=NULL;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("Enter value to %d row and %d column: ",i,j);
scanf("%d",&item);
if(item!=0)
start=insertend(start,item,i,j);
}
}
printf("The contents of the matrix are: \n");
display(start,m,n);
return 0;
}