Sunday, January 15, 2023

Singly Linked List Delete Specific Node or We also say In between

hello Guys,

Singly Linked List Insert at specific position or in between function.

Function For Deleting Specific Node:

void delete_specific()
{
    struct node *a,*b=head;
    int i,poition;
    printf("\nEnter Position which you want to delete:");
    scanf("%d",&position);
    for(i=1; i<position; i++)
    {
        a=b;
        b=b->next;
    }
    a->next=b->next;
    free(b);
}

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_specific()
{
    struct node *a,*b=head;
    int i,poition;
    printf("\nEnter Position which you want to delete:");
    scanf("%d",&position);
    for(i=1; i<position; i++)
    {
        a=b;
        b=b->next;
    }
    a->next=b->next;
    free(b);
}

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

Output:



No comments: