Query related to Single LinkedList?

Hi All

I have a single llist, let say with 50 links.

Some kind of corruption have occured in my llist such that the 35th link instead of pointing to the 36th link now points to the 7th link.

Now if i have to trace the entire llist for some value which is say present at the 40th link.

Since the llist is now corrupted tracing the llist will now go into infinite loop as the 36th link will send me back to the 7th link.

Now programmatically how do i identify that some corruption has occured in my llist & rectify it.

Waiting for suggestions

Regards
[611 byte] By [dp_76] at [2007-11-20 11:00:08]
# 1 Re: Query related to Single LinkedList?
As to identifying a loop in the list - you can check this (http://www.dev-archive.com/forum/showthread.php?t=416383&highlight=list) thread. See especially Yves M's post.
As for rectifying - if you don't somehow keep track of all nodes in the list then this is not possible since you can't track the rest of the list (after node 36). If you keep track of all nodes added to the list in another data structure, lets say a vector with pointers to all the nodes, or a hash table or a tree - then you can map all nodes that are ok - appear before the loop, identify the 1st node that has a backwards next pointer and point it to the node that is not pointed by any other node's next pointer (in your case, node 37).

Best of luck,

Zachm
Zachm at 2007-11-9 1:24:58 >
# 2 Re: Query related to Single LinkedList?
Hi
The link helped me.

Thanx & Rgds
:wave:
dp_76 at 2007-11-9 1:25:53 >
# 3 Re: Query related to Single LinkedList?
we can use this code also to find loop in list.

t1= first;
t2 = first;
while(t2)
{
t2 = t2->next;
if( t2 == t1 )
break;
}
if(t2 != NULL)
std::cout<<std::endl<<"Your List contains loop !!"<<std::endl;

Complete program can be like this

#include <iostream>
#include <string>

struct node
{
int data;
node *next;
};
class TestClassList
{
node *front,*rear;
node *createnode(int n);
public:
TestClassList(){front = rear = NULL;}
void insertnode(int n);
void createloop(int n);
void checkloop();
};

node * TestClassList::createnode(int n)
{
node *temp = new node;
temp->data = n;
temp->next = NULL;
return temp;
}
void TestClassList::insertnode(int n)
{
if( front == NULL)
front = rear = createnode(n);
else
{
node *temp = rear;
rear = createnode(n);
temp->next = rear;
}
}

void TestClassList::createloop(int n)
{
node *t = front;
int i=1;
while(t && i < n)
{
t = t->next;
i++;
}
t->next = front;
}

void TestClassList::checkloop()
{
std::cout<<std::endl<<"Nodes in Loop are "<<std::endl;
node *t1,*t2;
t1=t2=front;
while(t2)
{
std::cout<<t2->data<<" ";
t2 = t2->next;
if(t2 == t1)
break;
}
if(t2 != NULL)
std::cout<<std::endl<<"Your List contains loop !!"<<std::endl;
}

int main()
{
TestClassList lo;
int i,n;
for(i=0;i<10;i++)
lo.insertnode(i);
lo.checkloop();
std::cout<<std::endl<<"Enter the node number from where you want to make loop : ";
std::cin>>n;
lo.createloop(n);
std::cout<<std::endl;
lo.checkloop();
return (1);
}

Output

//Nodes Before any Loop in your list
Nodes in Loop are
0 1 2 3 4 5 6 7 8 9

Enter the node number from where you want to make loop : 6

//Nodes After loop exist in your list
Nodes in Loop are
0 1 2 3 4 5
Your List contains loop !!

Press ENTER to continue.
nitin1979 at 2007-11-9 1:26:54 >
# 4 Re: Query related to Single LinkedList?
If it were me, I'd be spending time tracking down and fixing how your list gets corrupted, rather than putting effort into recovery. It isn't very difficult to write a stable list class. Perhaps you could even use the std::list instead.
GCDEF at 2007-11-9 1:27:59 >