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

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input