Sunday, January 15, 2023

Singly Linked List Delete First Node From Linked List

 Welcome Friends,

Singly Linked List Delete First Node Function. It Just a Three Line Code. For Execute this, Use Full Program.

Function for Delete First Node:

void delete_first()
{
    struct node *a=head;
    head=a->next;
    free(a);
    a=NULL;    
}

Full Program:

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *newnode,*head,*temp;

void create()
{int num,i;
printf("\nHow many nodes you want to create:");
scanf("%d",&num);
for(i=0; i<num; i++)
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter Data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
{head=temp=newnode;}
else
{temp->next=newnode;
temp=temp->next;
}}}

void delete_first()
{
    struct node *a=head;
    head=a->next;
    free(a);
    a=NULL;    
}

void display()
{
    struct node *p;
p=head;
while(p->next!=NULL)
{
printf("%d->", p->data);
p=p->next;
}
printf("%d->",p->data);
}

Output:



No comments: