Skip to content

Commit 395a402

Browse files
authored
Merge pull request #24 from datastructures-js/add_push_pop
Add push/pop
2 parents cced25c + a726d4f commit 395a402

5 files changed

Lines changed: 34 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
## [4.2.0] - 2022-05-30
9+
### Added
10+
- push & pop as synonyms for enqueue & dequeue
11+
812
## [4.1.4] - 2022-05-15
913
### Fixed
1014
- README

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ A performant queue implementation in javascript.
1414
* [API](#api)
1515
* [constructor](#constructor)
1616
* [Queue.fromArray](#queuefromarray)
17-
* [enqueue](#enqueue)
17+
* [enqueue (push)](#enqueue-push)
1818
* [front](#front)
1919
* [back](#back)
20-
* [dequeue](#dequeue)
20+
* [dequeue (pop)](#dequeue-pop)
2121
* [isEmpty](#isEmpty)
2222
* [size](#size)
2323
* [clone](#clone)
@@ -91,11 +91,11 @@ const list = [10, 3, 8, 40, 1];
9191
const queue = Queue.fromArray<number>(list);
9292
```
9393

94-
### enqueue
95-
adds an element at the back of the queue.
94+
### enqueue (push)
95+
adds an element to the back of the queue.
9696

9797
```js
98-
queue.enqueue(10).enqueue(20);
98+
queue.enqueue(10).enqueue(20); // or queue.push(123)
9999
```
100100

101101
### front
@@ -112,11 +112,11 @@ peeks on the back element in the queue.
112112
console.log(queue.back()); // 20
113113
```
114114

115-
### dequeue
116-
dequeue the front element in the queue.
115+
### dequeue (pop)
116+
removes and returns the front element of the queue.
117117

118118
```js
119-
console.log(queue.dequeue()); // 10
119+
console.log(queue.dequeue()); // 10 // or queue.pop()
120120
console.log(queue.front()); // 20
121121
```
122122

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@datastructures-js/queue",
3-
"version": "4.1.4",
3+
"version": "4.2.0",
44
"description": "a performant queue implementation in javascript",
55
"main": "index.js",
66
"scripts": {

src/queue.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Queue {
1515
}
1616

1717
/**
18-
* Adds an element at the back of the queue.
18+
* Adds an element to the back of the queue.
1919
* @public
2020
* @param {number|string|object} element
2121
*/
@@ -24,6 +24,15 @@ class Queue {
2424
return this;
2525
}
2626

27+
/**
28+
* Adds an element to the back of the queue.
29+
* @public
30+
* @param {number|string|object} element
31+
*/
32+
push(element) {
33+
return this.enqueue(element);
34+
}
35+
2736
/**
2837
* Dequeues the front element in the queue.
2938
* @public
@@ -44,6 +53,15 @@ class Queue {
4453
return first;
4554
}
4655

56+
/**
57+
* Dequeues the front element in the queue.
58+
* @public
59+
* @returns {number|string|object}
60+
*/
61+
pop() {
62+
return this.dequeue();
63+
}
64+
4765
/**
4866
* Returns the front element of the queue.
4967
* @public

test/queue.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('Queue unit tests', () => {
2222
it('should enqueue 3 elements to the stack', () => {
2323
queue.enqueue(1);
2424
queue.enqueue(8);
25-
queue.enqueue(45);
25+
queue.push(45);
2626
});
2727
});
2828

@@ -73,7 +73,7 @@ describe('Queue unit tests', () => {
7373
describe('dequeue()', () => {
7474
it('should dequeue all elements', () => {
7575
expect(queue.dequeue()).to.be.equal(8);
76-
expect(queue.dequeue()).to.be.equal(45);
76+
expect(queue.pop()).to.be.equal(45);
7777
});
7878
});
7979

0 commit comments

Comments
 (0)