From 7c5b9a0824e966e1ad8634ca128da549055211dd Mon Sep 17 00:00:00 2001 From: Mohit Singh Date: Thu, 20 Dec 2018 19:18:32 +0530 Subject: [PATCH] Sparse Matrix program using Double Linked List --- .../sparseMatrix.c | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Sparse Matrix using Double Linked List/sparseMatrix.c diff --git a/Sparse Matrix using Double Linked List/sparseMatrix.c b/Sparse Matrix using Double Linked List/sparseMatrix.c new file mode 100644 index 0000000..d61aeba --- /dev/null +++ b/Sparse Matrix using Double Linked List/sparseMatrix.c @@ -0,0 +1,112 @@ +#include +#include + +struct node +{ + int row,col,data; + struct node *prev, *next; +}; + +struct node *getnode(); +struct node *insert(struct node*, int, int, int); +void displayList(struct node*); +void displayMatrix(struct node*, int, int); + +int main() +{ + struct node *list = NULL; + int nr,nc,num,row,col,data,i; + printf("Enter no. of rows and columns in the matrix:"); + scanf("%d%d",&nr, &nc); + printf("Enter no. of non zero elements in the matrix: "); + scanf("%d",&num); + printf("Enter non zero elements of the matrix:\n"); + for(i=0;i prev = newnode ->next = NULL; + return newnode; +} + +struct node *insert(struct node *head, int r, int c, int d) +{ + struct node *newnode = getnode(); + newnode -> row = r; + newnode -> col = c; + newnode -> data = d; + if(head == NULL) + { + head = newnode; + } + else + { + newnode -> next = head; + head -> prev = newnode; + head = newnode; + } + return head; +} + +void displayList(struct node *head) +{ + if(head == NULL) + { + printf("List is Empty\n"); + return; + } + struct node *temp = head; + printf("Row\tColumn\tData\n"); + while(temp!=NULL) + { + printf("%d\t %d\t %d\n",temp->row,temp->col,temp->data); + temp=temp->next; + } +} + +void displayMatrix(struct node *head, int r, int c) +{ + int i,j,flag; + struct node *temp = head; + if(head == NULL) + { + printf("Matrix is Empty\n"); + return; + } + for(i=0;irow == i && temp -> col == j) + { + printf("%d ",temp->data); + flag = 1; + } + temp=temp->next; + } + if(flag == 0) + printf("0 "); + } + printf("\n"); + } +}