简体   繁体   中英

c# linked lists, return multiple entries

I am trying to create a linked list, with like a Bird Survey type thing, and I am trying to have an end output where it returns all the species I've entered and how many times I've entered each one. Right now the output counts each different bird that I enter, but it doesn't give a separate count at the end of the report for each one that I've entered and I'm not sure how to do that. I've been fussing with this for hours now, I feel like I'm super close, please help if you can <3... Here is the code:

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

And the output looks something like this: 图片

when you run the report function it does not seem to be instructed to actually count the amount of every individual item it encounters.

The straightforward way of solving this is to iterate over the list and to save every individual string it encounters into a dictionary, and then to increment the value when you find that the dictionary already contains a duplicate.

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

The dict should contain all the birds as key, with their amounts as values.

Your code was good, it was just missing a few things. For one, each bird needs a counter. Also there is no need to add the bird in again once it is in the list. I rewrote your code a bit, and placed comments in for you to see. here you go:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM