vendredi 27 février 2015

change this c code struct to dynamically(malloc)



So I created a c code for this data below



struct node {
int value;
struct node *next;
}


My code should dynamically create the structure given like this:



1st -> 5 -> 7 -> 2, then 2->5(there is an arrow beneath 2 and link it back to 5)


Here is my code so far, do I need to change any pointer?



void insert(node * pointer, int value)
{
node *start = pointer;

/* Iterate through the list till we encounter the last node. */
while (pointer->next != start)
{
pointer = pointer->next;
}
/* Allocate memory for the new node and put value in it. */
pointer->next = (node *) malloc(sizeof(node));
pointer = pointer->next;
pointer->value = value;
pointer->next = start;
}



Aucun commentaire:

Enregistrer un commentaire