Skip to content

Commit

Permalink
Deque: Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsosborne committed Oct 3, 2017
1 parent 195a007 commit 491804e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
1 change: 0 additions & 1 deletion Chapter 5 - Linked Lists/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function copy(LL){
}
while(runner){
var nextNode = new listNode(runner.val);
console.log(nextNode.val);
previousNode.next = nextNode;
previousNode = nextNode;
runner = runner.next;
Expand Down
64 changes: 64 additions & 0 deletions Chapter6 - Queues and Stacks/Deque: Implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Having combined the designs of Stack and Queue, why not combine features as well. Let's create a class Deque (pronounced 'deck') representing a double-ended queue. On top of the basic six methods, ernable it to push and pop from opposite ends. Specifically, build class Deque with pushFront(val), pushBack(val), popFront(), front(), back(), contains(val), isEmpty(), and size().

class listNode{
constructor(val){
this.val = val;
this.next = null;
}
}


class Deque{
constructor(){
this.head = null;
}
pushFront(val){
var newNode = new listNode(val);
if(!this.head){
this.head = newNode;
}
else{
var runner = this.head;
this.head = newNode;
newNode.next = runner;
}
return this;
}
pushBack(val){
var newNode = new listNode(val);
if(!this.head){
this.head = newNode;
}
else{
var runner = this.head;
while(runner.next){
runner = runner.next;
}
runner.next = newNode;
}
return this;

}
popFront(){
if(!this.head){
return "List is empty";
}
else{
var temp = this.head.next;
this.head = null;
this.head = temp;
}
return this;
}
}

var deque = new Deque();
console.log(deque.pushFront(1));
console.log(deque.pushFront(2));
console.log(deque.pushFront(3));
console.log(deque.pop)

// var deque1 = new Deque();
// console.log(deque1.pushBack(1))
// console.log(deque1.pushBack(3))
// console.log(deque1.pushBack(2))

0 comments on commit 491804e

Please sign in to comment.