function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
this.clear = clear;
this.display = display;
}
function push( element) {
this.dataStore[ this.top++ ] = element;
}
function pop() {
return this.dataStore[ --this.top ];
}
function peek() {
if (this.top>0) {
return this.dataStore[this.top-1];
}else{
return null;
}
}
function length() {
return this.top;
}
function clear() {
this.dataStore = [];
this.top =0;
}
function display() {
if (this.top == 0) {
console.log(`stack is empty`);
return null;
}else{
for (let i = this.top-1; i >= 0; i--) {
console.log(this.dataStore[i]);
}
}
}
var stack = new Stack();
stack.push(`apple`);
stack.push(`banane`);
stack.push(`origan`);
stack.display();
console.log(`-----------, the length is ${stack.length()}`);
console.log(`the '${stack.pop()}' delete complete`);
stack.display();
console.log(`-----------, the length is ${stack.length()}`);
stack.clear();
stack.display();
console.log(`-----------, the length is ${stack.length()}`);