Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to write function for the reversion of the circular kind linked list !! i have read a thing and i understand using 3 pointers and i am finding it particularly difficult also NOT working , so any alternative with only using "last" or i mean not using 3 pointers and still reversing !!!

What I have tried:

struct node
{
	int data;
	struct node *next;
}*last = NULL, *p, *q, *rear = NULL, *x;

//Reverse Function , difficult to understand and yet not giving results
void circular_llist::reverse()
{
	x = last->next;
	p = last;
	last->next = NULL;
	while (x)
	{
		q = x->next;
		x->next = p;
		last = x;
		p = x;
		x = q;
	}

}

//Main
C++
int main()
{
	circular_llist cl;
      cl.reverse();
}
Posted
Updated 19-Dec-16 13:39pm

Use the debugger. That's part of what it is there for.
Put a breakpoint at the start of the function, and step through look at closely at what is going on.
What may help you understand is if you draw out on paper first what the list looks like - set up a simple one with say 4 nodes - then mark it with what it should look like when you are finished. As it gets changed while you run your code in the debugger, mark up your drawing to show the new state. If should be reasonably obvious once you get into it what is happening, and why it's going wrong.
 
Share this answer
 
Suppose you have p,q,r, pointing to three consecutive nodes, namely
C++
q = p->next;
r = q->next;

the reverse-and-update step is then
C++
q->next = p; // reverse
// update
p = q; 
q = r;
r = r->next;


The stop condition is q being equal to the head of the list.
 
Share this answer
 
v2
Comments
mayashah 19-Dec-16 22:57pm    
what is n what is r ??
update the above function for ease thankx
CPallini 20-Dec-16 2:26am    
Sorry, I've updated my answer. 'n' was a mistype for 'next'. 'r' is, together with 'p' and 'q', an auxiliary pointer.
Quote:
//Reverse Function , difficult to understand and yet not giving results
Normal, you are messing with pointers until the end of the list, problem the list is circular list, there no end!

As uou have been told multiple times, use the debugger and see what your code is doing.
You want advice but don't follow them. Looks like you want full blowup solutions with minimum effort.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to simulate your algorithm by hand, your program should use the same procedure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900