Core JavaScript

A Doubly-Linked List

This JavaScript Reference section displays the code for an example program that shows how to create a doubly-linked list in JavaScript.

DblLinkedList.html

<!DOCTYPE html>
<html>
<head>
    <title>XoaX.net's Javascript</title>
    <script type="text/javascript" src="DblLinkedList.js"></script>
</head>
<body onload="fnInitialization()">
    <p id="idOutput"></p>
</body>
</html>

DblLinkedList.js

// The Double Link Constructor
function CDblLink(qData) {
  this.mqData = qData;
  this.mqpPrev = null;
  this.mqpNext = null;
}

// An iterator for the doubly-linked list
function CIterator(qpNode) {
	this.mqpCurr = qpNode;
	this.mfnForward = function() {
		this.mqpCurr = this.mqpCurr.mqpNext;
	}
	this.mfnBack = function() {
		this.mqpCurr = this.mqpCurr.mqpPrev;
	}
}

// The Doubly Linked List Constructor
function CDblLinkedList() {
  this.mqpHead = null;
  this.mfnTail = function() {
	  this.mqpHead.mqpNext;
  }
  this.mfnInsertAtHead = function(qData) {
    var qpOldHead = this.mqpHead;
    this.mqpHead = new CDblLink(qData);
    if (qpOldHead == null) {
		this.mqpHead.mqpPrev = this.mqpHead;
		this.mqpHead.mqpNext = this.mqpHead;
	} else {
		this.mqpHead.mqpPrev = qpOldHead;
		this.mqpHead.mqpNext = qpOldHead.mqpNext;
		qpOldHead.mqpNext.mqpPrev = this.mqpHead;
		qpOldHead.mqpNext = this.mqpHead;
	}
  }
  this.mfnRemoveAtHead = function() {
	this.mqpHead.mqpPrev.mqpNext = this.mqpHead.mqpNext;
	this.mqpHead.mqpNext.mqpPrev = this.mqpHead.mqpPrev;
	this.mqpHead = this.mqpHead.mqpPrev;
  }
  this.mfnInsertAtTail = function(qData) {
    var qpTail = new CDblLink(qData);
    if (this.mqpHead == null) {
		this.mqpHead = qpTail;
		this.mqpHead.mqpPrev = this.mqpHead;
		this.mqpHead.mqpNext = this.mqpHead;
	} else {
		qpTail.mqpPrev = this.mqpHead;
		qpTail.mqpNext = this.mqpHead.mqpNext;
		this.mqpHead.mqpNext.mqpPrev = qpTail;
		this.mqpHead.mqpNext = qpTail;
	}
  }
  this.mfnRemoveAtTail = function() {
	this.mqpHead.mqpNext.mqpNext.mqpPrev = this.mqpHead;
	this.mqpHead.mqpNext = this.mqpHead.mqpNext.mqpNext;
  }
  this.mfnIsEmpty = function() {
    return (this.mqpHead == null);
  }
  this.mfnHead = function() {
    return this.mqpHead;
  }
  this.mfnTail = function() {
    return ((this.mqpHead != null) ? this.mqpHead.mqpNext : null);
  }
}

function fnInitialization() {
    var qDblList = new CDblLinkedList();
    // Add at the head in reverse order
    qDblList.mfnInsertAtHead("3");
    qDblList.mfnInsertAtHead("2");
    qDblList.mfnInsertAtHead("1");
    // Add at the tail in order
    qDblList.mfnInsertAtTail("A");
    qDblList.mfnInsertAtTail("B");
    qDblList.mfnInsertAtTail("C");

    var qStopIter = new CIterator(qDblList.mqpHead);
    var qIter = new CIterator(qDblList.mqpHead);
	var qpPrint = document.getElementById("idOutput");

	// Traverse the list backward. Start at the head.
	qIter.mqpCurr = qStopIter.mqpCurr;
	qpPrint.innerHTML = "";
	qpPrint.innerHTML += "Backward ( Head to Tail ): ";
    do {
		qpPrint.innerHTML += qIter.mqpCurr.mqData + " ";
		qIter.mfnBack();
	} while(qIter.mqpCurr != qStopIter.mqpCurr);
	qpPrint.innerHTML += "<br />";

	// Traverse the list forward. Begin at the tail.
	qStopIter.mqpCurr = qDblList.mfnTail();
	qIter.mqpCurr = qStopIter.mqpCurr;
	qpPrint.innerHTML += "Forward ( Tail to Head ): ";
    do {
		qpPrint.innerHTML += qIter.mqpCurr.mqData + " ";
		qIter.mfnForward();
	} while(qIter.mqpCurr != qStopIter.mqpCurr);
	qpPrint.innerHTML += "<br />";
}
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.