We have already seen about Array Data Structure. Stack data structure is implemented using arrays. With push(), pop(), shift(), unshift() we create stack . For creation of stack we always prefer push() and pop(). Now let us implement stack data structure.
Methods to implement Stack Data Structure :
push() – adds an element at top of the stack
pop() – removes the top most element of the stack and if it is called on an empty stack returns ‘underflow’
peek() – similar to pop() returns top most element but does not delete it.
isEmpty() – return true if stack is empty.
printStack() – returns a string in which all elements of the stack are concatenated.
Program to implement Stack :
class Stack {
constructor() {
this.items = [];
}
// add element to the stack
add(element) {
return this.items.push(element);
}
// remove element from the stack
remove() {
if(this.items.length > 0) {
return this.items.pop();
}
}
// view the last element
peek() {
return this.items[this.items.length - 1];
}
// check if the stack is empty
isEmpty(){
return this.items.length == 0;
}
// the size of the stack
size(){
return this.items.length;
}
// empty the stack
clear(){
this.items = [];
}
}
let stack = new Stack();
stack.add(10);
stack.add(20);
stack.add(30);
stack.add(40);
console.log(stack.items);
stack.remove();
console.log(stack.items);
console.log(stack.peek());
console.log(stack.isEmpty());
console.log(stack.size());
stack.clear();
console.log(stack.items);
Already in previous post we have seen about the Data Strucutres in JavaScript. Now we will jump into the creation and implementation of arrays.
As you already know, array represents collection of similar data items. It is an object that stores multiple data items.
Array Declaration:
By array literal
Simplest way to create an array is by using array literal [ ].
Syntax:
let arrayname = [value1, value2, value3,….]
By new keyword
Syntax:
let arrayname = new Array();
//creates an array with 6 elements using array literal
var arr1 = [2,3,1,4,5,7];
//create array using new keyword
var arr2 = new Array(1,3,5,7,9,0);
//create an array with undefined elements
var arr3 = new Array();
//provide elements in arr3
arr3[0] = "Apple";
arr3[1] = "Orange";
arr3[2] = "Banana";
//create an array with 2 elements
var arr4 = new Array("Tom","Paul");
console.log(arr1);
console.log(arr2);
console.log(arr3);
console.log(arr4);
Output:
Accessing arrays:
Elements of an array are accessed using indexes.
var arr1 = [2,3,1,4,5,7];
//first element
console.log(arr1[0]);
//fourth element
console.log(arr1[3]);
Output:
2
4
Adding elements to an array:
We use push() and unshift( ) methods to add elements to an array.
push() method adds elements to the end of an array.
unshift() method adds elements to the start of an array.
It is done by length property. Returns the number of elements in an array.
let fruits = ["grapes","apple","orange","banana"];
console.log(fruits.length);
Output:
4
Methods in an array data structure:
In JavaScript arrays there are various methods available.
Methods
Description
concat()
joins two or more arrays and returns a result
indexOf()
searches an element of an array and returns its position
find()
returns the first value of an array element that passes a test
findIndex()
returns the first index of an array element that passes a test
forEach()
calls a function for each element
includes()
checks if an array contains a specified element
push()
adds a new element to the end of an array and returns the new length of an array
unshift()
adds a new element to the beginning of an array and returns the new length of an array
pop()
removes the last element of an array and returns the removed element
shift()
removes the first element of an array and returns the removed element
sort()
sorts the elements alphabetically in strings and in ascending order
slice()
selects the part of an array and returns the new array
splice()
removes or replaces existing elements and/or adds new elements
let fruits = ["grapes","apple","orange","banana"];
//sort in alphabetical order
fruits.sort();
console.log(fruits);
//find the index position
const pos = fruits.indexOf("apple");
console.log(pos);
//slicing array
const frutis1 = fruits.slice(2)
console.log(fruits1);
//concat two arrays
const frutis2 = frutis.concat(fruits1);