Decryption Palindrome--stack

Source: Internet
Author: User
Tags data structures printf

In the previous section we learned about queues, which are a first-in, first-out data structure. There is also a LIFO data structure which is called a stack. Stack qualification can only be inserted and deleted at one end. For example, there is a small bucket, the diameter of the bucket can only put a small ball, we now to the small bucket in turn into 2nd, 1th, 3rd ball. If you need to take out the 2nd ball now, then you must first take out the 3rd ball, and then take out the 1th ball, the last to take the 2nd ball out. In the process of taking the ball, the first ball we put in the final can be taken out, and finally put in the ball can be first taken out. This is the LIFO, which can also be referred to as advanced after the first out.

There are a lot of examples in our lives, like when we're eating a bucket of potato chips, if you want to eat the last piece, you have to eat up all the front (it seems that the current barrel of potato chips in order to reduce the weight, in the bucket to add a transparent drawer); For example, when we browse the Web, we need to return to a previous page, We need a step-by-step click on the Back button. There is the hand-gun cartridge, in the cartridge, the last loaded with a bullet, is the first shot out. The implementation of the stack is also very simple, only need a one-dimensional array and a point to the top of the stack can be a variable. We insert and delete the stack through the top of the variable.

What is the role of this special stack of data structures? Let's take a look at an example. "Xyzyx" is a palindrome string, the so-called palindrome string is correct to read the same character sequence, such as "Chairman of the Chair", "Remember the Secretary", "Aha" and "Ahaha" are palindrome, but "Ahah" is not a palindrome. Through the stack of this data structure we will be very easy to determine whether a string is a palindrome.

First we need to read the line string and find out the length of the string.

Char a[101]; 101 is an estimate that is only as large as the length of the string to be read
int Len;
Gets (a);
Len=strlen (a);

If a string is a palindrome, then it must be intermediate symmetry, and we need to find the midpoint of the string, namely:

mid=len/2-1;

The next step is the stack.

We first put the characters in the previous section of mid into the stack. Because the stack here is used to store characters, the array type used to implement the stack here is the character array, char s[101]; The initialization stack is simple, top=0, and it's OK. The operation of the stack is top++;s[top]=x; (assuming that the character store that needs to be in the stack is in the character variable x) can actually be abbreviated as S[++TOP]=X;

Now we're going to put all the characters before mid into the stack. The loop here starts with 0, because just read the string using the gets () function, the first character read is stored in s[0], and then one character is stored in s[len-1].

for (i=0;i<=mid;i++)
{
S[++top]=a[i];
}

The next step is to judge the palindrome. The current stack of characters sequentially out of the stack to see if you can match the character one by one after mid, if you can match the string is a palindrome string, otherwise this string is not a palindrome string.

for (i=mid+1;i<=len-1;i++)//In fact, here is not necessarily mid+1, you need to discuss the string length of the parity
{
    if (A[i]!=s[top])
    {break
          ;
    }
    top--;
}
if (top==0)
    printf ("YES");
else
    printf ("NO");

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.