In: Computer Science
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;
}
}
}
public int countGroup(T data_)
{
int count=0;
Node pTmp = first;
while (pTmp.Next != null && pTmp.Next.Next != null)
{
if((pTmp.Data).Equals(data_))
{
count++;
}
pTmp = pTmp.Next;
}
return count;
}