JavaScript data structure and algorithm stack

Posted by orange08 on Thu, 05 Dec 2019 02:08:50 +0100

We can delete or add elements anywhere in the array, but sometimes we need to have more control data structures when adding or deleting elements. There are two data structures similar to array, but they are more controllable when adding or deleting elements. They are stack and queue.
This section focuses on stacks.

1. Stack data structure

Stack is an ordered set following LIFO principle. The newly added or to be deleted elements are stored at the same end of the stack, which is called the top of the stack and the bottom of the stack. In the stack, the new element is near the top of the stack, and the old element is near the bottom. As shown in the figure below:

2. create stack

// First, create a kind of presentation stack
function Stack(){
    let items = []; // Select an array to hold the elements in the stack 
    //Various properties and methods
}

Here are some methods to declare for the stack:

push()  //  Add one or more new elements to the top of the stack
pop()  //  Remove the top of stack element and return the removed element
peek()  // Just return the top element of the stack without any changes to the stack
isEmpty()  //  If no element in the stack returns true, false otherwise
clear()  // Remove all elements from the stack
size()  // Returns the number of elements in the stack (similar to array length) 

2.1 add elements to the stack

this.push() = function(element) {
    items.push(element);
}

2.2 remove elements from stack

this.pop = function() {
    items.pop();
}

2.3 view stack top elements

this.peek = function() {
    return items[items.length - 1];
}

2.4 check whether the stack is empty

this.isEmpty = function(){
    return items.length === 0;
}
this.size = function() {
    return items.lenght;
}

2.5 empty and print stack elements

this.clear = function() {
    items = [];
} 
this.print = function() {
    console.log(items.toString());
}

After adding the above methods, we have created a Stack completely.

3. Application of stack -- decimal to N-ary

First we write to convert decimal to binary:

function divideBy2(decNum) {
    var remStack = new Stack(),
        rem,
        binaryString = '';
    // Put the remainder of dividing decimal number by 2 into a stack    
    while(decNum > 0) {
        // Remainder
        rem = Math.floor(decNum % 2);
        // Push
        remStack.push(rem);
        decNum = Math.floor(decNum / 2);
    }    
    // Take it out of the stack, convert it to a string, and connect it to form a binary
    while(!remStack.isEmpty()) {
        // Stack out
        binaryString += remStack.pop().toString();
    } 
    return binaryString;
}

The following decimal conversion N-ary algorithm

function divideByN(decNum, n) {
    var remStack = new Stack(),
        rem,
        binaryString = '',
        digits = '0123456789ABCDEF';
    // Put the remainder of decimal number divided by N into a stack    
    while(decNum > 0) {
        // Remainder
        rem = Math.floor(decNum % n);
        // Push
        remStack.push(rem);
        decNum = Math.floor(decNum / n);
    }    
    // Take it out of the stack, convert it to a string, and connect it to form a binary
    while(!remStack.isEmpty()) {
        // Using digital to make a corresponding conversion in hexadecimal
        binaryString += digits[remStack.pop()];
    } 
    return binaryString;
}

Topics: Javascript