Skip to content

Commit

Permalink
Complete queueStack.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 1, 2015
1 parent ab40413 commit b6f89cf
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions queueStack/queueStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ var Stack = function () {

// add an item to the top of the stack
this.push = function (value) {
storage[size] = value;
storage[ size ] = value;
size += 1;
};

// remove an item from the top of the stack
this.pop = function () {
var popped;
if (this.size() > 0) {
popped = storage[size - 1];
delete storage[size - 1];
if ( this.size() > 0 ) {
popped = storage[ size - 1 ];
delete storage[ size - 1 ];
size -= 1;
return popped;
}
Expand All @@ -44,16 +44,30 @@ var Queue = function() {

// called to add an item to the `queue`
this.enqueue = function (value) {

inbox.push( value );
};

// called to remove an item from the `queue`
this.dequeue = function () {

var temp, popped;

while ( inbox.size() > 0 ) {
temp = inbox.pop();
outbox.push( temp );
}

popped = outbox.pop();

while ( outbox.size() > 0 ) {
temp = outbox.pop();
inbox.push( temp );
}

return popped;
};

// should return the number of items in the queue
this.size = function () {

return inbox.size();
};
};

0 comments on commit b6f89cf

Please sign in to comment.