Skip to content
HeechanYang edited this page Jan 1, 2019 · 15 revisions

Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

Properties

top : Stack.Node

  Top node of stack.

size : Number

  Size of stack.

Methods

push(value)

  Push the node with a value in the stack.

pop()

  Pop the top node of the stack.

top()

  Return value of the top node of the stack.

size()

  Return size of the stack.

isEmpty()

  Check the stack is empty.

clear()

  Clear the stack.

Example

npm i @structure-js/datastructure
let Stack = require('@structure-js/datastructure').Stack;

let stack = new Stack();

stack.push(3);
stack.push(1);
stack.push(13);
stack.push(12);
stack.push(7);
stack.push(9);

console.log("stack.top() : " + stack.top());
console.log("stack.size() : " + stack.size());

result

stack.top() : 9
stack.size() : 6

Then it would be like this.


[Image 1]

### Next, Let's `pop` each values.
let str = "";
str += stack.pop() + "\t";
str += stack.pop() + "\t";
str += stack.pop() + "\t";
str += stack.pop() + "\t";

console.log(str);

console.log("stack.top() : " + stack.top());
console.log("stack.size() : " + stack.size());

result

9	7	12	13	
stack.top() : 1
stack.size() : 2

Then, It would be like this


[Image 1]