Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int column;
int value;
int row;
struct node *next;
} element;
/* MENU */
void Menu()
{
printf("\n***\tADDING SPARSE MATRICES\t***\n");
printf("\n 1.) Insert in A");
printf("\n 2.) Insert in B");
printf("\n 3.) Printout both");
printf("\n 4.) A + B = ");
printf("\n 0.) EXIT");
printf("\nChoose ---------> ");
}
/* Initialize*/
void Init (element *x[])
{
int i;
for (i=0; i<3; i++) x[i] = NULL;
}
/* Printout */
void Printout (element *x[])
{
int i, test = 0;
element *temp;
for (i=0; i<3; i++)
{
temp = x[i];
while (temp != NULL)
{
printf("Element position (%d,%d) = %d\n", i, temp->column, temp->value);
test = 1;
temp = temp->next;
}
}
if (test == 0) printf("This matrix is empty!!\n");
}
/* INSERT */
void Insert(element *x[], int row, int column, int value)
{
int r = row;
element *p;
element *new = malloc(sizeof(element));
new->row = row;
new->column = column;
new->value = value;
if (x[r] == NULL)
{
x[r] = new;
new->next = NULL;
}
else
{
p = x[r];
if (new->column < p->column)
{
new->next = p;
x[r] = new;
}
else if (new->column > p->column)
{
while (p->next != NULL && p->next->column < new->column)
{
p = p->next;
}
new->next = p->next;
p->next = new;
}
else printf("An element already exists there!!\n");
}
}
/* A + B */
void Add (element *x[],element *y[],element *z[])
{
int i;
element *p, *q;
for (i=0; i<3; i++)
{
/*row of x empty, row of y not*/
if (x[i] == NULL && y[i] != NULL)
{
q = y[i];
while (q != NULL)
{
Insert(z, i, q->column, q->value);
q = q->next;
}
}
/*row of y empty, row of x not*/
if (x[i] != NULL && y[i] == NULL)
{
q = x[i];
while (q != NULL)
{
Insert(z, i, q->column, q->value);
q = q->next;
}
}
/*row of x and y not empty*/
if (x[i] != NULL && y[i] != NULL)
{
p = x[i];
q = y[i];
/*stay in while loop until one row runs out*/
while (p != NULL || q != NULL)
{
/*columns match*/
if (p->column == q->column && p != NULL && q != NULL)
{
Insert(z, i, p->column , p->value + q->value);
p = p->next;
q = q->next;
}
/*column of x is smaller than column of y*/
else if (p->column < q->column)
{
Insert(z, i, p->column , p->value);
p = p->next;
}
/*column of x is bigger than column of y*/
else
{
Insert(z, i, q->column , q->value);
q = q->next;
}
}
/*if row of x had more elements and didn't run out*/
if (p != NULL)
{
while (p != NULL)
{
Insert(z, i, p->column , p->value);
p = p->next;
}
}
/*if row of x had more elements and didn't run out*/
if (q != NULL)
{
while (q != NULL)
{
Insert(z, i, q->column , q->value);
q = q->next;
}
}
}
}
Printout(z);
}
/* FREE MEMORY*/
void Freedom (element *x[])
{
int i;
element *p;
for (i=0; i<3; i++)
{
if (x[i] != NULL)
{
p = x[i];
x[i] = x[i]->next;
free(p);
}
}
}
/* MAIN */
int main (int argc, const char * argv[])
{
int choice, column, row, value, number;
element *a[3], *b[3], *sum[3];
Init(a); Init(b); Init(sum);
do
{
Menu();
scanf("%d",&choice);
switch (choice)
{
case 1: /*Insert in A */
do
{
printf("Enter row -> ");
scanf("%d",&row);
} while (row < 0 || row > 3);
do
{
printf("Enter column -> ");
scanf("%d",&column);
} while (column < 0);
printf("Enter value -> ");
scanf("%d",&value);
Insert(a,row,column,value);
break;
case 2: /*Insert in B */
do
{
printf("Enter row -> ");
scanf("%d",&row);
} while (row < 0 || row > 2);
do
{
printf("Enter column -> ");
scanf("%d",&column);
} while (column < 0);
printf("Enter value -> ");
scanf("%d",&value);
Insert(b,row,column,value);
break;
case 3: /* Printout A & B */
printf("\n::::::: MATRIX A :> \n\n");
Printout(a);
printf("\n::::::: MATRIX B :> \n\n");
Printout(b);
break;
case 4: /* A + B */
Init(sum);
printf("\n::::::: MATRIX A + B :> \n\n");
Add(a,b,sum);
break;
default:
printf("\nWRONG CHOICE");
}
} while (choice != 0);
Freedom(a);
Freedom(b);
Freedom(sum);
return 0;
}