I have the following function to delete nodes in a doubly linked list at a specified position
void Delete(struct dnode **ptr,int pos) /*pos refers to position at which element is to be deleted and ptr is the pointer to the first node in the list */
{
struct dnode *temp;
temp=*ptr;
if(pos==1)
{
*ptr=(*ptr)->next;
(*ptr)->prev=NULL;
}
else
{
while(pos-1)
{
temp=temp->next;
pos--;
}
if(temp->next==NULL)
{
temp->prev->next=NULL;
}
else
{
temp->next->prev=temp->prev;
temp->prev->next=temp->next;
}
}
free(temp);
}
Here dnode refers to the structure node and prev and next are pointers to the previous and next elements respectively. I am not getting the desired output.
My linked list contained: 55 96 33 11 66 2 17 14 99
I called Delete two times in main()
int main()
{
Delete(&p,3); //where p points to the first element
Delete(&p,4);
}
Output was: 55 11 66 17 14 99