Javascript stack queue using inbuilt functions.

//implementing stack using inbuild functions. this is just to make sure the //user is not performing queue inbuilt operation on stack and stack inbuild operation on queue. // Using Linklist we can build Queue. with inbuild methods. function Stack() { var array=[]; // declaring private to avoid direct accessing. this.push = function(val){ // public array.push(val); }; this.pop= function() { return array.pop().toString(); }; } function Queue() { var queue = []; // declaring private to avoid direct accessing. this.enqueue = function(val){ // public this.array.push(val); }; this.dequeue = function(){ // public return this.queue.pop(); }; } var stack = new Stack(); var queue = new Queue(); stack.push(1); stack.push(2); stack.push(3); //console.log("trying to shift" + stack.shift()); // gives error console.log(stack.pop()); console.log(stack.pop()); console.log(stack.pop()); console.log(stack.toString()); //////////////////////////////////////////////////////////////////

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.