Donate

Example Of IsPalindrome Generic Function Using Queue In C#

Hello,

Excerpt from .NET 4.0 Generic's Guide, there's an example on testing whether a string is Palindrome using a Stack<T> object. I decided to create a a Queue<T> based method. One additional tip was to reverse the Queue object. See sample function below:
public static bool IsPalindromic<T>(IEnumerable<T> inputSequence)   
       where T : IComparable  
     {  
       Queue<T> buffer = new Queue<T>();  
       foreach (T element in inputSequence)  
       {  
         buffer.Enqueue(element); 
       }  
       buffer = new Queue<T>(buffer.Reverse());  
       for (int i = 0; i < inputSequence.Count(); i++)  
       {  
         if (buffer.Dequeue().CompareTo(inputSequence.ElementAt(i)) == 0)  
         {  
           continue;  
         }  
         else  
           return false;  
       }  
       return true;  
     }  
That's It Folks!

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Bootstrap Modal In ASP.NET MVC With CRUD Operations