Data Structure | Introduction

Developer, trying to understand, learn and teach better.
Search for a command to run...

Developer, trying to understand, learn and teach better.
No comments yet. Be the first to comment.
After speaking to many developers and aspirants, I realized that the first question that bothers most folks is, "how do I get started?" As someone who is mostly self-taught, I wish someone had given me this guidance when I started out. I'm breaking ...
What is a Singly Linked List? A singly linked list is a linear data structure similar to an array. However, unlike arrays, elements are not stored in a particular memory location or index. Rather each element is a separate object that contains a poin...

Given a string, return a new string with the reversed order of characters. Examples: reverse('apple') === 'leppa' reverse('hello') === 'olleh' reverse('Greetings!') === '!sgniteerG' Pseudo Solution: Convert the string to array Reverse the array usin...

Question: Write a program that prints the numbers from 1 to n. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five, print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz" Opti...

Data Structures are a collection of values, the relationship between them and the functions or operations that can be applied to the data.
The most commonly used data structures are
Different data structures excel at different things. Some are highly specialised , while others are more generally used
Javascript does not come with many of these data structures pre built. Class keyword was introduced in Javascript as a part of ES2015.
Class is like a blueprint to create objects with pre defined properties and methods.
Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 classalike semantics
One way to define a class is using a class declaration. To declare a class, you use the class keyword with the name of the class in the example below the name of the class is "Rectangle".
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
A class expression is another way to define a class. Class expressions can be named or unnamed. The name given to a named class expression is local to the class's body. (it can be retrieved through the class's (not an instance's) name property, though).
// unnamed
let Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle"
// named
let Rectangle = class Rectangle2 {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle2"
The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name "constructor" in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method.
A constructor can use the super keyword to call the constructor of the super class.
"this" keyword refers to the property of that particular object that is being called
Instance methods refer to the functions that can be written inside a class, such that the function is called on each object that is being created. In example below, the function area() can be called on 'square' and 'square1' separately.
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
const square1 = new Rectangle(20, 10);
console.log(square.area); // 100
console.log(square1.area); // 200
Static methods refer to the functions that can be written inside a class, such that the function is called NOT on each object that is being created but can be called using the class itself. Static members (properties and methods) are called without instantiating their class and cannot be called through a class instance. Static methods are often used to create utility functions for an application, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances A method can be made into a static method by adding 'static' keyword before it.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static displayName = "Point";
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.hypot(dx, dy);
}
}
const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
p1.displayName; // undefined
p1.distance; // undefined
p2.displayName; // undefined
p2.distance; // undefined
console.log(Point.displayName); // "Point"
console.log(Point.distance(p1, p2)); // 7.0710678118654755
Reference Documents: