954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

recursively delete Element at given Position from a doubly linked List

Hey Guys,

I am really trying to get into List-Implementation.
Everything I wrote works fine except the "delete Element at Position" funktion.
With the iterative Implementation there is no problem, everything works, but I want to do a recursive Version.

This is the Task:
Invent an algorithm which becomes a doubly linked list (L) and a Position (n) and deletes the Element at the given Position recursively.

I really need your help. Hope you have a solution.

Greetings

eRayven

eRayven38
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

find position n - 1. Call it temp. Then write a function that returns nothing, takes no parameters, and deletes temp->next, as long as temp->next isn't NULL.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Sounds good. But I thing I do also have to change the pointers Prev and next don't I?
And it is a special Problem if the Element being deleted is the first Element or the end auf the List, isn't it?

I am trying now for about 5 hours and I think I am miles away from a working solution. Will be a long night with lots of coffee.

eRayven38
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I don't understand the assignment. Deleting an element from a doubly linked list is not inherently an iterative operation, which means that there is no obvious way to transform it into a recursive operation.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

It's an Assignment of an exam from last year ans i really don't get an Idea how to do it.

eRayven38
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

The question isn't very clear to me neither.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 
My first idea was:

LinkedList *delete(LinkedList *L, usigned int n){

i(n<1 || head==NULL) //empty list
return NULL;

else if(n==1) //first Element being deleted
node=head;
head=head.next;
head.prev=NULL;
delete node;
return L;

else if(L.next==NULL) //delete tail
node=tail;
tail=tail.prev;
tail.next=NULL;
delete node;
return L;

else if (n==1) //delete Element from the middle
node=L;
node.prev->next=node.next;
node.next->prev=node.prev;
delete node;
return L;

else //rekursion
return delete(L.next, n-1);


}
eRayven38
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

oh no... if statement (n==1) two times. -.-
But I do not know how to elude.

eRayven38
Newbie Poster
5 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

what are head and tails.....? i cant understand that...for what instead u use it ?.......i think they are instead of t,t1......positons......can u pls explain it clearly? and i want delete by position and delete by value functions of double linked list program

deepu.sri24
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: