Dry goods sharing-realize stack function through linked list

Realize stack function through linked list

Array Features return
push Insert at the bottom Array length
unshift Top insert Array length
pop Delete at the bottom Deleted element
shift Top delete Deleted element

The stack is a data structure that can only be operated on at one end, and it complies with LIFO-last in first out.
The simulation stack operation in JavaScript is very simple, such as push+pop or unshift+shift in the table above, all have the function of simulation stack.

Linked list is a non-contiguous, non-sequential storage structure, it is composed of nodes, and each node has two parts, one is the data stored by the node, and the other is the reference address of the adjacent node.

Singly linked list realizes stack function

A singly linked list (singly linked list) is a kind of linked list. Its characteristic is that the link direction of the linked list is one-way, and the access to the linked list starts from the head through sequential reading. 1

function createNode(){
    
    
    let address;//self address
    let nextNode;//next node address
    let data;//self data

    function getAddress(){
    
    
        return address;
    }

    function setNextNode(_nextNode){
    
    
        nextNode = _nextNode;
    }

    function getNextNode(){
    
    
        return nextNode;
    }

    function setData(_data){
    
    
        data = _data;
    }

    function getData(){
    
    
        return data;
    }

    return address = {
    
    
        getAddress : getAddress,
        setNextNode : setNextNode,
        getNextNode : getNextNode,
        setData : setData,
        getData : getData,
    }
};


let stack = (function(){
    
    
    let headNode = null;
    let footNode = null;

    function push(_node){
    
    
        if(!headNode){
    
    
            headNode = _node;
        }else{
    
    
            footNode.setNextNode(_node);
        }
        footNode = _node;
    }

    function pop(){
    
    
        if(!headNode){
    
    
            return headNode;
        }
        if(headNode === footNode){
    
    
            return headNode = footNode = null;
        }
        let node = findFootNodePrevNode(headNode,footNode);
        if(!node){
    
    
            return headNode = footNode = null;
        }else{
    
    
            footNode = node;
            node.setNextNode(null);
            return footNode;
        }
    }
    
    function foreach(){
    
    
        forEachNode(headNode);
    }

    return {
    
    
        push : push,
        pop : pop,
        foreach : foreach
    }
})();

/**
 * 三种情况  
 * 1. headNode === footNode 只有一个节点 这种在调用这个方法前已经判断了  不存在
 * 2. headNode != null && footNode == null 这种算数据有问题了 不考虑
 * 3. headNode的子节点 === footNode
 * @param {*} headNode 
 * @param {*} footNode 
 */
function findFootNodePrevNode(headNode,footNode){
    
    
    let node = headNode.getNextNode();
    if(node === footNode){
    
    
        return headNode;
    }else{
    
    
        return findFootNodePrevNode(node,footNode);
    }
}

function forEachNode(node){
    
    
    if(!!node){
    
    
        console.log('node.data.name : ' + (node.getData() || 'unKnown'));
        let nextNode = node.getNextNode();
        forEachNode(nextNode);
    }
}

Test code:
Insert picture description here

Doubly linked list realizes stack function

A doubly linked list is also called a double-linked list, which is a kind of linked list. Each data node in it has two pointers, which point to the direct successor and the direct predecessor respectively. 2

function createNode(){
    
    
    let address;//self address
    let nextNode;//next node address
    let prevNode;//previous node address
    let data;//self data

    function getAddress(){
    
    
        return address;
    }

    function getNextNode(){
    
    
        return nextNode;
    }

    function setNextNode(_nextNode){
    
    
        nextNode = _nextNode;
    }

    function getPrevNode(){
    
    
        return prevNode;
    }

    function setPrevNode(_prevNode){
    
    
        prevNode = _prevNode;
    }

    function setData(_data){
    
    
        data = _data;
    }

    function getData(){
    
    
        return data;
    }

    return address = {
    
    
        getAddress : getAddress,
        setNextNode : setNextNode,
        getNextNode : getNextNode,
        setPrevNode : setPrevNode,
        getPrevNode : getPrevNode,
        setData : setData,
        getData : getData,
    }
};


let stack = (function(){
    
    
    let headNode = null;
    let footNode = null;

    function push(_node){
    
    
        if(!headNode){
    
    
            headNode = _node;
        }else{
    
    
            _node.setPrevNode(footNode);
            footNode.setNextNode(_node);
        }
        footNode = _node;
    }

    function pop(){
    
    
        if(!headNode){
    
    
            return headNode;
        }
        if(headNode === footNode){
    
    
            return headNode = footNode = null;
        }
        footNode = footNode.getPrevNode();
        footNode.setNextNode(null);
    }
    
    function foreach(){
    
    
        forEachNode(headNode);
    }

    return {
    
    
        push : push,
        pop : pop,
        foreach : foreach
    }
})();


function forEachNode(node){
    
    
    if(!!node){
    
    
        console.log('node.data.name : ' + (node.getData() || 'unKnown'));
        let nextNode = node.getNextNode();
        forEachNode(nextNode);
    }
}

Test code:
Insert picture description here


  1. Source: https://baike.baidu.com/item/%E5%8D%95%E5%90%91%E9%93%BE%E8%A1%A8/8671935 ↩︎

  2. Source: https://baike.baidu.com/item/%E5%8F%8C%E5%90%91%E9%93%BE%E8%A1%A8/2968731 ↩︎

Guess you like

Origin blog.csdn.net/qq_35508835/article/details/108696023