C# - 删除链接列表的项目

时间:2016-10-30 19:27:21

标签: c# list

好吧,我遇到了问题,无论我尝试什么,我似乎无法让它发挥作用。问题是我每次写入Console时都会收到错误:

  

参数1:无法转换为' void'到了布尔'

但是,根据条件,我没有布尔数据。

    class Link
{
    private int data;
    private Link next;

    public Link(int item) //constructor with an item
    {
        data = item;
        next = null;
    }
    public Link(int item, Link list) //constructor with item and list
    {
        data = item;
        next = list;
    }

    public int Data //property for data
    {
        set { this.data = value; }
        get { return this.data; }
    }

    public Link Next //property for next
    {
        set { this.next = value; }
        get { return this.next;}
    }

}

}

我所有其他方法都可以工作,即显示项目,计算项目数量等。但似乎问题是什么:

class LinkList
{
    private Link list = null; //default value – empty list

    public void AddItem(int item) //add item to front of list
    {
        list = new Link(item, list);
    }

    public string DisplayItems() //write items to string and return
    {
        Link temp = list;
        string buffer = "";
        while (temp != null) // move one link and add head to the buffer
        {
            buffer += temp.Data + "";
            temp = temp.Next;
        }
        return buffer;
    }

    public int NumberOfItems() // returns number of items in list
    {
        Link temp = list;
        int count = 0;
        while (temp != null) // move one link and add 1 to count
        {
            count++;
            temp = temp.Next;
        }
        return count;
    }



    public void RemoveItem(int item)
    {
        Link current = list;
        Link previous = null;

        while (current != null)
        {
            if (current.Data == item)
            {
                if (previous != null)
                {

                    previous.Next = current.Next;
                    current = current.Next;

                }


                else
                {
                    previous = current;
                    current = current.Next;
                    list = current;
                }
            }
            else
                previous = current;
               current= current.Next;
        }

    }





    public bool IsPresent(int item)
    {
        Link temp = list;
        bool result = false;
        while (temp != null)
        {
            if (temp.Data == item)
            {
                result = true;
                break;

            }

            else
            {
                temp = temp.Next;

            }
        }
        return result;
    }

}

这是我用来打印到控制台的程序类 -

    class Program
{
    static void Main(string[] args)
    {
        LinkList testList = new LinkList();
        testList.AddItem(10);
        testList.AddItem(20);
        testList.AddItem(30);
        testList.AddItem(40);
        testList.AddItem(50);
        testList.AddItem(60);

        System.Console.WriteLine("Number of Items " + testList.NumberOfItems());
        System.Console.WriteLine("Display Items " + testList.DisplayItems());
        System.Console.WriteLine("10 is Present " + testList.IsPresent(10));
        System.Console.WriteLine("20 is Present " + testList.IsPresent(20));
        System.Console.WriteLine("30 is Present " + testList.IsPresent(30));
        System.Console.WriteLine("40 is Present " + testList.IsPresent(40));
        System.Console.WriteLine("50 is Present " + testList.IsPresent(50));
        System.Console.WriteLine("60 is Present " + testList.IsPresent(60));
        System.Console.WriteLine(testList.RemoveItem(10));






        System.Console.ReadLine();
        Console.ReadKey();
    }

}

但是RemoveItem方法让我感到悲伤,有人可以帮助我吗?谢谢。

这是给我一个问题的代码 -

    public void RemoveItem(int item)
    {
        Link current = list;
        Link previous = null;

        while (current != null)
        {
            if (current.Data == item)
            {
                if (previous != null)
                {

                    previous.Next = current.Next;
                    current = current.Next;

                }


                else
                {
                    previous = current;
                    current = current.Next;
                    list = current;
                }
            }
            else
                previous = current;
               current= current.Next;
        }

    }

谢谢。

2 个答案:

答案 0 :(得分:6)

您遇到问题

 System.Console.WriteLine(testList.RemoveItem(10));

因为RemoveItem没有返回任何值:

 // void - the method doesn't return any value
 public void RemoveItem(int item) 
 {
   ...
 }    

你无法打印出来。只需致电RemoveItem,不要WriteLine

 testList.RemoveItem(10);

更好的方法是重新设计RemoveItem并让它返回bool(如果项已被移除):

 public bool RemoveItem(int item) {
   ... 
 }

答案 1 :(得分:3)

你已经触及了自己的问题。您正尝试在此行上打印RemoveItem的输出:

System.Console.WriteLine(testList.RemoveItem(10));

但是RemoveItem 没有 任何输出,因为它的返回值为void。但void并不意味着“忽略这种方法”。 Console.WriteLine仍然期望一个值,但它给出的值不是它可以做任何事情,因此错误。

您需要删除打印电话:

RemoveItem(10);
System.Console.Writeline("10 removed.");

或更改RemoveItem以返回值:

public bool RemoveItem(int item)
{
    ...
    return true;
}