C Programming language

adplus-dvertising
C - Singly linked list, Delete
Previous Home Next

Like insertion we can perform deletion in the following three manners :

  1. Deleting element from the front.
  2. Deleting element from the end.
  3. Deleting element from the specified position.

Following is a small code snippet which suggest how we can perform deletion in a singly linked list :

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
    {
int num;
struct node *next;
};                           /* declaring a global node of type struct */
typedef struct node NODE;    /* providing a type definition to the above created structure */
NODE *head=NULL;            /* declaring some of the global variable that would be used throughtout the program */
NODE *temp, *first;
int info;
void display();
void create_list();
void del_at_begin();
void del_at_end();
void del_at_specifiedpos();
void main()                 /* starting the main method() */
{
int i;
clrscr();
printf("\nprogram for deletion in a singly linked list :\n");
do
{
printf("\nEnter your choice :\n");        /*    creating menu for various deletion operations on the list */
printf("\n1.Create a linked list :");
printf("\n2.Delete from the front in the linked list :");
printf("\n3.Delete from the end position in the linked list :");
printf("\n4.Delete at any specified position in the linked list :");
printf("\n5.Exit\n");
fflush(stdin);
scanf("\n%d",&i);
switch(i)
{
case 1:
create_list();
display();
case 2:
del_at_begin();
display();
break;
case 3:
del_at_end();
display();
break;
case 4:
del_at_specifiedpos();
display();
break;
case 5:
exit(0);
}
}
while(i!=5);
getch();
}
void create_list()
{
printf("\nEnter your element in the linked list :");
scanf("%d",&info);
temp=(NODE *)malloc(sizeof(NODE));         /* allocating memory for the node to be inserted */
temp->num=info;
temp->next=NULL;
if(head==NULL)                            /* checking whether list is empty */
{
head=temp;
}
else
{
first=head;
while(first->next!=NULL)
{
first=first->next;
}
first->next=temp;
}
}
void display()
{
first=head;
printf("\nStatus of the linked list is as follows :\n");
while(first->next!=NULL)                           /* traversing the linked list */
{
printf("\n%d",first->num);
first=first->next;
}
}
void del_at_begin()
{
 if(head==NULL)
{
     printf("linked list is empty");
}
 else
{
    temp=head;
head=temp->next;
free(temp);
}

}
void del_at_end()
{
 if(head==NULL)

 {
  printf("\nlinked list is empty");
 }
else
{
     head=temp;
 while(temp->next!=NULL)
{
     first=temp;
 temp=temp->next;
}
    first->next=NULL;
free(temp);
}
}
void del_at_specifiedpos()
{
 int node_num;
 if(head==NULL)
{
    printf("\nlinked list is empty");
}
else
{
    temp=head;
 printf("\nEnter the position of the node you want to delete");
 scanf("\n%d",&node_num);
    for(int i=1;i<node_num;i++)
{
     first=temp;
 temp=temp->next;
}
first->next=temp->next;
free(temp);
}
}
Previous Home Next