ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories >> Code-Snippets >> Programming-Code >> C-Code
 
 
 
Question
Program to Delete an element from a doubly linked list.
 Question Submitted By :: =-PKG-=
I also faced this Question!!     Rank Answer Posted By  
 
Answer
// assumin all things are given function to delete 
from                beginning.....



int delete_from_beg()
 {
   int el;
   if(head==NULL)
     {
       printf("\n can't delete ");
       return -1;
     }
  else
   {
      struct doubly *temp;
      temp=head;
      el=temp->info;
      head=temp->next;
      temp->next=NULL;
      head->prev=NULL;
      return el;
   }

 }



//delete from end


int delete_from_end()
 {
    int el;
    if(head==NULL)
       {
	 printf("\n can't delete");
	 return -1;
       }
    else
    {
      struct doubly *temp;
      temp=head;
      while(temp->next!=NULL)
       temp=temp->next;
      el=temp->info;
      if(temp==head)
	head=NULL;
      else
	temp->prev->next=NULL;
      return el;
    }
 }




// delete from any position



int delete_at_pos(int item)
 {
   int el,flag=0;
   struct doubly *temp;
   if(head==NULL)
     {
	printf("\n cant delete ");
	return -1;
     }
   else
     {
	temp=head;
	while(item>1)
	  {
	     item--;
	     temp=temp->next;
	     if(temp==NULL&& item>=1)
	       {
		 flag=1;
		 break;
	       }
	  }
	if(flag==1)
	 {
	    el=-1;
	    printf("\n cant delete at the specified 
location");
	 }
	else
	 {
	    if(temp==head)
	      {
		el=temp->info;
		head=temp->next;
	      }
	     else
	       {
		 struct doubly *t;
		 t=temp;
		 el=temp->info;
		 temp->prev->next=t->next;
		 temp->next->prev=t->prev;
	       }

	 }
     }
     return el;
 }




// where doubly is structure

struct doubly
 {
     int data;
     struct  doubly *prev,*next;
};
 
1
Splurgeop
 
View All Answers
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com