Problem: Write a c program to add two matrices using linked list.

Each node of the linked list represent a matrix element, where:

  • row  –  Stores the row value of the element.
  • col    –  Stores the column value of the element.
  • data  – Store the value of the element.
  • *next -Store the address of next element (node).

The following code add two matrices in C using the concept of linked list..

Output

Case 1: When the order of the two matrix are the same.

matrix addition in x using linked list

Case 2: When the order of the two matrices is not the same.

matrix addition in c

Case 3: When one of the matrices is empty.

matrix addition code

In this program, we are using two matrices. Each matrix has:

  • rows        – Stores the count of the total number of rows in the matrix.
  • columns – Stores the count of the total number of columns in the matrix.
  • *head      – Sores the head element.
  • *tail         – Stores the tail element.

The program displays the menu in the loop which can handle multiple operations like

  • Displaying matrix.
  • Inputting elements into the matrix.
  • Adding two matrix.
  • Exit & more.

I recommend you to run the code with different inputs to understand the code at best.

Leave a Reply