Rahul Reddy
Rahul Reddy

Reputation: 13153

How to declare a linked list in c

struct node {
int data;
struct node *next,*prev;
};
void insert(struct node *head,int data){
if(head == NULL){
head = (node *)malloc(sizeof(node));
 --- code continues-----

I just want to know the difference between head = (node *)malloc(sizeof(node)); and struct node *head = malloc(sizeof(struct node)); And if I pass **head as a parameter of the insert function what does it do ?

Upvotes: 1

Views: 15802

Answers (5)

Prashant Anuragi
Prashant Anuragi

Reputation: 390

The answer for your first question is:

head=(node *)malloc(sizeof(node));

malloc() returns a pointer of type void so here you are explicitly converting it to a pointer of type node .But remember, in C a pointer of type void() converts implicitly into the type of pointer that it is assign to. So if you write like:

head=malloc(sizeof(node));

It will stil work correctly. But with the C++ compiler it is not the case, In C++ pointer of type void is not implicitly cast to the type of pointer that it it assign to.

Also to answer your question,

void insert(struct node *head,int data)

If you pass **head as parameter it will show an error declaring that the type of operator is not matched. This is because you had declared it as struct node *head not as struct node **head.

Upvotes: 2

9827085704
9827085704

Reputation: 29

create (struct node **p)
{
struct node *temp,*q;
int n;
printf("enter the length of link list");
scanf("%d".&n);
while(n>0)
{
if(*p == NULL)
{
*p=(struct node*)malloc(sizeof(struct node*));
printf("enter the element");
scanf("%d".&((*p)->data));
(*p)->next=NULL;
}
else
q=*p;
while(q->next=NULL)
q=q->next;

Upvotes: 0

user2030052
user2030052

Reputation:

If you type in

typedef struct node{
    int data;
    struct node *next, *prev;
} node;

the compiler will accept your head = (node *)malloc(sizeof(node)); code. Remember that typedef allows you to use the struct the same way it's used in C++.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 754090

The difference between:

head = (node *)malloc(sizeof(node));
struct node *head = malloc(sizeof(struct node));

is that a C compiler will reject the first and allow the second, but a C++ compiler will accept the first and reject the second.

In C, the code shown does not create a type name node when you define or declare struct node. You would need to add typedef struct node node; in the C source. C++ automatically creates the type name node from the definition of struct node. (A C++ compiler rejects the second because of the implicit cast from void * to struct node *; C++ does not allow that, and would require struct node *head = (struct node *)malloc(sizeof(struct node));)

And if I pass **head as a parameter of the insert function, what does it do?

You'd have to adjust the body of the function, but it would allow you to change the location of the head of the list in the calling function.

Upvotes: 3

Refugnic Eternium
Refugnic Eternium

Reputation: 4291

Technically there isn't a difference between sizeof(node) and sizeof(struct node) (at least in C++), since both will return the size of the struct.

In C however it's mandatory to write struct node, just like it's mandatory to write struct node when declaring a variable of that type, simply because there is no type node. C only understands primitives (int, char, long) and custom types declared with struct. Every C compiler is very strict with this keyword struct and assumes you're talking about a variable if you forget it.

Regarding your second question: You can't. You can't pass a pointer to a pointer to a function, which only accepts a regular pointer, unless you cast it. In that case however, it will point to a completely arbitrary position on the stack (where your pointer is located) and probably make your program crash.

Upvotes: 0

Related Questions