c#链表,返回多个条目

时间:2019-03-15 14:58:16

标签: c# linked-list counter

我正在尝试创建一个链接列表,就像一个“鸟类调查”类型的东西一样,并且我试图在最终输出中返回所有我输入的物种以及每个物种输入了多少次。现在,输出会计算我输入的每只不同的鸟,但是在报告末尾,我输入的每只鸟都不会单独计数,我不确定该怎么做。我已经忙了好几个小时了,我感觉我已经很亲密了,如果可以的话请<3 ...这是代码:

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();

输出看起来像这样: image

2 个答案:

答案 0 :(得分: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;
}

字典应包含所有鸟作为键,其数量作为值。

答案 1 :(得分:0)

您的代码很好,只是缺少了一些东西。首先,每只鸟需要一个计数器。同样,也无需在列表中再次添加该鸟。我稍微重写了您的代码,并在其中添加了注释供您查看。你去了

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