Untitled

 avatar
unknown
plain_text
14 days ago
7.0 kB
5
Indexable
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int row;            // Row index of the matrix element
    int col;            // Column index of the matrix element
    int value;          // Value of the matrix element
    struct Node* next;  // Pointer to the next node in the row
    struct Node* prev;  // Pointer to the previous node in the row
} Node;

typedef struct SparseMatrix {
    Node* head;        // Head of the linked list
    int rows;          // Number of rows
    int cols;          // Number of columns
} SparseMatrix;

// Function to create a new node
Node* createNode(int row, int col, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->row = row;
    newNode->col = col;
    newNode->value = value;
    newNode->next = NULL;
    newNode->prev = NULL;
    return newNode;
}

// Function to create a sparse matrix
SparseMatrix* createSparseMatrix(int rows, int cols) {
    SparseMatrix* matrix = (SparseMatrix*)malloc(sizeof(SparseMatrix));
    matrix->head = NULL;
    matrix->rows = rows;
    matrix->cols = cols;
    return matrix;
}

// Function to read a sparse matrix
SparseMatrix* readSparseMatrix() {
    int rows, cols, nonZeroCount;
    printf("Enter number of rows, columns and non-zero elements: ");
    scanf("%d %d %d", &rows, &cols, &nonZeroCount);

    SparseMatrix* matrix = createSparseMatrix(rows, cols);
    
    printf("Enter row, column and value of non-zero elements:\n");
    for (int i = 0; i < nonZeroCount; i++) {
        int row, col, value;
        scanf("%d %d %d", &row, &col, &value);
        Node* newNode = createNode(row, col, value);
        
        // Insert the new node into the linked list
        if (matrix->head == NULL) {
            matrix->head = newNode;
        } else {
            Node* temp = matrix->head;
            while (temp->next != NULL) {
                temp = temp->next;
            }
            temp->next = newNode;
            newNode->prev = temp;
        }
    }
    return matrix;
}

// Function to add two sparse matrices
SparseMatrix* addSparseMatrices(SparseMatrix* mat1, SparseMatrix* mat2) {
    SparseMatrix* result = createSparseMatrix(mat1->rows, mat1->cols);
    Node* ptr1 = mat1->head;
    Node* ptr2 = mat2->head;

    while (ptr1 != NULL || ptr2 != NULL) {
        if (ptr1 == NULL) {
            // Only mat2 has elements left
            Node* newNode = createNode(ptr2->row, ptr2->col, ptr2->value);
            if (result->head == NULL) {
                result->head = newNode;
            } else {
                Node* temp = result->head;
                while (temp->next != NULL) {
                    temp = temp->next;
                }
                temp->next = newNode;
                newNode->prev = temp;
            }
            ptr2 = ptr2->next;
        } else if (ptr2 == NULL) {
            // Only mat1 has elements left
            Node* newNode = createNode(ptr1->row, ptr1->col, ptr1->value);
            if (result->head == NULL) {
                result->head = newNode;
            } else {
                Node* temp = result->head;
                while (temp->next != NULL) {
                    temp = temp->next;
                }
                temp->next = newNode;
                newNode->prev = temp;
            }
            ptr1 = ptr1->next;
        } else if (ptr1->row == ptr2->row && ptr1->col == ptr2->col) {
            // Same position in both matrices
            int sum = ptr1->value + ptr2->value;
            if (sum != 0) {
                Node* newNode = createNode(ptr1->row, ptr1->col, sum);
                if (result->head == NULL) {
                    result->head = newNode;
                } else {
                    Node* temp = result->head;
                    while (temp->next != NULL) {
                        temp = temp-> next;
                    }
                    temp->next = newNode;
                    newNode->prev = temp;
                }
            }
            ptr1 = ptr1->next;
            ptr2 = ptr2->next;
        } else if (ptr1->row < ptr2->row || (ptr1->row == ptr2->row && ptr1->col < ptr2->col)) {
            // Only mat1 has the current element
            Node* newNode = createNode(ptr1->row, ptr1->col, ptr1->value);
            if (result->head == NULL) {
                result->head = newNode;
            } else {
                Node* temp = result->head;
                while (temp->next != NULL) {
                    temp = temp->next;
                }
                temp->next = newNode;
                newNode->prev = temp;
            }
            ptr1 = ptr1->next;
        } else {
            // Only mat2 has the current element
            Node* newNode = createNode(ptr2->row, ptr2->col, ptr2->value);
            if (result->head == NULL) {
                result->head = newNode;
            } else {
                Node* temp = result->head;
                while (temp->next != NULL) {
                    temp = temp->next;
                }
                temp->next = newNode;
                newNode->prev = temp;
            }
            ptr2 = ptr2->next;
        }
    }
    return result;
}

// Function to transpose a sparse matrix
SparseMatrix* transposeSparseMatrix(SparseMatrix* matrix) {
    SparseMatrix* transposed = createSparseMatrix(matrix->cols, matrix->rows);
    Node* current = matrix->head;

    while (current != NULL) {
        Node* newNode = createNode(current->col, current->row, current->value);
        if (transposed->head == NULL) {
            transposed->head = newNode;
        } else {
            Node* temp = transposed->head;
            while (temp->next != NULL) {
                temp = temp->next;
            }
            temp->next = newNode;
            newNode->prev = temp;
        }
        current = current->next;
    }
    return transposed;
}

// Function to print a sparse matrix
void printSparseMatrix(SparseMatrix* matrix) {
    Node* current = matrix->head;
    printf("Sparse Matrix:\n");
    while (current != NULL) {
        printf("Row: %d, Col: %d, Value: %d\n", current->row, current->col, current->value);
        current = current->next;
    }
}

// Main function
int main() {
    printf("Enter first sparse matrix:\n");
    SparseMatrix* mat1 = readSparseMatrix();
    printf("Enter second sparse matrix:\n");
    SparseMatrix* mat2 = readSparseMatrix();

    SparseMatrix* sum = addSparseMatrices(mat1, mat2);
    printf("Sum of the sparse matrices:\n");
    printSparseMatrix(sum);

    SparseMatrix* transposed = transposeSparseMatrix(mat1);
    printf("Transposed of the first sparse matrix:\n");
    printSparseMatrix(transposed);

    // Free allocated memory (not shown for brevity)
    
    return 0;
}
Editor is loading...
Leave a Comment