DEV Community

Collins Etemesi
Collins Etemesi

Posted on

Introduction to Data Structures and Algorithms With Modern Javascript

What is Data Structure and Algorithms
A data structure (DS) is a way of organising data so that it can be used effectively.
An algorithm is a series of step-by-step instructions that describe how to do something.

Why Data Structures and Algorithms are important

  • Learning data structures will help to write clean and consistent code.
  • Effective algorithms helps to break a problem down into smaller parts and think carefully about how to solve each part with code.

This article will go through a list of following Data Structures and Algorithms: Arrays, Queue, Stack, Linked list.

Arrays
An array is a special variable, which can hold more than one value.
Using an array literal is the easiest way to create a JavaScript Array.
const array_name = [item1, item2, ...];
Example:
const snacks = ["Ice cream", "Nuggets", "Popcorn"];
Operations available in an array include:

  • Accessing Array Elements
  • Changing an Array Element
  • Looping Array Elements
  • Adding Array Elements

Queue
A queue is an ordered list of elements where an element is inserted at the end of the queue and is removed from the front of the queue. A queue works based on the first-in, first-out (FIFO) principle, which is different from a stack, which works based on the last-in, first-out (LIFO) principle.
A queue has two main operations:

  • Enque. Insert a new element at the end of the queue.
  • Dequeue. Remove an element from the front of the queue. Enqueue
enqueue(element){
         this.items[this.count] = element;
         this.count ++;
     }
Enter fullscreen mode Exit fullscreen mode

Dequeue

dequeue(){
         if (this.isEmpty()) {
             return undefined;
         }
         let result = this.items[this.lowestCount]; 
         delete this.items[this.lowestCount]; 
         this.lowestCount ++; 
         return result; 

     }
Enter fullscreen mode Exit fullscreen mode

Stack
Stack is a linear data structure in which addition or removal of element follows a particular order i.e. LIFO(Last in First Out) AND FILO(First in Last Out).
The following is an example of a stack class using array:

// Stack class
class Stack {

    // Array is used to implement stack
    constructor()
    {
        this.items = [];
    }

    // Functions to be implemented
    // push(item)
    // pop()
    // peek()
    // isEmpty()
    // printStack()
}
Enter fullscreen mode Exit fullscreen mode

The following is a list of operations available in implementing a stack

  • Push : Insert an element.
  • Pop : Remove an element
  • Peek : Get the topmost element.
  • Size : Get size of the stack.
  • isEmpty : Check if the stack is empty if empty return true else false.
  • Clear : Reset the stack.

Linked List
A linked list is a linear data structure where each element is a separate object that contains a pointer or a link to the next object in that list. Each element (commonly called nodes) contains two items: the data stored and a link to the next node.
The following an example of a Linked List Node:

// User defined class node
class Node {
    // constructor
    constructor(element)
    {
        this.element = element;
        this.next = null
    }
}
Enter fullscreen mode Exit fullscreen mode

List Of Operations Available in linked list

  • Push: Insert an element at the end of the linked list.
  • Insert: Insert an element at the given index of the linked list.
  • Remove: Remove the end element of the linked list.
  • RemoveAt: Remove the element at the given index of the linked list.
  • GetElementAt: Get the element at the given index of the linked list.
  • IndexOf: Return the index of the element in the linked list.

Conclusion
Data structures will help to write clean and consistent code and algorithms helps to break a problem down into smaller parts and think carefully about how to solve each part with code. Some of these data structures and algorithms are: Arrays, Queue, Stack, Linked list. I hope this article has introduced them to your understanding and satisfaction.

Top comments (0)