c# -删除链表中的项

本文关键字:链表 删除 | 更新日期: 2023-09-27 18:15:46

好吧,我有一个问题,我只是似乎不能得到这个工作,无论我尝试什么。问题是每次我写控制台我得到一个错误:

参数1:不能从'void'转换为'bool'

但是,除了条件之外,我看不到布尔数据。

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

谢谢。

c# -删除链表中的项

你有问题

 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(如果item已被删除):

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

你已经触及你自己的问题了。您正在尝试在这一行打印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;
}