-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
195a007
commit 491804e
Showing
2 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |