# deleting first element of a linked list

By this code I can't delete the first element of my linked list. Can anybody tell me how to fix it

``````// defining the linked list (basics)
#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node* next;
};

void create(struct node **head, int data)
{
struct node **temp = head;
while (*temp != NULL)
{
temp = &(*temp) -> next;
}
(*temp) = (struct node*)malloc(sizeof(struct node));
(*temp) -> data = data;
(*temp) -> next = NULL;
}

void del(struct node *head, int data)
{
if (head -> data == data)
{
struct node *temp = head -> next;
}
else
{
while ((head -> next -> data) != data)
{
}
struct node *prev = head;
struct node *next = head -> next -> next;
prev -> next = next;
}
}

void prnt(struct node *head)
{
{
}
}

int main()
{
struct node *begin = NULL;
for (int i = 0; i < 10; i++)
{
create(&begin, i);
}
prnt(begin);
printf("\n");
del(begin,5);
prnt(begin);
printf("\n");
return 0;
}
``````

Deleting a node from a linked list requires changing the pointer to this node. So it might be useful to have a pointer to the head pointer, like

``````void del(struct node **head_ptr, int data)
{

while (*head_ptr != NULL && (*head_ptr) -> data != data)
{
}
if (*head_ptr != NULL)
{
struct node *next = (*head_ptr) -> next;
``````    del(&begin, 5);
• Also tell me how to write the code by your logic but with this initialization of the function `void del(struct node *head_ptr, int data)` – Rahul Oct 28 '17 at 14:41