Saturday, January 29, 2011

How to detect a loop in a single linked list

Continue my topic regarding linked list, I couple days ago , after I create the linked list class, I was thinking , in a single list, if one node point to a node prior to itself in the list, then it create a loop.  Then when you traverse the linked list, Ooops! you get into a dead loop.
So I was thinking How to detect this loop situation, then I did some research over internet, I do find quite a lot good articles.  here is one,  http://ostermiller.org/find_loop_singly_linked_list.html it explain good solutions and bad solutions.
this is another one explain how to address the culprit which cause the loop http://crackinterviewtoday.wordpress.com/2010/03/16/calculate-node-that-causes-loop-in-a-linked-list/

Today,I get some time , so I implement the function , here is the code.
Not only this function can address the loop, but also , it can address the exact node that cause it.


public bool hasLoop()
        {
            bool bReturn = false;
            
            if(_root != null && _root.next != null)
            {
                Node slow = _root;
                Node fast1 = Root;
                Node fast2 = Root;
                while(slow != null)
                {
                    fast1 = fast2.next;
                    if(fast1 != null)
                    {
                        fast2 = fast1.next;
                        if(fast1 == slow || fast2 == slow) //we have a loop
                        {
                            bReturn = true;
                            break;
                        }
                        else if(fast2 == null)//fast2 reach the end of linked list.
                        {
                            break;
                        }
                    }
                    else //fast1 is null means we reach the end of the linked list.
                    { 
                        break;
                    }
                    slow = slow.next;
                }
                slow = slow.next;
                Console.WriteLine(string.Format("Node:{0} cause a loop...",slow.Name));
            }
            
            return bReturn;
        }

Silverlight drag and drop issue

This week , I upgrade our project to silverlight 4, you know silverlight 4 is on the market for a long time, but unfortunately , we are still using silverlight 3.0, constrained by the infrastructure. cause it is not easy to change our build server.  finally now it is silverlight 4.

Once I finish up the upgrade, I came across a drag and drop issue. In silverlight 4, when I move mouse on the top of a ListBoxDragDropTarget, even me mouse left key is not down, but sometimes, it just start to drag....Which might cause a lot problem.  after some search , it turns out it is a bug of silverlight 4 toolkit, and I found a solution here  http://silverlight.codeplex.com/workitem/5930 , this solution works for me.

Wednesday, January 26, 2011

How to reverse a linked list

If you go for an interview and the position which you are interviewing for is r&d focus, then most likely they will ask you some very basic technologies,data structures.  Which you might learned from your uni, but after so many years of working, you almost forget about it.

Recently I came across a question: how to reverse a linked list?

Linked list is quite easy ,  if you don't know about it , you can go to http://en.wikipedia.org/wiki/Linked_list to read more.

From the question you can know , that the linked list which require you to reverse is single linked list.  cause double linked list, there is no point to reverse.

They give me a marker , and ask me to write down the function on a white board. I spend around 10 minutes and didn't get it right, then they lost patience.   the problem for me , is  I am not used to write any code on a board....

So today I create a function to reverse a linked list. Include build the linked list, traverse , and reverse , it totally take me around half an hour in front of my PC.

First of all , we need to define a class which can be used to represent a linked list.

internal class Node
{
public Node(string strName)
{
this._name = strName;
}

private string _name = string.Empty;
public string Name
{
get{return this._name;}
set{this.Name = value;}
}

private Node _next = null;
public Node next
{
get{return this._next;}
set{this._next = value;}
}

public void setNext(Node node)
{

this.next = node;

}
}