rvdp88
Legacy Member
Hallo, ik heb hier de code om nodes met een bepaalde int-waarde toe te voegen aan een gelinkte lijst. Stel nu dat ik echter geen int-waardes maar verschillende functies (vb. som(int a, int b) / verschil(int c, int d) /...) wil toevoegen aan deze lijst. Wat moet er dan juist aangepast worden in de code en wat is de correcte manier om deze functies dan in te geven in de main? Ik heb in vet gezet wat ik vermoed dat hoogstwss aangepast zal moeten worden, maar weet niet juist hoe...
Code:
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for malloc */
typedef struct ns
{
[B]int data;[/B]
struct ns *next; /* pointer to next element in list */
} node;
node *list_add(node **p, [B]int i[/B])
{
node *n = (node *)malloc(sizeof(node));
if (n == NULL)
return NULL;
n->next = *p; /* the previous element (*p) now becomes the "next" element */
*p = n; /* add new empty element to the front (head) of the list */
[B] n->data = i;[/B]
return *p;
}
int main(void)
{
node *n = NULL;
list_add(&n, 0); /* list: 0 */
list_add(&n, 1); /* list: 1 0 */
list_add(&n, 2); /* list: 2 1 0 */
list_add(&n, 3); /* list: 3 2 1 0 */
list_add(&n, 4); /* list: 4 3 2 1 0 */
return 0;
}