LinkedList类的removeFirst和addLast方法是Unknown

时间:2010-06-02 19:10:41

标签: c#

我的C#中的代码有问题。如果我点击编译器按钮,我会收到以下错误

  

'System.Collections.Generic.LinkedList<int?>'   不包含的定义   'removeFirst'并没有扩展方法   'removeFirst'接受第一个   类型的论证   'System.Collections.Generic.LinkedList<int?>'   可以找到(你错过了吗?   使用指令或程序集   引用?)。

  

'System.Collections.Generic.LinkedList<Hanoi_tower.Sol>'   不包含的定义   'addLast'并没有扩展方法   'addLast'接受第一个参数   类型   'System.Collections.Generic.LinkedList<Hanoi_tower.Sol>'   可以找到(你错过了吗?   使用指令或程序集   引用?)

这是我的程序

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;
     }

}


    }

3 个答案:

答案 0 :(得分:6)

C#区分大小写。正确的方法名称为RemoveFirstAddLast,大写为R和A.

所有.NET方法和属性都在Pascal大小写中完成,其中每个单词的首字母大写。

答案 1 :(得分:5)

看起来你需要正确地大写:RemoveFirst和AddLast

这是LinkedList的所有方法的链接: http://msdn.microsoft.com/en-us/library/h64606bk(v=VS.100).aspx

答案 2 :(得分:2)

C#区分大小写,因此方法名称removeFirstRemoveFirst不一样。您发布的代码段具有正确的capitolization,但您提供的错误没有。哪个是对的?

这是.NET List类'方法的列表。 http://msdn.microsoft.com/en-us/library/h64606bk(v=VS.100).aspx

编辑:

如果您正在寻找类似于堆栈的canonnical pop功能的东西,那么该函数会返回被删除的对象,那么您可能会感到失望。在t2上调用t1.AddFirst(t2[0])之前,您需要首先使用RemoveFirst()之类的内容。

另一个编辑:

好的,这是另一个问题,但是这里你可能会将一个对象从一个列表移动到另一个列表,这是伪代码,因为我很确定这是一个功课问题,我不想交给你在银盘上的答案。

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

每次要将对象从一个列表移动到另一个列表时,都会调用与此类似的函数。如果你不需要指定索引,那么你可以只传递两个列表的函数,并假设索引为0,我指定了变量。