JavaScript를 사용한 기본 데이터 구조 - Stacks🚀
* 🤓 INTRODUCTION
* ❔ WHY DATA STRUCTURES
* 🔖 CLASSIFICATION
* 💉 OPERATIONS
* 📚 STACKS
* 🛠 IMPLEMENTATION
* 👩🏻💻 CODE
* 🙏 THANK YOU
🤓 소개
Welcome, hackers!🚀 As promised in the previous series, we are starting a new series that is dedicated to elementary data structures in computer science. Data structures are an important part of computer science and programming in general. Data structures can provide you a fresh point of view on how to solve the problem, and how to implement an optimal solution for your problem.
If you missed the previous series "Sorting algorithms with JavaScript", I suggest you start from the beginning. Here is a link:
더 이상 사용할 수 없는 기사
Also, please feel free to connect with me via , or
❔ 데이터 구조가 필요한 이유
A data structure is a data organization, management, and storage format that enables efficient access and modification. A data structure is a way to store and organize data so that it can be used efficiently. There are many ways of organizing the data in the memory, for example, a simple array is a collection of memory elements in which data is stored sequentially (one after another).
The data structure is a set of algorithms that we can use in any programming language to structure the data in the memory.
🔖 분류
- Primitive
- Non-primitive
Primitive data structures are primitive data types (integer, char, float, double, and pointer are the primitive data structures). Primitive data structures hold a single value.
Non-primitive data structures are furtherly classified into:
- Linear data structures
- Non-linear data structures
Linear data structure - The data arrangement is in a sequential manner, and it is known as a linear data structure. The data structures used for this purpose are Arrays, Linked Lists, Stacks, and Queues.
Non-linear - When one element is connected to the "n" number of elements - examples are trees and graphs.
Data structures can also be classified into:
- Static - The required size is allocated at the compile time
- Dynamic - The required size is allocated at the run time - it's flexible.
💉 운영
The common operations that can be performed on the data structures are:
- Searching
- Sorting
- Insertions
- Updation
- Deletion
📚 스택
The stack is a linear data structure. In the stack, we can only access an element that is on the top of the stack (last added element). This is LIFO data structure or "Last-In, First-Out"
A Stack is a special case of a linear list. We can only add or delete an element from only one end of the list.
The insert operation on a stack is often called PUSH, and the DELETE operation, which does not take an element argument, is often called POP. These names are allusions to physical stacks, such as the spring-loaded stacks of plates used in cafeterias. The order in which plates are popped from the stack is the reverse of the order in which they were pushed onto the stack since only the top plate is accessible.
🛠 구현
Let's say I am Iron Man 😂 And I have a stack of suits of armor that I can use. Here is how I would add a new suit of armor, delete the suit of armor or check how many of them I have using stack data structure. Let's name it the StarkStack!
👩🏻💻 코드
class StarkStack {
constructor(){
this.data = [];
this.top = 0;
}
push(element) { //adds a suit on top of the stack
this.data[this.top] = element;
this.top = this.top + 1;
}
length() {
return this.data.length;
}
peek() { //Get the Top suit of the Stack
return this.data[this.top-1];
}
isEmpty() {
return this.top === 0;
}
pop() { //read and remove an suit from the top of the stack
if( this.isEmpty() === false ) { //remove only if stack is not empty
this.top = this.top -1;
return this.data.pop(); //remove the last suit
}
}
print() {
var top = this.top - 1;
while(top >= 0) {
console.log(`Stack of suits at ${top+1}: ${this.data[top]}`);
top--;
}
}
reverseMyStack() {
this.reverse(this.top - 1 );
}
reverse(index) {
if(index != 0) {
this.reverse(index-1);
}
console.log(`Reversed stack of suits at ${index+1}: ${this.data[index]}`);
}
}
var stack = new StarkStack();
stack.push("Space Armor MK1");
stack.push("Iron Man Armor MK VII");
stack.push("Iron Man Armor MK VI");
stack.push("Hydro Armor");
stack.push("Mark IX");
stack.push("Mark X");
stack.push("Mark Xi");
stack.print();
stack.reverseMyStack();
console.log(`The suit on the top: ${stack.peek()}`);
console.log(`Number of suits in the stack: ${stack.length()}`);
/*
OUTPUT
Stack of suits at 7: Mark Xi
Stack of suits at 6: Mark X
Stack of suits at 5: Mark IX
Stack of suits at 4: Hydro Armor
Stack of suits at 3: Iron Man Armor MK VI
Stack of suits at 2: Iron Man Armor MK VII
Stack of suits at 1: Space Armor MK1
Reversed stack of suits at 1: Space Armor MK1
Reversed stack of suits at 2: Iron Man Armor MK VII
Reversed stack of suits at 3: Iron Man Armor MK VI
Reversed stack of suits at 4: Hydro Armor
Reversed stack of suits at 5: Mark IX
Reversed stack of suits at 6: Mark X
Reversed stack of suits at 7: Mark Xi
The suit on the top: Mark Xi
Number of suits in the stack: 7
*/
🙏 읽어주셔서 감사합니다!
References:
School notes...
School books...
Please leave a comment, tell me about you, about your work, comment your thoughts, connect with me!
☕ SUPPORT ME AND KEEP ME FOCUSED!
즐거운 해킹 시간 되세요! 😊
Reference
이 문제에 관하여(JavaScript를 사용한 기본 데이터 구조 - Stacks🚀), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/devlazar/elementary-data-structures-with-javascript-stacks-10no
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
School notes...
School books...
Reference
이 문제에 관하여(JavaScript를 사용한 기본 데이터 구조 - Stacks🚀), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/devlazar/elementary-data-structures-with-javascript-stacks-10no텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)