I have this problem where you have a list of odd or even number of items in doubly link list and you have delete the middle one. For the even number one you delete the two middle elements, say you have 1,2,3,...,10 you delete 5 and 6, and for the odd one say for 1,2,3,4,5 you delete 3.

The problem is that you do not know whether the list is even or odd and you have to delete the middle element(s). I have to write a pseudo-code procedure to delete the middle (one or two) record(s) of a doubly linked list without counting first or knowing the total number of elements in the list. So what approach can I use to do this?

Recommended Answers

All 2 Replies

You could try using 2 pointers, where you increment the first pointer by 1 and the second pointer by 2. That way when the second pointer reaches the end of the list, the first one is at the middle. You can figure out whether its odd or even by making a note of how much you have to increment the second pointer by just before it reaches the end of the list.

Or,

you can use 2 pointers pointing to the first and last element of the list and then increment 1 and decrement the other. Until we get to the centre of the list . then delete that particular node(s)

This way we dont count or, know how many elments we have.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.