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