JavaScript data structures in common

data structure

  • Stack : one kind of an ordered set of compliance with the advanced (LIFO) principle; to be newly added or deleted elements stored at the end of the stack, called the top of the stack, and the other end to bottom of the stack. In the stack, new elements are close to the top of the stack, old elements are close to the bottom of the stack.
  • Queue : an ordered set of items on the opposite a follow FIFO (FIFO / First In First Out) principle; new elements added to the queue at the tail, and the element is removed from the head. Newly added elements must be in the end of the queue.
  • List : storing an ordered set of elements, but is different from the array, the elements of the list in memory is not placed in series; and each element of a storage element by the node itself and a reference point to the next element (pointers / links) composition.
  • Set : a set of unordered and only (i.e. not duplicate) items composition; this data structure used with the same limited set of mathematical concepts, but the application of computer science data structure.
  • Dictionary : In [key value] as the data form of the data structure, wherein the key name to query a particular element, similar in Javascript Object.
  • Hash : According to the key value (Key value) directly access the data structure; it to access the recording, to speed up table lookup is mapped to a position by the key value; this mapping function called a hash function, for storing a recording the array is called a hash table.
  • Tree : a n (n> = 1) a finite set of nodes having a hierarchical relationship; it is called "tree" because it looks like an upside down tree, that is to say it is the root upwards, Leaf and downward, substantially-many relationship, a tree can be seen as a special form of FIG.
  • FIG : FIG is an abstract model of the network structure; Figure is a set of nodes (vertices) connected by an edge; may be any binary relation represented by FIG., Such as common: road map diagram, showing many relationships.

Stack

 

 

Examples of Stack's life in common, such as a pile of books, you finally put up that this will be the first after you take away; and such as access to the browser's history, when clicking the back button, last accessed the site from the first history pop up.

Stack comprising the following general method:

  1. push: the element is pushed into a stack
  2. pop: remove the top element, and returns the element is removed
  3. peek: returns the top element
  4. Are there elements of the stack: isEmpty ()
  5. clear (): remove all elements of the stack
  6. size: Returns the number of elements in the stack

Achieve stack as objects

    <script type="text/javascript">
        function Stack() {
            this.count = 0;
            this.storage = {};
            //将一个元素推入栈顶
            this.push = function (value) {
                this.storage[this.count] = value;
                this.count++;
            }
            // remove the top element, the element is removed and returned 
            the this .pop = function () {
                 IF ( the this .count === 0 ) {
                     return undefined;
                }
                this.count--;
                var result = this.storage[this.count];
                delete this.storage[this.count];
                return result;
            }
            // Returns the top element 
            the this .peek = function () {
                 return  the this .storage [ the this .count -. 1 ];
            }
            // if there are elements in the stack 
            the this .isEmpty = function () {
                 // use es6 object attribute syntax determines the length 
                // return Object.keys (this.storage) .length == 0; 
                return  the this .count == 0 ;
            }
            // remove all of the elements in the stack 
            the this .clear = function () {
                 the this .count = 0
                 // return this.storage = {}; 
            }
             // returns the number of elements in the stack in 
            the this .size = function () {
                 return  the this .count;
            }
        }


        was news thanks = New Stack ();
        newStack.push ( "the first element" );
        newStack.push ( "second element" );
        newStack.push ( "third element" );
        the console.log ( "number of printing elements in the stack:" + newStack.size ());
        the console.log ( "Print the stack top element:" + newStack.peek ());
        the console.log ( "the stack to remove the print elements:" + newStack.pop ());
        the console.log ( "top of the stack after removal of the top element in the stack printing again:" + newStack.peek ());
        the console.log ( "determines whether there are elements in the stack:" + newStack.isEmpty ());
        the console.log ( "the stack to remove all of the elements:" + newStack.clear ());
        the console.log ( "After removal of the stack is determined whether there are elements:" + newStack.isEmpty ());
        the console.log ( "the stack to remove the print elements:" + newStack.pop ());
     </ Script>

Achieve stack as an array

    <Script type = "text / JavaScript">
         function Stack () {
             // save the stack array elements 
            the this .dataStore = [];
             // Top stack position used for recording, is initialized to 0         
            the this .top = 0 ;

            the this .push = function (Element) {
                 the this .dataStore [ the this .top ++] = Element;    // first added element in top position, top after adding 1 
            }
             the this .pop = function () {
                 // top first decremented by 1, and then returns the top position of the elements 
                return  the this .dataStore [- the this .top];
            }
            this.peek = function peek() {
                return this.dataStore[this.top - 1];
            }

            
            this.isEmpty = function clear() {
                return this.top ==0;
            }

            this.clear = function clear() {
                this.top = 0;
            }

            this.length = function length() {
                return this.top;
            }
        }
        was news thanks = New Stack ();
        newStack.push ( "the first element" );
        newStack.push ( "second element" );
        newStack.push ( "third element" );
        the console.log ( "Print the number of elements in the stack:" + newStack.length ());
        the console.log ( "Print the stack top element:" + newStack.peek ());
        the console.log ( "the stack to remove the print elements:" + newStack.pop ());
        the console.log ( "top of the stack after removal of the top element in the stack printing again:" + newStack.peek ());
        the console.log ( "determines whether there are elements in the stack:" + newStack.isEmpty ());
        the console.log ( "the stack to remove all of the elements:" + newStack.clear ());
        the console.log ( "After removal of the stack is determined whether there are elements:" + newStack.isEmpty ());
     </ Script>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

queue

An ordered set of items opposite to the stack, following the queue is a FIFO (FIFO / First In First Out) principle; new elements added to the queue at the tail, and the element is removed from the head. Newly added elements must be in the end of the queue. ·

In reality, the most common example is line up, line up to eat, banking line up on the front door of the bus at the back door mechanism ... in front of people a priority to complete their transactions, after the completion of the next person to continue.

 

In computer science, a common example is the print queue. For example, we need to print five copies of a document. We will open each document, and then click the Print button. Each document will be sent to the print queue. The first document is sent to the print queue will be printed first, and so on, until you have printed all documents. ·

Similarly, we achieved a queue class in Javascript.

 

Guess you like

Origin www.cnblogs.com/zhuochong/p/11627598.html