Introduction
Data structures are an essential part of any programming language. They allow us to store and manipulate data efficiently, enabling us to solve complex problems. In TypeScript, we have access to a wide range of data structures, each with its own strengths and use cases.
In this article, we will explore some of the most commonly used data structures in TypeScript and discuss their implementations. We will also cover popular interview questions related to these data structures, providing solutions and explanations.
Arrays
Arrays are a fundamental data structure in TypeScript. They allow us to store a collection of elements of the same type and access them using an index. TypeScript provides built-in support for arrays, including various methods for manipulating and iterating over them.
// Creating an array
const fruits: string[] = ["apple", "banana", "orange"];
// Accessing array elements
console.log(fruits[0]); // Output: apple
// Modifying array elements
fruits[1] = "grape";
console.log(fruits); // Output: ["apple", "grape", "orange"]
// Iterating over an array
for (const fruit of fruits) {
console.log(fruit);
}
Linked Lists
Linked lists are a dynamic data structure that consists of a sequence of elements, where each element points to the next element in the list. Unlike arrays, linked lists can grow or shrink in size during runtime.
class Node<T> {
value: T;
next: Node<T> | null;
constructor(value: T) {
this.value = value;
this.next = null;
}
}
class LinkedList<T> {
head: Node<T> | null;
constructor() {
this.head = null;
}
// Inserting a new node at the end of the list
append(value: T) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
}
// Displaying the elements in the list
display() {
let current = this.head;
while (current) {
console.log(current.value);
current = current.next;
}
}
}
const list = new LinkedList<number>();
list.append(1);
list.append(2);
list.append(3);
list.display();
Stacks and Queues
Stacks and queues are abstract data types that represent collections of elements with specific orderings. A stack follows the Last-In-First-Out (LIFO) principle, while a queue follows the First-In-First-Out (FIFO) principle.
// Stack implementation using an array
class Stack<T> {
items: T[];
constructor() {
this.items = [];
}
// Adding an element to the stack
push(element: T) {
this.items.push(element);
}
// Removing the top element from the stack
pop() {
return this.items.pop();
}
// Checking if the stack is empty
isEmpty() {
return this.items.length === 0;
}
}
const stack = new Stack<number>();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.pop()); // Output: 3
console.log(stack.isEmpty()); // Output: false
// Queue implementation using an array
class Queue<T> {
items: T[];
constructor() {
this.items = [];
}
// Adding an element to the queue
enqueue(element: T) {
this.items.push(element);
}
// Removing the front element from the queue
dequeue() {
return this.items.shift();
}
// Checking if the queue is empty
isEmpty() {
return this.items.length === 0;
}
}
const queue = new Queue<number>();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.dequeue()); // Output: 1
console.log(queue.isEmpty()); // Output: false
Trees
Trees are hierarchical data structures that consist of nodes connected by edges. Each node can have zero or more child nodes, and one node is designated as the root of the tree. Trees find applications in various areas, such as file systems, databases, and computer networks.
class TreeNode<T> {
value: T;
children: TreeNode<T>[];
constructor(value: T) {
this.value = value;
this.children = [];
}
// Adding a child node
addChild(value: T) {
const childNode = new TreeNode(value);
this.children.push(childNode);
}
}
const root = new TreeNode("root");
const child1 = new TreeNode("child1");
const child2 = new TreeNode("child2");
root.addChild(child1);
root.addChild(child2);
Conclusion
Understanding data structures is crucial for any developer, especially when it comes to solving technical interview questions. In this article, we have explored common data structures in TypeScript, including arrays, linked lists, stacks, queues, and trees, along with code examples.
By familiarizing ourselves with these data structures and their implementations, we can enhance our problem-solving skills and become more efficient programmers. Keep practicing and applying these concepts in real-world scenarios to strengthen your understanding.