Insert from back : Linked List
I read the code from this site:
http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-using-C-C,
but it gave me segmentation fault and I don't quite get it.
*I modified it to my struct
struct Node
{
int type;
char cmd[256];
struct Node *next;
};
void insert(int val, char arr[])
{
struct Node *temp1 = (struct Node*)malloc(sizeof(struct Node));
struct Node *temp2 = (struct Node*)malloc(sizeof(struct Node));
temp1 = head;
while(temp1->next != NULL)
temp1 = temp1->next;
temp2->type = val;
strcpy(temp2->cmd, arr);
temp2->next = NULL;
temp1->next = temp2;
}
What is wrong with this code?
No comments:
Post a Comment