Simple data structure (quad) stack and simple application of queues

Source: Internet
Author: User

Programming to determine whether a string is a palindrome. Palindrome refers to a sequence of characters with an intermediate character as the datum on both sides of the character exactly the same, such as the character sequence "ACBDEDBCA" is a palindrome.

Algorithm thought: To determine whether a character sequence is a palindrome, is to compare the first character with the last word typeface, the second character is compared with the second-lowest character, and so on, and so on, and the n-i character is compared with the first character. If each comparison is equal, then a palindrome, if a comparison is not equal, it is not a palindrome. Therefore, you can put the sequence of characters into the queue and stack, and then out of the queue and out of the stack and compare the queue characters and the characters are equal, if all the same character sequence is a palindrome, otherwise it is not a palindrome.

Program implementation:

Static voidMain (string[] args) {            stringstr = Console.ReadLine ();//Save the characters you enteredstack<Char>stack=Newstack<Char> ();//Create a new stackqueue<Char>queue=Newqueue<Char> ();//Create a new queue             for(inti =0; I < Str. Length; i++)//put each character into a queue{stack.                Push (Str[i]); Queue.            Enqueue (Str[i]); }            BOOLIshui =true;//defines a flag for determining whether it is             while(Stack. Count >0)            {                if(Stack. Pop ()! = queue. Dequeue ())//separate the stack out queue to determine whether they are equal{//The stack is starting from the tail out of the queue is starting from scratch so can be reached to detect whether PalindromeIshui =false;  Break; }} Console.WriteLine ("whether it is a palindrome string:"+Ishui);        Console.readkey (); }

Results:

Simple data structure (quad) stack and simple application of queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.