Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
  <meta charset="utf-8">
  <title>Mocha Tests</title>
  <link href="https://unpkg.com/mocha@5.2.0/mocha.css" rel="stylesheet" />
</head>
<body>
  <div id="mocha"></div>

  <script src="https://unpkg.com/chai/chai.js"></script>
  <script src="https://unpkg.com/mocha@5.2.0/mocha.js"></script>

  <div></div>
  

</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                class Node {
  
  constructor(data){
    this.data = data;
    this.prev = null;
    this.next = null;
  }
  
}


class DoublyLinkedList{
  
  constructor(){
    this.head = null;
    this.tail = null;
    this.length = 0;
  }
  
  push(data){
    
    const node = new Node(data);
    
    if(!this.head){
      this.head = node;
      this.tail = node;
    }else{
       node.prev = this.tail; 
      this.tail.next = node;
     
      this.tail = node;
      
    }
    
    this.length++;
  }
  
  
  pop(){
    
    if(!this.head) return null
    
    // tail is the last node so that we take the 
    // prev property from the tail
    const prevNode = this.tail.prev

    if(prevNode){
       prevNode.next = null;
       this.tail = prevNode; // updating the tail
    }else{
      // if  prev property is null 
      // it means there is only single node
      this.head = null;
      this.tail = null;
    }
     this.length--; //decrement the length
  }
  
  
  insertBeginning(data){
    
    // new node is created
    const node = new Node(data);
    
    // if there is no nodes
    if(!this.head) {
      this.head = node;
      this.tail = node;
    }else{
      // update the head.prev to the new node
      // take the new node.next property and link it to the
      // head property
      this.head.prev = node
      node.next = this.head;
      this.head = node;
    }
    // increment the length
    this.length++;
    
  }
  
  removeFirst(){
    
    if(!this.head) return null
    
    const node = this.head.next;
    
    if(node){
      node.prev = null
      this.head = node
    }else{
      this.head = null
      this.tail = null
    }
    
     this.length--;
    
  }
  
  
}



mocha.setup('bdd');


var assert = chai.assert;

describe('Doubly Linked list',()=>{
  
  it("should add the new node at the end",()=>{
    
    const list = new DoublyLinkedList();
    list.push(1)
    list.push(2)
    list.push(3)
    list.push(4)
    
    assert.equal(4,list.length)
      
  })

     it("should remove the node at the end",()=>{
    const list = new DoublyLinkedList();
    list.push("A")
    list.push("B")
    list.push("C")
    list.push("D")
     
       list.pop();
       list.pop();
    
    assert.equal(2,list.length)
    assert.equal("B",list.tail.data)
  })
  
  
    it("should insert the new node at the beginning",()=>{
    const list = new DoublyLinkedList();
    list.push("C")
    list.push("D")
    list.insertBeginning("B");
    list.insertBeginning("A");
      
      
    assert.equal(4,list.length);
    assert.equal("A",list.head.data)  
      
  })
  
  
   it("should remove the node at the beginning",()=>{
    const list = new DoublyLinkedList();
    list.push("C")
    list.push("D")
    list.insertBeginning("B");
    list.insertBeginning("A");
    list.removeFirst()  
      
    assert.equal(3,list.length);
    assert.equal("B",list.head.data)  
      
  })
  
})



mocha.run()
              
            
!
999px

Console