0% found this document useful (0 votes)
25 views3 pages

Sparse Matrix Creation Algorithm

The document describes algorithms for creating and manipulating a sparse matrix data structure using linked lists. It includes algorithms for creating nodes, inserting terms into a sparse matrix, and displaying the contents of the sparse matrix.

Uploaded by

Shanthi.V
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)
25 views3 pages

Sparse Matrix Creation Algorithm

The document describes algorithms for creating and manipulating a sparse matrix data structure using linked lists. It includes algorithms for creating nodes, inserting terms into a sparse matrix, and displaying the contents of the sparse matrix.

Uploaded by

Shanthi.V
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

Sparse Matrix

struct node
{
int Row;
int Col;
int Data;
struct node * link;
}

typedef struct node * NODE_PTR;

Algorithm Create_Node (int r, int c, int d)


1. NODE_PTR Temp;
2. Temp = malloc (sizeof (struct node)
3. If (Temp = NULL)
4. Print (No Sufficient Memory)
5. Exit
6. Else
7. Temp->Row = r
8. Temp->Col = c
9. Temp0>Data = d
10. Temp->link = NULL
11. Return Temp;
12. Exit

Algorithm Create_Sparse_Matrix ( )
1. NODE_PTR Start = NULL
2. Repeat Steps 3 to 18 while (New Term is added)
3. Read (Row, d; Column, c; and Data, D)
4. Cur = Create_Node (r, c, d)
5. Start = Insert_Term (Cur, Start)
6. Return Start

Algorithm Insert_Term (NODE_PTR Cur, Start)


1. NODE_PTR Cur, Prev, Next
2. If (Start = NULL)
3. Cur->link = Start
4. Start = Cur
5. Else
6. Prev = Start
7. Next = Start->link
8. Repeat Steps 14 and 15 while (Next ≠ NULL)
9. Prev = Next
10. Next = Next->link
11. Prev->link = Cur
12. Cur->link = Next
13. Return Start

Algorithm Display (NODE_PTR Start)


1. NODE_PTR Cur
2. Cur = Start
3. Repeat Steps 4, 5 and 6 while (Cur ≠ NULL)
4. Print (Cur->Row)
5. Print (Cur->Col)
6. Print (Cur->Data)
7. Cur = Cur-> link
8. Exit

Algorithm Main ( )
1. NODE_PTR Sparse
2. Sparse = Create_Sparse_Matrix ( )
3. Display (Sparse)
4. Exit

You might also like