Facebook

Deletion in Linked List

The elements of linked list can be deleted in three ways.

  • Deletion at beginning
  • Deletion at end
  • Deletion at any location

Program 1

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

int del_beg()
{
struct node *temp,*start;

temp = start;
start = start->next;
free(temp);
printf("nThe Element deleted Successfully ");
}
int delete_from_middle(int value)
{
     struct node *temp,*var;
     temp=head;
     while(temp!=NULL)
     {
          if(temp->data == value)
          {
                if(temp==head)
                {
                     head=temp->next;
                     free(temp);
                     return 0;
                }
                else
                {
                     var->next=temp->next;
                     free(temp);
                     return 0;
                }
          }
          else
          {
               var=temp;
               temp=temp->next;
          }
     }
printf("data deleted from list is %d",value);
}
int delete_from_end()
{
     struct node *temp;
     temp=head;
     while(temp->next != NULL)
     {
          var=temp;
          temp=temp->next;
     }
     if(temp ==head)
     {
          head=temp->next; 
          free(temp);
          return 0;
     }
     printf("data deleted from list is %d",temp->data);
     var->next=NULL;
     free(temp);
     return 0;
}
void display()
{
     trav=head;
     if(trav==NULL)
     {
          printf("\nList is Empty");
     }
     else
     {
          printf("\nElements in the List: ");
          while(trav!=NULL)
          {
               printf(" -> %d ",trav->data);
               trav=trav->next;
          }
      printf("\n");
      }
}
int main()
{
     int i=0;
     head=NULL;
     while(1)
     {
           printf("\nenter the choice of operation to perform on linked list");
           scanf("%d",&i);
           switch(i)
           {
           		 case 1:
                {
                del_beg();
                display();
                break;
                }

                case 2:
                {
                delete_from_end();
                display();
                break;
                }
                case 3:
                {
                int value;
                display();
                printf("\nenter the data that you want to delete from the list shown above");
                scanf("%d",&value);
                delete_from_middle(value);
                display();
                break;
                }
                case 6:
                {
                exit(0);
                }
           }
      }
return 0;
}

Run

 

 

Illustration of Program 1

  • node is the name of structure.
  • data refers to element of linked list.
  • *next points to subsequent address of linked list.
  • *head points to first node of list.
  •  *var refers to the node.
  •  *trav refers to the node.

Working of  int del_beg()

  • *temp and *start are pointers of structure.
  •   temp =start (Fig 1) - Value of start is stored in temporary pointer.
Fig 1 : Linked List
Fig 1 : Linked List

 

  • start =start ->next - The start pointer will move one position ahead i.e it will point to 2.(Fig 2)
Fig 2 :Working of start = start ->next
Fig 2 :Working of start = start ->next
  • free(temp) - Deletes the temp node(node at beginning of linked list)(Fig 3)
Fig 3 : Deletion at front
Fig 3 : Deletion at front

 

Working of  int delete_from_middle(int value)

  • *temp and *var are pointers of structure.
  • temp = head - Value of head is stored in temp (Fig 4)
  • Refer Fig 5 for working of function.
Screen Shot 2014-04-04 at 3.35.56 PM
Fig 4 : Linked List

 

 

 

 

 

 

Screen Shot 2014-04-04 at 4.15.52 PM
Fig 6 :Working of while loop

 

Screen Shot 2014-04-04 at 4.03.40 PM
Fig 7 : Deletion at any location

 

Working of int delete_from_end()

  • *temp and *var are pointers of structure.
  •  temp = head ;(Value of head is stored in temp)
Screen Shot 2014-04-04 at 3.35.56 PM
Fig 8 : Linked Lis

 

Screen Shot 2014-04-04 at 4.45.04 PM
Fig 9 : Working of while loop

 

 

Screen Shot 2014-04-04 at 4.38.15 PM
Fig 10 : Deletion at end

 

  • main function is used to call the functions.The function is called using cases of switch statement.
Rate this post

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar