Deletion in Linked List

http://install4install.com

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

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

Working of  int del_beg()

Fig 1 : Linked List

Fig 1 : Linked List

 

Fig 2 :Working of start = start ->next

Fig 2 :Working of start = start ->next

Fig 3 : Deletion at front

Fig 3 : Deletion at front

 

Working of  int delete_from_middle(int value)

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()

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

 



Short URL: http://tinyit.cc/9d848
Author: Cusp2207 on April 4, 2014
Category: Computer Science
Tags:

Leave a Reply

Last articles