Challenge data structures and algorithms--stack push, pop sequence __java

Source: Internet
Author: User

Topic source "Data structure and algorithm surface test 80". Here gives my solution, if you have a better solution, welcome message.

Problem Analysis: The basic operation of the test stack, the stack is a "advanced after-out" data structure. Determining whether a sequence is a stack of pop sequences is a common problem that can be simulated by the push and pop processes, push and pop are always paired, such as:

Method:

 #define PUSH 1 #define POP-1 bool Judge_push_pop (int *a, int *b, int len_a, int len_b) {if (NULL = = A | | NULL = = B | |
    Len_a! = Len_b) return false;
    int *p_a = A;
    int *p_b = b;
    int *OP = (int *) malloc (sizeof (int) * LEN_A * 2);
    int index = 0;
    int i = 0;

        while (I < len_a) {Op[index] = push;

        Index + +;
            if (*p_a = = *p_b) {p_b + +;
            Op[index] = Pop;
        Index + +;
        } p_a + +;
    i + +;
    } while (Index < LEN_A * 2) {op[index++] = Pop;
    }//Judge int start = 0;
    int end = Len_a * 2-1;
            while (Start < end) {if (Op[start] + op[end] = = 0) {start + +;
        End--;
    }else return false;
    } return true;
Free (OP); }

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.