Dry goods sharing-realize the queue function through the linked list

Realize queue 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 queue is a special linear table. The special feature is that it only allows delete operations at the front of the table, and inserts at the rear of the table. Like the stack, the queue is an operation subject to Linear table of limits. The end that performs the insertion operation is called the end of the queue, and the end that performs the deletion operation is called the head of the queue 1 . For example, push+shift or unshift+pop in the above table all have the function of simulating queue.

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 queue 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. 2

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 queue = (function(){
    
    
    let headNode = null;
    let footNode = null;

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

    function shift(){
    
    
        if(!headNode){
    
    
            return headNode;
        }else{
    
    
            let node = headNode.getNextNode();
            headNode.setNextNode(null);
            headNode = node;
        }
    }

    function foreach(){
    
    
        forEachNode(headNode);
    }

    return {
    
    
        push : push,
        shift : shift,
        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

Doubly linked list realizes queue 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. 3

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 setNextNode(_nextNode){
    
    
        nextNode = _nextNode;
    }

    function getNextNode(){
    
    
        return 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 queue = (function(){
    
    
    let headNode = null;
    let footNode = null;

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

    function pop(){
    
    
        if(!headNode){
    
    
            return headNode;
        }else{
    
    
            let node = footNode.getPrevNode();
            if(!node){
    
    
                return headNode = footNode = node;
            }
            node.setNextNode(null);
            footNode = node;
        }
    }

    function foreach(){
    
    
        forEachNode(headNode);
    }

    return {
    
    
        unshift : unshift,
        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/%E9%98%9F%E5%88%97/14580481 ↩︎

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

  3. 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/108696394