create a javascript array-based stack structure

  Stack is an ordered set of compliance LIFO (LIFO) principle. Add or delete new elements are to be saved at the same end of the stack, called the top of the stack, and the other end is called the 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. 

Stack has the following methods:

push (element): element stack, add one or more new elements to the top of the stack

pop (): the elements of the stack, the stack element is removed, while the return element is removed

peek (): Returns the top of the stack of elements, the elements do not make any changes to the station

isEmpty (): determine whether the stack is empty, if no elements in the stack returns true, false otherwise

clear (): remove all elements within the stack

size (): returns the number of elements in the stack, length and the properties of the method is similar to the array

 

Description:

  The head of the tail of the array is the bottom of the stack, the stack is an array

Because it is based on an array of javascript build stack, so the array will use a variety of methods, first create a class represents the class, here uses syntax ES6, the next one by one began to stack six conventional methods.

 

 

 

s1. Stack constructor statement

1  // declare stack constructor in an empty array elements to preserve the stack 
2  class {Stack
 . 3      constructor () {
 . 4          the this .items = [];    
 . 5      }
 . 6 }

s2. realization push () method, an element onto the stack

Using an array of push method, into the end of the array elements, i.e. the top of the stack in the stack configuration.

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

s3. realization pop () method, the stack elements and returns the element

According to the principle of last-out, removing the element is the last element added to the stack, pop method used here arrays

pop() {
  return this.items.pop();
}

s4. realize peek () method, see the top of the stack, which is the last element added to the stack

Performance of the array elements of the array index position of the last, the last element of the array can be accessed by length -1

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

s5. realize isEmpty () method to see if the stack is empty

In fact, determine the length of the array is 0

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

s6. realize clear () method for all elements, empty stack

Directly to the array can be reset to empty

clear() {
  return this.items = [];
}

s7. realize size () method returns the length of the stack

Performance in the array is to return the length of the array

size() {
  return this.items.length;
}

  At this point, based on an array of built stack is complete, then began to test! Copy the following code can be used directly.

<! DOCTYPE HTML> 
<HTML lang = "EN"> 
<head> 
  <Meta charset = "UTF-. 8"> 
  <Meta name = "the viewport" Content = "width = Device-width, Initial-Scale = 1.0"> 
  < HTTP-equiv = Meta "X--the UA-Compatible" Content = "IE = Edge"> 
  <title> 
  
  </ title> 
</ head> 
<body> 
	<h1 of> stack test </ h1 of> 
	<Script> 
		// stack constructor declared empty array elements to preserve a value in the stack 
class stack { 
	constructor () { 
		this.items = [];	 
	} 
	
	Push (element) { 
		this.items.push (element); 
	} 

	POP () { 
		return this.items.pop ();
	}

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

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

	Clear () { 
		return this.items = []; 
	} 

	size () { 
		return this.items.length; 
	} 
} 
	// instantiate an object first stack 
	const stack = new stack (); 

	stack.push (12 is); // push 
	stack.push (20 is); 
	the console.log (stack.isEmpty ()); // output to false 
	the console.log (stack.pop ()); // output 20, where the removal of the top element 20, and returns 
	console.log (stack.peek ()); // output 12, which is the return element 12 is still stored in the stack 

	console.log (stack.size ( )); // output. 1 

	stack.clear (); // clear the stack, the stack is empty at this time 
	console.log (stack.isEmpty ()); // output to true 
</ Script> 
</ body> 
</ HTML>

  

 

 

 He would later write a stack-based structure to build a JavaScript object implementation. Pure difficult hand to play, please indicate the source!

<html> <html lang = " en"> <head> <meta charset = "UTF-8"> <meta name = "viewport" content = "width = device-width, initial-scale = 1.0"> <meta http -equiv = "X-UA-Compatible " content = "ie = edge"> <title> </ title> </ head> <body> <h1> stack test </ h1> <script> // stack in the configuration empty array function declaration elements to preserve a class stack {constructor () {this.items the stack = [];} Push (element) {this.items.push (element);}
POP () {return the this. items.pop ();}
PEEK () {return this.items [this.items.length -. 1];}
isEmpty () {return this.items.length === 0;}
Clear () {return this.items = [];}
size () {return this.items.length;}} // first instance of an object const stack stack stack new new = ();
stack.push (12 is); // push stack.push (20 ); console.log (stack.isEmpty ()) ;// output falseconsole.log (stack.pop ()); // output 20, where the removal of the top element 20, and returns console.log (stack.peek ()); // output 12, which is returned element 12 is still stored in the stack
console.log (stack.size ()); // output. 1
stack.clear (); // clear the stack, the stack is empty at this time console.log (stack.isEmpty ()); // output true </ script> </ body> </ html>

 

Guess you like

Origin www.cnblogs.com/cducz/p/11827568.html