【问题标题】:c# linked lists, return multiple entriesc#链表,返回多个条目
【发布时间】:2019-03-15 14:58:15
【问题描述】:

我正在尝试创建一个链接列表,类似于鸟类调查类型的东西,并且我试图有一个最终输出,它返回我输入的所有物种以及我输入每个物种的次数。现在输出计算了我输入的每只不同的鸟,但它没有在报告的末尾为我输入的每只鸟单独计数,我不知道该怎么做。我已经为此烦恼了好几个小时了,我觉得我已经很接近了,如果可以请帮助

class Program
{
    public class Node
    {

        /* You add the type of bird and a count of 
         * how many times that bird is said. 
         * Then you use a method to print out 
         * the types of birds and how many times each bird was said*/
        public string bird;
        public Node next;
        public Node(string i)
        {
            bird = i;
            next = null;
        }
        public void getReport()
        {
            Console.Write("|" + bird + "|->");
            if (next != null)
            {
                next.getReport();
            }
        }
        public void AddToEnd(string bird)
        {
            if (next == null)
            {
                next = new Node(bird);
            }
            else
            {
                next.AddToEnd(bird);
            }
        }

        public class BirdList
        {
            public Node headNode;

            public BirdList()
            {
                headNode = null;
            }
            public void AddToEnd(string bird) //method to add to the end of a list 
            {
                if (headNode == null)
                {
                    headNode = new Node(bird);
                }
                else
                {
                    headNode.AddToEnd(bird);
                }
            }
            public void AddToBeginning(string bird) //add to the beginning of a list
            {
                if (headNode == null)
                {
                    headNode = new Node(bird);                      
                }
                else
                {
                    Node temp = new Node(bird);
                    temp.next = headNode;
                    headNode = temp;
                }
            }
            public void getReport()
            {
                if (headNode != null)
                {
                    headNode.getReport();
                }
            }
            public int getCount(string bird)
            {                 
                Node current = headNode;
                int count = 0;
                while (current!= null)
                {
                    if (current.bird == bird)
                    {
                        count++;
                    }
                    current = current.next; 
                }
                return count;
            }

        }
        static void Main(string[] args)
        {
            BirdList birdList = new BirdList();

            string userInput = "";
            while (userInput != "done")
            {
                Console.WriteLine("Please enter a bird:");
                userInput = Console.ReadLine();
                if (userInput == "done")
                {
                    break;
                }
                birdList.AddToEnd(userInput);
                Console.WriteLine(birdList.getCount(userInput));
            }

            birdList.getReport();
            Console.ReadLine();

输出看起来像这样:

【问题讨论】:

  • 首先你真的不需要写你自己的链表。您可以使用LinkedList<T>。其次,我不确定链接列表在这里是否最有意义。除非您需要添加鸟类的顺序,否则我只会使用Dictionary<string, int> 来捕获鸟类和频率。现在,如果这是您需要滚动自己的链接列表的作业,那么请忽略。
  • 不幸的是,分配是这样设置的:/使用 LinkedList 会容易得多

标签: c# linked-list counter


【解决方案1】:

当您运行报告功能时,它似乎没有被指示实际计算它遇到的每个单独项目的数量。

解决这个问题的直接方法是遍历列表并将遇到的每个单独的字符串保存到字典中,然后在发现字典已经包含重复项时增加值。

Node n = birdList.headNode;
Dictionary<string,int> dict = new Dictionary<string,int>();
while(n!=null){
    if(dict.ContainsKey(n.bird))
    {
        dict[n.bird]++;
    }else{
        dict.Add(n.bird,1);
    }
    n=n.next;
}

字典应该包含所有的鸟作为键,它们的数量作为值。

【讨论】:

  • 我已经更新了我的答案,为您提供可能的解决方案
【解决方案2】:

您的代码很好,只是缺少一些东西。一方面,每只鸟都需要一个柜台。一旦它在列表中,也不需要再次添加鸟。我稍微重写了您的代码,并将 cmets 放入您查看。给你:

class Program
    {
        public class Node
        {

            /* You add the type of bird and a count of 
             * how many times that bird is said. 
             * Then you use a method to print out 
             * the types of birds and how many times each bird was said*/
            public string bird;
            public Node next;
            public int count; // each bird needs a counter
            public Node(string i)
            {
                bird = i;
                next = null;
                count = 0;
            }
            public void getReport()
            {
                Console.Write("|" + bird + "|->" +  count );
                if (next != null)
                {
                    next.getReport();
                }
            }
            public void AddToEnd(string bird)
            {
                if (this.bird != bird) // if the bird is already in the list, it wont add it in again.
                {
                    if (next == null)
                    {
                        next = new Node(bird);
                    }
                    else
                    {
                        next.AddToEnd(bird);
                    }
                }
            }

            public class BirdList
            {
                public Node headNode;

                public BirdList()
                {
                    headNode = null;
                }
                public void AddToEnd(string bird) //method to add to the end of a list if bird is not already in the list. 
                {
                    if (headNode == null)
                    {
                        headNode = new Node(bird);
                    }
                    else
                    {
                        headNode.AddToEnd(bird);
                    }
                }
                public void AddToBeginning(string bird) //add to the beginning of a list
                {
                    if (headNode == null)
                    {
                        headNode = new Node(bird);
                    }
                    else
                    {
                        Node temp = new Node(bird);
                        temp.next = headNode;
                        headNode = temp;
                    }
                }
                public void getReport()
                {
                    if (headNode != null)
                    {
                        headNode.getReport();
                    }
                }
                public int getCount(string bird)
                {
                    Node current = headNode;
                    int count = 0;
                    while (current != null)
                    {
                        if (current.bird == bird)
                        {
                            current.count++; // once the bird is found, increment the counter.
                            count = current.count; // set the birds counter to the count.
                        }
                        current = current.next;
                    }

                    return count;
                }

            }
            static void Main(string[] args)
            {
                BirdList birdList = new BirdList();

                string userInput = "";
                while (userInput != "done")
                {
                    Console.WriteLine("Please enter a bird:");
                    userInput = Console.ReadLine();
                    if (userInput == "done")
                    {
                        break;
                    }
                    birdList.AddToEnd(userInput);
                    Console.WriteLine(birdList.getCount(userInput));
                }

                birdList.getReport();
                Console.ReadLine();
            }
        }
    }

【讨论】:

  • 随时!快乐编码!如果您的问题得到回答,请不要忘记标记。它帮助了我:-)
  • 嗯好吧,没问题。感谢您的尝试:-)
  • 它确实说它被记录了 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 2019-12-21
  • 2012-01-07
  • 1970-01-01
相关资源
最近更新 更多