Pages

Monday, 3 October 2011

cs301 Assignment no 1 Solution Fall 2011


Here is the proposed implementation of squish and twin method


List squish(List list)

{

            list.currentNode = list.headNode;
            Node * PrevNode = list.headNode;                // previous to dublicate Node
            Node * dublicateNode  =  list.headNode->getNext();
    int currentValue;
            int dublicateValue;


    while(list.next())

                        {
                                    currentValue = list.get();
                                    PrevNode = list.currentNode;
                                    dublicateNode = list.currentNode->getNext();
                                   

                                    while(dublicateNode != NULL)
                                   
                                    {
                                               
                                                dublicateValue = dublicateNode->get();
                                   
                                    if (currentValue == dublicateValue)
                                               
                                    {
                       
                                               
                                    if( dublicateNode != NULL && dublicateNode != list.headNode)
                                   
                                                {
                                                            PrevNode->setNext(dublicateNode->getNext());
                                                            delete dublicateNode;
                                                            dublicateNode = PrevNode->getNext();
                                   
                                                }


                                    }          
           
                                    else

                                    {
                                   
                                                PrevNode = dublicateNode;
                                                dublicateNode = dublicateNode->getNext();
                                   
                                    }
                                   
                                   
                                    }

           
           
            }
                                   
            return list;


}