Program to Delete an element from a doubly linked list.

Answers were Sorted based on User's Feedback



Program to Delete an element from a doubly linked list. ..

Answer / splurgeop

// 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;
};

Is This Answer Correct ?    34 Yes 9 No

Program to Delete an element from a doubly linked list. ..

Answer / viktor

typedef int info_t;
typedef struct element
{
info_t info;
struct element *next;
struct element *prev;
}node;
typedef node* nodep;


Function to delete a node:

void del_node(nodep p)
{
(p->prev)->next=p->next;
(p->next)->prev=p->prev;
free(p);
}

Is This Answer Correct ?    20 Yes 9 No

Program to Delete an element from a doubly linked list. ..

Answer / shruti

to delete an element.
enter the position of the element to be deleted.
-> pos.

structure of node is

struct node
{
int data;
struct node *prev , *next;
}

//home is the starting pointer of hte list.
struct node * delete(struct node *home , int pos)
{
temp = home;
if(pos == 1)
{
temp = home;
home = home -> next;
free(temp);
}

temp = home;
p = home;

for(i = 0 ; i < pos ; i++)
{
p = p -> next;
}

temp = p -> next;
temp1 = temp -> next;

p -> next = temp1;
temp1 -> prev = p

free(temp);
}

return home;
}

Is This Answer Correct ?    9 Yes 5 No

Program to Delete an element from a doubly linked list. ..

Answer / aggdhbsam

dsgfdagfdg
[op;iop

Is This Answer Correct ?    7 Yes 21 No

Post New Answer

More C Code Interview Questions

main() { int i=5; printf("%d",++i++); }

1 Answers  


main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } }

2 Answers   CSS, Wipro,


Predict the Output: int main() { int *p=(int *)2000; scanf("%d",2000); printf("%d",*p); return 0; } if input is 20 ,what will be print

2 Answers  


main( ) { void *vp; char ch = ‘g’, *cp = “goofy”; int j = 20; vp = &ch; printf(“%c”, *(char *)vp); vp = &j; printf(“%d”,*(int *)vp); vp = cp; printf(“%s”,(char *)vp + 3); }

1 Answers  


#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

2 Answers  






C statement to copy a string without using loop and library function..

2 Answers   Persistent, TCS,


main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }

9 Answers   CSC, GoDB Tech, IBM,


main() { int i=10; i=!i>14; Printf ("i=%d",i); }

1 Answers  


main() { main(); }

1 Answers  


Write a program to print a square of size 5 by using the character S.

6 Answers   Microsoft,


What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }

1 Answers  


Finding a number multiplication of 8 with out using arithmetic operator

8 Answers   Eyeball, NetApp,


Categories