I'm trying print the truth table of a logic gate. I have to use a linked list and a specific structure.
typedef struct data
{
int value;
struct data * next;
} Data;
typedef Data * DataList;
int myandlst(DataList *list);
int report(DataList inputs);
int main( )
{
DataList inputs=NULL;
myandlst(&inputs);
report(inputs);
return 0;
}
Data * createData( int value)
{
Data * dataptr;
dataptr = malloc(sizeof (Data));
dataptr->value = value;
dataptr->next = NULL;
return dataptr;
}
void appendData(DataList *lstptr, Data *newptr)
{
if (*lstptr==NULL)
{
*lstptr = newptr;
return;
}
appendData( &((*lstptr)->next), newptr);
return;
}
int myandlst (DataList *inlist)
{
int i,j;
for (i=0; i<2; i++)
{
for (j = 0; j<2; j++)
{
appendData(inlist,createData(i*j));
}
}
return 0;
}
int report(DataList inputs)
{
DataList temp ;
if (inputs == NULL)
return;
for (temp = inputs; temp != NULL; temp = temp->next)
printf("%d\n",temp->value);
printf("\n");
return 0;
}
I've managed to pass the values to the list and print them. What I don't know how to do is print the full truth table of the gate. Current output:
0
0
0
1
Desired output:
0 0 0
0 1 0
1 0 0
1 1 1
Is there a good way to do this that I can't think of? The program is supposed to take n amount of inputs each time, not only 2, therefore a lot of tricks I thought of won't work.