0

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.

Joe Durner
  • 27
  • 3
  • Also, how can I make it so that I can use the same list to print more truth tables without printing all of them each time I call the report function? – Joe Durner May 30 '21 at 12:07
  • 3
    How is a linked list related to a logic gate? Why would you use a linked list at all - why not an array or just a loop? What does the linked list store? You seem to have stored 4 values in a linked list, if the presented output is from `report()` function, yet your output is a 4x3 table. How do the 4 values correspond to values inside a 4x3 table? – KamilCuk May 30 '21 at 12:16
  • Please don't typedef pointers. And if you do, don't use `Datalist` in some code and `Data *` in others. The typedef here just obfuscates the code, and does not clarify anything. – William Pursell May 30 '21 at 12:16
  • How is your desired output not a square? For a 4 value list, I would expect the desired output to be 2x2. For 3 values in the truth table, I would expect a 3x3 output. What situation would render a 4x3 output? – William Pursell May 30 '21 at 12:35
  • As I said "I have to use a linked list and a specific structure" . This is exactly what I'm asked to do. I can't change the typedef in any way either. – Joe Durner May 30 '21 at 12:48
  • @WilliamPursell: For 3 values in the truth table, you get 2³ rather than 3×3, no? – M Oehm May 30 '21 at 12:48
  • These are 2 values and the output. – Joe Durner May 30 '21 at 12:49
  • The prescribed data structure is strange for this problem. Linked lists are useful for short lists of initially unknown length. If you know the number of input values n, you know the number of combinations, 2ⁿ and could just use an array or a loop, where the binary representation of the index are the input values. I could perhaps imagine a binary tree, i.e. a linked list with two links, where going left on a level means false and going right means true. – M Oehm May 30 '21 at 13:05
  • @MOehm Yes, of course. with 3 values you get an 8X3 – William Pursell May 30 '21 at 13:22

0 Answers0