0% found this document useful (0 votes)
7 views

Sparse Matrix

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Sparse Matrix

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SPARSE MATRIX IMPLEMENTATION

USING LINKED LIST

#include<stdio.h>
#include<stdlib.h>

struct Node{
int val;
int row;
int col;
struct Node *next;
};

struct Node *create(int val, int row, int col){


struct Node *n = (struct Node *)malloc(sizeof(struct
Node));
n->next = NULL;
n->val = val;
n->row = row;
n->col = col;
return n;
}

void display(struct Node *head){


struct Node *n = head;
while(n != NULL){
printf("%d %d %d\n", n->val, n->row, n->col);
n = n->next;
}
}

int main(){
int row, col, val;
struct Node *head = NULL;
struct Node *node = head;
printf("Enter number of rows : ");
scanf("%d", &row);
printf("Enter number of cols : ");
scanf("%d", &col);
printf("Enter your matrix in standard form\n");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
scanf("%d", &val);
if(val != 0){
if(node == NULL){
node = create(val, i, j);
head = node;
}
else{
node->next = create(val, i, j);
node = node->next;
}
}
}
}
printf("Sparse matrix form")
display(head);
return 0;
}

OUTPUT

You might also like