Objective
A stack is an ordered collection of last-in, first-out (LIFO) principles that are stored at the end of the stack, called the top of the stack, and the bottom end of the stack. In the stack, the new elements are near the top of the stack, and the old elements are close to the bottom. You can imagine a stack of books on a table, or a stacked plate in the kitchen.
First, the creation of the stack
You can create a class to represent a stack
1, the creation of a data structure to hold the contents of the stack, select the array//2, the method of declaring some stacks//push (element (s)): Add one or some elements to the top//Pop (): Remove the top element of the stack, and return the element that was removed. Peek (): Returns the element at the top of the stack, simply returns, without making any modifications. IsEmpty (): Returns True if there are no elements in the stack, false. Clear (): Clears the elements inside the stack. Size (): Returns the number of stacks inside the stack. function Stack () {var items = []; This.push = function (Element) {Items.push (element); } This.pop = function () {return items.pop (); } This.peek = function () {return items[items.length-1]; } this.isempty = function () {return items.length = = 0; } this.clear = function () {items = []; } this.size = function () {return items.length; } this.print = function () {Console.log (items.tostring ()); }}//uses stack var stack = new Stack (), Console.log (Stack.isempty ());//truestack.push (+); Stack.push (n); Console.log ( Stack.isempty ());//falseconsole.log (Stack.size ());//2console.log (Stack.peek ()); Stack.pop (); Stack.print ();//100
Second, the application of the stack
Decimal Turn binary
Algorithm Description: Divides the decimal number and 2, or the remainder of each time (0 or 1) until the result is 0 position. The binary of Figure 10 is: //10/2 = 5, remainder 0 //5/2 = 2 , remainder 1 //2/2 = 1 , remainder 0 //= 0 , remainder 1 //10 of binary tabulation Shown as: 0101
function DiviceBy2 (decnumber) { var remstack = new Stack (), rem, binarystring = '; while (Decnumber > 0) { rem = Math.floor (decnumber%2); Remstack.push (REM); Decnumber = Math.floor (DECNUMBER/2); } while (!remstack.isempty ()) { binarystring + = Remstack.pop (). toString (); } return binarystring;} Console.log (DiviceBy2 (Ten)); 1010console.log (DiviceBy2 (520)); 1000001000console.log (DiviceBy2 (1000)); 1111101000
Decimal into other binary
function Divicebybase (Decnumber, base) { var remstack = new Stack (), rem, basestring = ', digits = ' 0123456789ABCDEF ';//8 binary is the remainder is (0-7) 16 binary is (0-9a-f) while (Decnumber > 0) { rem = Math.floor (decnumber% base); Remstack.push (REM); Decnumber = Math.floor (decnumber/base); } while (!remstack.isempty ()) { basestring + = Digits[remstack.pop ()]; } return basestring;} Console.log (Divicebybase (100345, 2)); 11000011111111001console.log (Divicebybase (100345, 8)); 303771console.log (Divicebybase (100345, 16)); 187f9
JavaScript data structure and algorithm----stack