circular linked list - delete ith node

I have an examination coming up in the next few weeks and having looked over some of the previous papers I have come across a question that keeps appearing which I am unable to answer and I would be very grateful for your help.

Write a function that deletes the ith node from a circular linked list !!

I have no problems deleting a node in two way linked and was wondering if someone could have a look at my code and let me know if I am tackling this problem in the correct way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

void circularList::deleteNode(int x)
{
      node *current;
      node *temp;
      current = this->start;

      while(current->next != this->start)
      {
	if(current->next->value == x)
	{
             temp = current->next;
	     current->next = current->next->next;
	     delete current->next;
	 }	
  	     		
      }
}
can someone give me a hint as to whether i am doing this correctly?
You need a counter that starts at zero and is incremented for each link you visit.

If the counter == x, you have the node you need to delete.
Last edited on
how would i iterate through the circular list using a counter?
You don't use the counter to iterate; you use the counter to count the number of times you've iterated.
in that case you'd probably want to remove the while loop, depending on how your list functions. EX: if you have 5 nodes and you want to delete the 7th node, then the while loop would prevent that. I'm not sure if that's proper behavior for the list you're being tested on though. You'd just use a for loop instead.
Topic archived. No new replies allowed.