コード例 #1
0
        // Return a string representing this
        // cell and those that follow.
        public override string ToString()
        {
            string result = "";

            for (LetterCell cell = this; cell != null; cell = cell.Next)
            {
                result += cell.Letter;
            }
            return(result);
        }
コード例 #2
0
 // Return true if the cells after this one form a palindrome.
 public bool IsListPalindrome()
 {
     // Check each cell for a palindrome.
     for (LetterCell cell = this.Next; cell != null; cell = cell.Next)
     {
         if (IsPalindromeAtCell(cell))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #3
0
        // See if the string is a palindrome.
        private void checkButton_Click(object sender, EventArgs e)
        {
            // Make the string into a list.
            string txt = stringTextBox.Text.ToUpper();

            txt = RemoveNonLetters(txt);
            LetterCell sentinel = new LetterCell(txt);

            // Check for a palindrome in various ways.
            if (sentinel.IsListPalindrome())
            {
                atCellLabel.Text = "Is a palindrome";
            }
            else
            {
                atCellLabel.Text = "Not a palindrome";
            }
        }
コード例 #4
0
        // Return true if the cells before and after this
        // one form a palindrome of odd length.
        public bool IsEvenPalindromeAtCell(LetterCell cell)
        {
            // Go forward and backward comparing letters.
            LetterCell NextCell = cell;
            LetterCell PrevCell = cell;

            while ((NextCell != null) && (PrevCell != null))
            {
                if (NextCell.Letter != PrevCell.Letter)
                {
                    return(false);
                }
                NextCell = NextCell.Next;
                PrevCell = PrevCell.Prev;
            }
            if ((NextCell != null) || (PrevCell != null))
            {
                return(false);
            }
            return(true);
        }
コード例 #5
0
        // Make a list to represent a string.
        public LetterCell(string txt)
        {
            // Create the end sentinel.
            LetterCell endSentinel = new LetterCell();

            // Make it into a list.
            LetterCell firstCell = endSentinel;

            foreach (char ch in txt)
            {
                LetterCell cell = new LetterCell();
                cell.Letter    = ch;
                cell.Next      = firstCell;
                firstCell.Prev = cell;

                firstCell = cell;
            }

            // Make this cell be the sentinel.
            Next           = firstCell;
            firstCell.Prev = this;
        }
コード例 #6
0
 // Return true if the cells before and after this one form a palindrome.
 public bool IsPalindromeAtCell(LetterCell cell)
 {
     return
         (IsEvenPalindromeAtCell(cell) ||
          IsOddPalindromeAtCell(cell));
 }
コード例 #7
0
 // Parameterless constructor.
 public LetterCell()
 {
     Letter = '\0';
     Next   = null;
     Prev   = null;
 }