An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
February 11, 2021 06:10 am GMT
Original Link: https://dev.to/code_regina/data-structures-introduction-5073
Data Structures Introduction
-Which Data Structure is The Best? -ES2015 Class Syntax Overview -Data Structure: The Class Keyword -Data Structure: Adding Instance Methods
Enter fullscreen mode Exit fullscreen mode
Which Data Structure is The Best?
Data structures are collections of values, the relationships among them, and the functions or operations that can be applied to the data. Data structures excel at different things. Some are highly specialized, while others are more generally used.
Visualized Data Structures
Array
Singly Linked List
Hash Table
Tree
Binary Search Tree
Undirected Graph
Directed Graph
All data structures store data, however the relationship between the data and the functionality between methods differ.
ES2015 Class Syntax Overview
What is a class?
A class is a blueprint for creating objects with pre-defined properties and methods.
Class example
class Student { constructor(firstName, lastName, year){ this.firstName = firstName; this.lastName = lastName; this.grade = year; }}let firstStudent = new Student("Colt", "Steele",1);let secondStudent = new Student("Blue", "Steele",2);
Enter fullscreen mode Exit fullscreen mode
The method to create new objects must be called constructor.
The class keyword creates a constant that can not be redefined.
Data Structure: Adding Instance Methods
class Student { constructor(firstName, lastName, year){ this.firstName = firstName; this.lastName = lastName; this.grade = year; this.tardies = 0; this.scores = []; } fullName(){ return `Your full name is ${this.firstName} ${this.lastName}`; } markLate(){ this.tardies += 1; if(this.tardies >= 3) { return "YOU ARE EXPELLED!!!!" } return `${this.firstName} ${this.lastName} has been late ${this.tardies} times`; } addScore(score){ this.scores.push(score); return this.scores } calculateAverage(){ let sum = this.scores.reduce(function(a,b){return a+b;}) return sum/this.scores.length; } }let firstStudent = new Student("Colt", "Steele",1);let secondStudent = new Student("Blue", "Steele",2);
Enter fullscreen mode Exit fullscreen mode
Original Link: https://dev.to/code_regina/data-structures-introduction-5073
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To