Stack of JavaScript data structures

Source: Internet
Author: User
Tags stack pop

Author's original: http://hawkzz.com/blog/blog/1515054561771

Defined

A stack is a special list of elements that can only be accessed from one end of the list, which is called the top of the stack. Stack is called a first-in and out-of-the-way data structure;

Since the stack has a first-in and out, after-first-out features, so in any not at the top of the stack of elements can not be accessed, in order to get the bottom element, you must first take off the above elements. The two main operations on the stack are an element pressed into the stack and an element popup stack;

Principle

Implementation of the Stack

1. Create a stack constructor:

function Stack() {    this.dataList = [];    this.top = 0;    this.length = length;    this.push = push;    this.pop = pop;    this.peek = peek;    this.clear = clear;}

2, the length of the current stack

function length() {    return this.top;}

3. Press the element into the stack

function push(ele) {    this.top += 1;    this.dataList[this.top - 1] = ele;}

4, the stack top element pop-up stack

function pop() {    this.top -= 1;    return this.dataList[this.top];}

5. Read the top element of the stack

function peek() {    return this.dataList[this.top - 1];}

6. Empty the top of the stack

function clear() {    this.top = 0;}
Implement infix expression into suffix expression

1. Rules

Infix expression (8+3)(9-2)/(6+1), converted to a suffix expression 83+92-61+/;

Conversion process:

    • If the operand is encountered, the output is direct;
    • If an operator is encountered, it is pressed into the stack, including the opening parenthesis;
    • If a closing parenthesis is encountered, the top element of the stack pops up and outputs, and always encounters an opening parenthesis;
    • If another operator is encountered, the element pops out of the stack until it encounters a lower priority element, and then pushes the operator into the stack when it pops up;
    • When the end is read, the elements in the stack pop up sequentially;

2. Realize

    • First, the parentheses are pressed directly into the stack;
    • Read the number 8, direct output;
    • Read the operator "+" and press it directly into the stack;
    • Read the number 3, direct output;

Stack stack: (+
Output: 8 3

    • Read ")", Eject and output "+", Eject "(";

Stack stack:
Output: 8 3 +

    • Read the operator "*", because the stack is empty, directly pressed into the stack;
    • Read "(", directly pressed into the stack;
    • Read the number 9, direct output;
    • Read "-", because the top of the stack is "(", so directly pressed into the stack;
    • Read the number 2, direct output;

Stack stack: * (-
Output: 8 3 + 9 2

    • Read ")", pop Up and enter "-", Pop "(";

Stack Stack: *
Output: 8 3 + 9 2-

    • Read "/", because the top element of the stack "*" and the Read "/" priority is the same, so also to eject and output, and then read the "/" press into the stack;

Stack stack:/
Output: 8 3 + 9 2-*

    • Read "(", directly pressed into the stack;
    • Read the number 6, direct output;
    • Read the operator "+", because the top of the stack is "(", so directly pressed into the stack;
    • Read the number 1, direct output;

Stack stack:/(+
Output: 8 3 + 9 2-6 1

    • Read ")", Eject and output "+", Eject "("

Stack stack:/
Output: 8 3 + 9 2-6 1 +

    • Finally, because the arithmetic expression has been read, the elements in the stack are popped and output

Stack stack:
Output 8 3 + 9 2-6 1 +/

Code
function init (str) {var operatorstack = new Stack ();    var pusharr = [];            for (var i = 0; i < str.length; i++) {if (Isoperator (Str[i])) {var topele = Operatorstack.peek ();            if (!topele) {Operatorstack.push (str[i]);                        } else {switch (Str[i]) {case ' (': Operatorstack.push (Str[i]);                    Break                            Case ') ': while (Topele!== ' (') {var operator = Operatorstack.pop ();                            Pusharr.push (operator);                        Topele = Operatorstack.peek ();                        } operatorstack.pop ();                    Break                        Case ' + ': Case '-': var flag = true; while (flag) {if (Topele = = = ' (') {OPeratorstack.push (Str[i]);                            Flag = false;                                } else {var operator = Operatorstack.pop ();                                Pusharr.push (operator);                            Topele = Operatorstack.peek ();                    }} break;                            Case ' * ': Case '/': if (topele = = = ' * ' | | topele = = = '/') {                            var operator = Operatorstack.pop ();                        Pusharr.push (operator);                        } operatorstack.push (Str[i]);                Break        }}}} else {Pusharr.push (str[i]); }} if (Operatorstack.length () > 0) {while (operatorstack.length () > 0) {var operator = op            Eratorstack.pop ();        Pusharr.push (operator); }} RetuRN Pusharr.join (');} function Isoperator (str) {return [' + ', '-', ' * ', '/', ' (', ') '].indexof (str) >-1;}

Reference: http://blog.csdn.net/sgbfblog/article/details/8001651

Stack of JavaScript data structures

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.