I am too bad now. My list is not working! I know that there is an issue of just coping my ptr into function, not actually using real one, but I can't understand how can I make this work as I want.
PS. I see also that if I make head as global value, it will be ok. But I want to get function, which I can call it specific list.
Here is function of adding element into a blamk list. I can't make even this function work. I tried to play with double pointers, but now I am here to ask some help.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void add( int num,struct node * head )
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if (head== NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
int main()
{
struct node *head;
head=NULL;
add(20,head);
if(head==NULL) printf("List is Empty\n");
return 0;
}
UPD: MY own playing with double pointers:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void add( int num, struct node **head )
{
struct node **temp;
temp=(struct node **)malloc(sizeof(struct node*));
(*temp)->data=num;
if (*head== NULL)
{
*head=*temp;
(*head)->next=NULL;
}
else
{
(*temp)->next=head;
*head=temp;
}
}
int main()
{
struct node *head;
head=NULL;
add(20,&head);
if(head==NULL) printf("List is Empty\n");
return 0;
}