tags:

views:

147

answers:

3

I have a problem with my code in C# . if i click in compiler button , I get the following errors

'System.Collections.Generic.LinkedList<int?>' does not contain a definition for 'removeFirst' and no extension method 'removeFirst' accepting a first argument of type 'System.Collections.Generic.LinkedList<int?>' could be found (are you missing a using directive or an assembly reference?).

and

'System.Collections.Generic.LinkedList<Hanoi_tower.Sol>' does not contain a definition for 'addLast' and no extension method 'addLast' accepting a first argument of type 'System.Collections.Generic.LinkedList<Hanoi_tower.Sol>' could be found (are you missing a using directive or an assembly reference?)

This is my program

using System.;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hanoi_tower
{
   public class Sol
    {

public LinkedList<int?> tower1 = new LinkedList<int?>();
   public LinkedList<int?> tower2 =new LinkedList<int?>();
   public LinkedList<int?> tower3 =new LinkedList<int?>();
   public int depth;

   public LinkedList<Sol> neighbors;

    public Sol(LinkedList<int?> tower1, LinkedList<int?> tower2, LinkedList<int?> tower3)
    {
        this.tower1 = tower1;
        this.tower2 = tower2;
        this.tower3 = tower3;

        neighbors = new LinkedList<Sol>();
    }

    public virtual void getneighbors()
    {

        Sol temp = this.copy();
        Sol neighbor1 = this.copy();
        Sol neighbor2 = this.copy();
        Sol neighbor3 = this.copy();
        Sol neighbor4 = this.copy();
        Sol neighbor5 = this.copy();
        Sol neighbor6 = this.copy();


        if (temp.tower1.Count != 0)
        {

            if (neighbor1.tower2.Count != 0)
            {
                if (neighbor1.tower1.First.Value < neighbor1.tower2.First.Value)
                {
                    neighbor1.tower2.AddFirst(neighbor1.tower1.RemoveFirst);
                    neighbors.AddLast(neighbor1);
                }

            }
            else
            {
                neighbor1.tower2.AddFirst(neighbor1.tower1.RemoveFirst());
                neighbors.AddLast(neighbor1);
            }



            if (neighbor2.tower3.Count != 0)
            {
                if (neighbor2.tower1.First.Value < neighbor2.tower3.First.Value)
                {
                    neighbor2.tower3.AddFirst(neighbor2.tower1.RemoveFirst());
                    neighbors.AddLast(neighbor2);
                }
            }
            else
            {
                neighbor2.tower3.AddFirst(neighbor2.tower1.RemoveFirst());
                neighbors.AddLast(neighbor2);
            }

        }




        //-------------

        if (temp.tower2.Count != 0)
        {


            if (neighbor3.tower1.Count != 0)
            {
                if (neighbor3.tower2.First.Value < neighbor3.tower1.First.Value)
                {
                    neighbor3.tower1.AddFirst(neighbor3.tower2.RemoveFirst());
                    neighbors.AddLast(neighbor3);
                }
            }
            else
            {
                neighbor3.tower1.AddFirst(neighbor3.tower2.RemoveFirst());
                neighbors.AddLast(neighbor3);
            }



            if (neighbor4.tower3.Count != 0)
            {
                if (neighbor4.tower2.First.Value < neighbor4.tower3.First.Value)
                {
                    neighbor4.tower3.AddFirst(neighbor4.tower2.RemoveFirst());
                    neighbors.AddLast(neighbor4);
                }
            }
            else
            {
                neighbor4.tower3.AddFirst(neighbor4.tower2.RemoveFirst());
                neighbors.AddLast(neighbor4);
            }


        }


        //------------------------

        if (temp.tower3.Count() != 0)
        {


            if (neighbor5.tower1.Count() != 0)
             {
                if(neighbor5.tower3.ElementAtOrDefault() < neighbor5.tower1.ElementAtOrDefault())
                {
                    neighbor5.tower1.AddFirst(neighbor5.tower3.RemoveFirst());
                neighbors.AddLast(neighbor5);
                }
             }
                 else
                 {
             neighbor5.tower1.AddFirst(neighbor5.tower3.RemoveFirst());
             neighbors.AddLast(neighbor5);
                 }



            if (neighbor6.tower2.Count() != 0)
             {
                if(neighbor6.tower3.element() < neighbor6.tower2.element())
                {
                    neighbor6.tower2.addFirst(neighbor6.tower3.removeFirst());
                 neighbors.addLast(neighbor6);
            }
             }
                else
                {
             neighbor6.tower2.addFirst(neighbor6.tower3.removeFirst());
             neighbors.addLast(neighbor6);
                }

            }
    }

        public override string ToString()
{

    string str;

    str="tower1"+ tower1.ToString() + "   tower2" + tower2.ToString() + "   tower3" + tower3.ToString();


    return str;

}


        public Sol copy()

{

    Sol So;
     LinkedList<int> l1= new LinkedList<int>();
     LinkedList<int> l2=new LinkedList<int>();
     LinkedList<int> l3 = new LinkedList<int>();


     for(int i=0;i<=this.tower1.Count() -1;i++)
     {

         l1.AddLast(tower1.get(i));

     }



      for(int i=0;i<=this.tower2.size()-1;i++)
     {

         l2.addLast(tower2.get(i));

     }

      for(int i=0;i<=this.tower3.size()-1;i++)
     {

         l3.addLast(tower3.get(i));

     }


      So = new Sol(l1, l2, l3);
     return So;

}



        public bool Equals(Sol sol)

{

    if (this.tower1.Equals(sol.tower1) & this.tower2.Equals(sol.tower2) & this.tower3.Equals(sol.tower3))
        return true;

    return false;

}


        public virtual bool containedin(Stack<Sol> vec)
     {

         bool found =false;

         for(int i=0;i<= vec.Count-1;i++)
         {
            if(vec.get(i).tower1.Equals(this.tower1) && vec.get(i).tower2.Equals(this.tower2) && vec.get(i).tower3.Equals(this.tower3))
            {
                found=true;
              break;
         }

         }

         return found;
     }

}


    }
+5  A: 

It looks like you need to capitalize correctly: RemoveFirst and AddLast

Here's a link to all the methods of LinkedList: http://msdn.microsoft.com/en-us/library/h64606bk(v=VS.100).aspx

CubanX
+6  A: 

C# is case-sensitive. The correct method names are RemoveFirst and AddLast, with a capital R and A.

All .NET methods and properties are done in Pascal casing, where the first letter of each word is capitalized.

R. Bemrose
Incidentally, Java uses camel-case, and its `LinkedList` has a `removeFirst`.
R. Bemrose
FWIW, it's typically called PascalCase in C# due to Anders :) - http://www.c2.com/cgi/wiki?PascalCase - http://blogs.msdn.com/b/brada/archive/2004/02/03/67024.aspx
James Manning
Additionally, RemoveFirst returns a void, and where he's using it he's adding a T to the list, so he's trying to add a void to the list... I don't think that'll work like he wants. Unless those add the appropriate t[1|2|3] kind of like how `x[i].value = i--` vs `x[i].value = --i` are different behaviors...
drachenstern
@James Manning: Whoops, corrected that name.
R. Bemrose
+2  A: 

C# is case sensitive, so the method names removeFirst and RemoveFirst are not the same. The code snippet you posted has the correct capitolization, but the errors you provided do not. Which is correct?

This is a listing of the .NET List class' methods. http://msdn.microsoft.com/en-us/library/h64606bk(v=VS.100).aspx

Edit:

If you are looking for something similar to the canonnical pop functionality of a stack, where the object being removed is returned by the function, then you may be disappointed. You will need to first use something like t1.AddFirst(t2[0]) before you call RemoveFirst() on t2.

Another Edit:

Okay, this is really another question, but here's the way you might go about moving an object from one list to another, in pseudocode since I'm pretty sure this is a homework question and I don't want to hand you the answer on a silver platter.

Function MoveObject(list1, index1, list2, index2)  
    add object at index1 in list1 to list2 at index2
    remove object at index2 from list2

You would call a function similar to that each time you want to move an object from one list to another. If you don't need to specify indices, then you could just pass the function two lists and have it assume index 0 where I've specified variables.

Caleb Thompson
his code is incomplete. I imagine it's further down.
drachenstern
I could not solve the problem ... I modify the question and raise the full program
Agreed at the stacklike behavior needs to be revised on the program. Encapsulation as a method may be better than dual line code snippets everywhere.
drachenstern
:( :( :( :( :( help me,plz
I've modified the answer with some pseudocode for how you might want to do that. Throw that function coded in your language at the end and call it, and it ought to work alright.
Caleb Thompson
Idid not understand your speech .. You have to implement all means, but the program does not work with me and show me the same errors
UP ^ ^ ^ ^ ^ ^ ^ ^ ^ ^