niisar
8/26/2015 - 1:24 PM

Queue Class Implementation

Queue Class Implementation

function Queue(){
	this.dataStore = [];
	this.enqueue = enqueue;
	this.dequeue = dequeue;
	this.front = front;
	this.back = back;
	this.toString = toString;
	this.empty = empty;
}

function enqueue(element){
	this.dataStore.push(element);
}

function dequeue(element){
	this.dataStore.shift();
}

function front(){
	return this.dataStore[0];
}

function back(){
	return this.dataStore[this.dataStore.length-1];
}

function toString(){
	var retStr="";
	for (var i = 0; i < this.dataStore.length; ++i) {
		retStr += this.dataStore[i] +"\n";
	}
	return retStr;
}

function empty(){
	if(this.dataStore.length == 0){
		return true;
	}
	else{
		return false;
	}
}


// test
// var q  = new Queue();
// q.enqueue("john");
// q.enqueue("hill");
// q.enqueue("Angela");
// q.enqueue("Joshep");
// console.log(q.toString());
// q.dequeue();
// console.log(q.toString());
// console.log("First element "+ q.front());
// console.log("Last element "+ q.back());