I need a help. I tried to create a singly linked list. Everything is fine so far,but I got a wrong output. I have no idea about my mistake. Thank you!
My output is :
create a single linked list
list:
aaaaa
-1342177280
My code is :
struct node{
int key;
int val;
struct node *next;
};
struct node *create_single_link(){
struct node *head = malloc(sizeof(struct node));
head->next = NULL;
return(head);
}
void insertVal( struct node *lst, int v){
struct node *current = malloc(sizeof(struct node));
current -> val = v;
current->next = lst;
lst = current;
}
void print_list(struct node * head){
struct node *ptr = head;
printf("list:\n");
while(ptr != NULL ){
printf("aaaaa\n");
printf("%d \n ",ptr ->val );
ptr = ptr->next;
}
}
int main(int argc, char const *argv[])
{
struct node *list;
list = create_single_link();
if(list != NULL){
printf("create a single linked list\n");
}
else
printf("failed to create a single linked list\n");
insertVal(list,2222);
//insertVal(list,2);
print_list(list);
return 0;
}