Data Structures
Data structures are a fundamental concept in computer science and programming. They provide a way to organize and store data in a way that makes it easier to access, manipulate, and process. There are many different types of data structures, each with its own strengths and weaknesses, and each designed to solve specific problems. In general, a data structure is a way of organizing and storing data so that it can be accessed and used efficiently. The choice of data structure depends on the specific problem that needs to be solved, as well as the constraints and requirements of the system.
Arrays
An array is a collection of elements, each identified by an index or a key. Arrays are typically used to store data of the same type, such as a list of numbers or a list of strings. Arrays are a simple and efficient data structure, but they can be limited in their flexibility, as their size is fixed when they are created.
1// creating an array
2const myArray = [1, 2, 3, 4, 5];
3
4// accessing an element in an array
5const secondElement = myArray[1];
6
7// adding an element to the end of an array
8myArray.push(6);
9
10// removing an element from the end of an array
11myArray.pop();
Linked Lists
A linked list is a collection of elements, each containing a value and a pointer to the next element in the list. Linked lists are used to store data of any type and can be easily modified by adding or removing elements. However, accessing an element in a linked list requires traversing the list from the beginning, which can be time-consuming.
1// creating a linked list node
2class ListNode {
3 constructor(value, next = null) {
4 this.value = value;
5 this.next = next;
6 }
7}
8
9// creating a linked list
10class LinkedList {
11 constructor() {
12 this.head = null;
13 }
14
15 // adding a node to the end of the list
16 append(value) {
17 const newNode = new ListNode(value);
18 if (!this.head) {
19 this.head = newNode;
20 return;
21 }
22 let currentNode = this.head;
23 while (currentNode.next) {
24 currentNode = currentNode.next;
25 }
26 currentNode.next = newNode;
27 }
28
29 // removing a node from the list
30 remove(value) {
31 if (!this.head) {
32 return;
33 }
34 if (this.head.value === value) {
35 this.head = this.head.next;
36 return;
37 }
38 let currentNode = this.head;
39 while (currentNode.next) {
40 if (currentNode.next.value === value) {
41 currentNode.next = currentNode.next.next;
42 return;
43 }
44 currentNode = currentNode.next;
45 }
46 }
47}
48
49// using the linked list
50const myList = new LinkedList();
51myList.append(1);
52myList.append(2);
53myList.append(3);
54myList.remove(2);
Stacks
A stack is a data structure that follows the Last In, First Out (LIFO) principle. Elements are added to and removed from the top of the stack, and the most recently added element is always the first to be removed. Stacks are used to keep track of function calls, undo and redo operations, and other similar tasks.
1//creating a stack
2class Stack {
3constructor() {
4 this.items = [];
5}
6
7// adding an item to the top of the stack
8push(item) {
9 this.items.push(item);
10}
11
12// removing the top item from the stack
13pop() {
14 return this.items.pop();
15}
16
17// getting the top item from the stack without removing it
18peek() {
19 return this.items[this.items.length - 1];
20}
21}
22
23// using the stack
24
25const myStack = new Stack();
26myStack.push(1);
27myStack.push(2);
28myStack.push(3);
29myStack.pop();
30
Queues
A queue is a data structure that follows the First In, First Out (FIFO) principle. Elements are added to the back of the queue and removed from the front, and the oldest element is always the first to be removed. Queues are used to manage tasks in a system, such as printing jobs or network requests.
1// creating a queue
2class Queue {
3 constructor() {
4 this.items = [];
5 }
6
7 // adding an item to the back of the queue
8 enqueue(item) {
9 this.items.push(item);
10 }
11
12 // removing the first item from the queue
13 dequeue() {
14 return this.items.shift();
15 }
16
17 // getting the first item from the queue without removing it
18 peek() {
19 return this.items[0];
20 }
21}
22
23// using the queue
24const myQueue = new Queue();
25myQueue.enqueue(1);
26myQueue.enqueue(2);
27myQueue.enqueue(3);
28myQueue.dequeue();
Trees
A tree is a hierarchical data structure that consists of nodes connected by edges. Each node contains a value, and the edges define the relationship between the nodes. Trees are used to represent hierarchical relationships, such as the structure of a file system or the organization of a company.
Let's create a simple file system hierarchy as a tree using JavaScript. We can represent each directory as a node in the tree, with its children directories and files as its edges.
1class Node {
2constructor(name) {
3 this.name = name;
4 this.children = [];
5}
6
7addChild(node) {
8 this.children.push(node);
9}
10}
11
12// Create root directory
13const root = new Node("root");
14
15// Create subdirectories and files
16const dir1 = new Node("dir1");
17const dir2 = new Node("dir2");
18const file1 = new Node("file1.txt");
19const file2 = new Node("file2.txt");
20
21// Add subdirectories and files to root directory
22root.addChild(dir1);
23root.addChild(dir2);
24root.addChild(file1);
25root.addChild(file2);
26
27// Create subdirectories and files for dir1
28const dir1_1 = new Node("dir1_1");
29const dir1_2 = new Node("dir1_2");
30const file1_1 = new Node("file1_1.txt");
31const file1_2 = new Node("file1_2.txt");
32
33// Add subdirectories and files to dir1
34dir1.addChild(dir1_1);
35dir1.addChild(dir1_2);
36dir1.addChild(file1_1);
37dir1.addChild(file1_2);
Graphs
A graph is a collection of nodes and edges that can be connected in any way. Graphs are used to represent complex relationships between elements, such as social networks, road networks, and communication networks.
Let's create a simple social network as a graph using JavaScript. We can represent each user as a node in the graph, with their connections to other users as its edges.
1class Node {
2constructor(name) {
3 this.name = name;
4 this.connections = [];
5}
6
7addConnection(node) {
8 this.connections.push(node);
9}
10}
11
12// Create users
13const user1 = new Node("User 1");
14const user2 = new Node("User 2");
15const user3 = new Node("User 3");
16
17// Connect users
18user1.addConnection(user2);
19user2.addConnection(user3);
20user3.addConnection(user1);
21
In this example, we create a Node class that represents each user in the social network. We create three users, user1, user2, and user3, and connect them to each other using the addConnection method. We represent the connections between users as edges in the graph.