1
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 ?

Étienne
  • 4,773
  • 2
  • 33
  • 58
Rahul Reddy
  • 12,613
  • 10
  • 22
  • 21

5 Answers5

3

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • can you please help me write a C code to insert elements in a double linked list at the tail ? – Rahul Reddy Jun 05 '13 at 17:54
  • @user2384801: In that case, the question would become a duplicate ;-) – wildplasser Jun 05 '13 at 17:55
  • How are you going to organize your doubly-linked list? Will it be circular with the initial head node pointing to itself, or will you use a different organization? You can change the interface you have to `struct node *insert(struct node *head, int data)` and have the function return the new head value (possibly after allocating it), or you can use `void insert(struct node **head, int data)` and modify the value in `*head` inside the function. You can report an error by returning a null pointer in the first interface; you don't have a direct way of reporting an error in the second. – Jonathan Leffler Jun 05 '13 at 17:59
  • insert an element into doubly linked list .not a circular list. – Rahul Reddy Jun 05 '13 at 18:08
  • You can have circular or non-circular doubly-linked lists. There are advantages and disadvantages to both organizations, and you write the code using the lists differently. So you need to decide what you're up to. What don't you understand of what Wikipedia says about [doubly linked lists](http://en.wikipedia.org/wiki/Doubly_linked_list)? – Jonathan Leffler Jun 05 '13 at 18:18
2

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.

Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27
Prashant Anuragi
  • 390
  • 4
  • 14
1

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++.

0

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.

Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24
0
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;
9827085704
  • 29
  • 2