What is a data structure?
A data structure defines how to store & use a group of data.
Before explaining data structures, what is data? Data are all values that make up real life, such as letters, numbers, sounds, pictures, and videos. We can classify data as our name, age, height, home address, voice or even genetic DNA. However, data does not hold any information by itself. For example, if you can only know age data, you cannot tell whether it is age of a person, the age of a dog, or the age of a tree. In this way, data can have meaning only when it is analyzed, organized, and utilized.
In addition, we classify and define data according to the purpose of use.
Let's assume that different types of data are organized and utilized in only one way. As you would in a phone book, group numbers in groups of 3 or 4and combine them with a hyphen(-). If you need to name and keep this bundle of numbers, you must always enter a specific name to get the number when retrieving that data. If you're making a phone book, you can use it as is, but you don't need the hyphen when you need to get distances or keep numbers longer than five digits. If you store numeric data in this way, you'll need to name all the numbers in a way that you don't need. As such, it is necessary to understand the characteristics of data, organize it, and utilize it as needed.
It is much more advantageous to organize and store data systematically than to store data without set rules or to organize and use data in one structure only.
Numerous senior developers have researched different ways to efficiently deal with data in a myriad of situations.
Four mose common data structures:
Stack has the same meaning as stacking up or piling.
Literally it is a data structure that stacks data in order.
Ex:
Five cars are passing through a naroow alley in town. A vehicle entering a narrow alley does not know what the future holds, but the end of this alley is blocked. The first car went straight through this alley, and the rest of the cars followed the tail of the car in foront. But the first car hit the dead end. The fifth car that came in last had to reverse and get out first.
An alley can be compared to a data structure: stack, and a car to data. This stack data structure policy is also called LIFO(Last in First Out) or FILO(First in Last Out).
Data structure Queue is a concept opposite to Stack, and has the characteristics of FIFO(First in First Out) or LILO(Last in Last OUt), in which data that enters first comes out first.
Below are codes that shows how Stack or Queue works using the class method.
class Stack {
constructor() {
this.storage = {};
this.top = 0;
size() {
return this.top;
}
push(element) {
this.storage[this.top] = element;
this.top += 1;
}
pop() {
if (this.size() <= 0) {
return;
}
const result = this.storage[this.top - 1];
delete this.storage[this.top - 1];
this.top -= 1;
return result;
}
}
class Queue {
constructor() {
this.storage = {};
this.front = 0;
this.rear = 0;
}
size() {
return this.rear - this.front
}
enqueue(element) {
this.storage[this.rear] = element;
this.rear += 1;
}
dequeue() {
if (this.size() === 0) {
return;
}
const result = this.storage[this.front];
delete this.storage[this.front];
this.front += 1;
return result;
}
}
However, this takes a long time when solving algorithm problems. Therefore using the array to replace stack and queue data structures is a much efficient way.
const stack = [];
stack.push(1); // [1]
stack.push(2); // [1, 2]
stack.push(3); // [1, 2, 3]
stack.push(4); // [1, 2, 3, 4]
stack.push(5); // [1, 2, 3, 4, 5]
console.log(stack); // [1, 2, 3, 4, 5]
stack.pop(); // [1, 2, 3, 4]
stack.pop(); // [1, 2, 3]
console.log(stack); // [1, 2, 3]
const queue = [];
queue.push(1); // [1]
queue.push(2); // [1, 2]
queue.push(3); // [1, 2, 3]
queue.push(4); // [1, 2, 3, 4]
queue.push(5); // [1, 2, 3, 4, 5]
console.log(queue); // [1, 2, 3, 4, 5]
queue.shift(); // [2, 3, 4, 5]
queue.shift(); // [3, 4, 5]
console.log(queue); // [3, 4, 5]