Question

In: Computer Science

Add a CountGroups method to the linked list class below (OurList). It returns the number of...

Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values.

Examples using strings:
A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one
   CountGroup(“one”) prints 4 groups of one's
   CountGroup(“dog”) prints 3 groups of dog's

Do not turn in the code below. Turn in only your method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinkListExample
{
    public class OurList<T>
    {
        private class Node
        {
            public T Data { get; set; }
            public Node Next { get; set; }
            public Node(T paramData = default(T), Node paramNext = null)
            {
                this.Data = paramData;
                this.Next = paramNext;
            }
        }

        private Node first;

        public OurList()
        {
            first = null;
        }

        public void Clear()     // shown in class notes
        {
            first = null;
        }

        public void AddFirst(T data)     // shown in class notes
        {
            this.first = new Node(data, this.first);
        }

        public void RemoveFirst()     // shown in class notes
        {
            if (first != null)
                first = first.Next;
        }

        public void AddLast(T data)     // shown in class notes
        {
            if (first == null)
                AddFirst(data);
            else
            {
                Node pTmp = first;
                while (pTmp.Next != null)
                    pTmp = pTmp.Next;

                pTmp.Next = new Node(data, null);
            }
        }

        public void RemoveLast()     // shown in class notes
        {
            if (first == null)
                return;
            else if (first.Next == null)
                RemoveFirst();
            else
            {
                Node pTmp = first;
                while (pTmp.Next != null && pTmp.Next.Next != null)
                    pTmp = pTmp.Next;

                pTmp.Next = null;
            }
        }

        public void Display()     // shown in class notes
        {
            Node pTmp = first;
            while (pTmp != null)
            {
                Console.Write("{0}, ", pTmp.Data);
                pTmp = pTmp.Next;
            }
            Console.WriteLine();
        }

        public bool IsEmpty()     // shown in class notes
        {
            if (first == null)
                return true;
            else
                return false;
        }
     }
}

Solutions

Expert Solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    class OurList<T>
    {
        private class Node
        {
            public T Data { get; set; }
            public Node Next { get; set; }
            public Node(T paramData = default(T), Node paramNext = null)
            {
                this.Data = paramData;
                this.Next = paramNext;
            }
        }

        private Node first;

        public OurList()
        {
            first = null;
        }

        public void Clear()     // shown in class notes
        {
            first = null;
        }

        public void AddFirst(T data)     // shown in class notes
        {
            this.first = new Node(data, this.first);
        }

        public void RemoveFirst()     // shown in class notes
        {
            if (first != null)
                first = first.Next;
        }

        public void AddLast(T data)     // shown in class notes
        {
            if (first == null)
                AddFirst(data);
            else
            {
                Node pTmp = first;
                while (pTmp.Next != null)
                    pTmp = pTmp.Next;

                pTmp.Next = new Node(data, null);
            }
        }

        public void RemoveLast()     // shown in class notes
        {
            if (first == null)
                return;
            else if (first.Next == null)
                RemoveFirst();
            else
            {
                Node pTmp = first;
                while (pTmp.Next != null && pTmp.Next.Next != null)
                    pTmp = pTmp.Next;

                pTmp.Next = null;
            }
        }

        public void Display()     // shown in class notes
        {
            Node pTmp = first;
            while (pTmp != null)
            {
                Console.Write("{0}, ", pTmp.Data);
                pTmp = pTmp.Next;
            }
            Console.WriteLine();
        }

        public bool IsEmpty()     // shown in class notes
        {
            if (first == null)
                return true;
            else
                return false;
        }

    
    public int CountGroups(T search) {
        if(first == null) {
            return 0;
        }
        Node tmp = first;
        
        int count = 0;
        T last = default(T);
        while(tmp != null) {
            if(tmp.Data.Equals(search)) {
                // if this was same as what we got previously, then do 
                // not increment group count.
                if(!tmp.Data.Equals(last)) {
                    count++;
                }
            }
            last = tmp.Data;
            tmp = tmp.Next;
        }
        return count;
    }
     }


class MainClass {
  public static void Main (string[] args) {
    OurList<String> l = new OurList<String>();

    l.AddLast("one");
    l.AddLast("one");
    l.AddLast("dog");
    l.AddLast("dog");
    l.AddLast("one");
    l.AddLast("one");
    l.AddLast("one");
    l.AddLast("dog");
    l.AddLast("dog");
    l.AddLast("dog");
    l.AddLast("dog");
    l.AddLast("one");
    l.AddLast("one");
    l.AddLast("dog");
    l.AddLast("one");

    Console.WriteLine (l.CountGroups("one"));
    Console.WriteLine (l.CountGroups("dog"));
  }
}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
In C++, type a method getSmallest(), which returns the smallest number in the following linked list....
In C++, type a method getSmallest(), which returns the smallest number in the following linked list. 8->4->6->7->5 (8 is the head).
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question...
Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question 2: Write a member method getPosition(int entry) which returns the position of the entry is in the linked list. If the entry is not in the list, return -1. Please use C++ language for both questions, I only need functions.
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // slist.cpp #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to the front of...
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
Add a method to OurQueue class that dequeues the Nth item on a queue and returns...
Add a method to OurQueue class that dequeues the Nth item on a queue and returns it. It must remove the Nth item from the Queue but leave the rest of the queue. Test it with the following code: Console.WriteLine("Testing DequeueNth"); OurQueue<string> ourQ = new OurQueue<string>(12); // Empty Q try { ourQ.DequeueNth(0); Console.WriteLine("\a Error on empty list"); } catch { Console.WriteLine("Empty Queue worked"); } for (int i = 0; i < 9; ++i) ourQ.Enqueue("a" + i); for (int i =...
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Using python. Produce a method for a linked list that is called FIND , which returns...
Using python. Produce a method for a linked list that is called FIND , which returns the index of a lookup value within the linked list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT