-
-
Notifications
You must be signed in to change notification settings - Fork 411
feat: Add circular queue data structure implementation #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
vcnovaes
wants to merge
4
commits into
TheAlgorithms:master
from
vcnovaes:feat--add-queue-data-struct
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,98 @@ | ||
/** | ||
* @class CircularQueue | ||
* @description Circular implementation of a queue | ||
*/ | ||
export interface Queue<T> { | ||
push(value: T): void; | ||
pop(): T; | ||
length(): number; | ||
} | ||
|
||
export class CircularQueue<T> implements Queue<T> { | ||
private queue: T[]; | ||
private limit: number; | ||
private frontIdx: number; | ||
private rearIdx: number; | ||
/** | ||
* constructor of the queue, can set a limit, if not provided there is no limit to the queue. | ||
* @function constructor | ||
* @description sets the maximum size possible of the queue, the default value is the biggest size possible | ||
* @param {number} [limit=Number.MAX_VALUE] the limit of the queue | ||
*/ | ||
constructor(limit: number = Number.MAX_VALUE) { | ||
this.limit = limit; | ||
this.queue = new Array<T>(limit); | ||
this.frontIdx = 0; | ||
this.rearIdx = 0; | ||
} | ||
|
||
private increaseRear = () => this.rearIdx = (this.rearIdx + 1) % this.limit; | ||
private increaseFront = () => this.frontIdx = (this.frontIdx + 1) % this.limit; | ||
/** | ||
* @function push | ||
* @description - adds a new element in the end of the queue | ||
* @param {T} value - the new value to add | ||
*/ | ||
public push(value: T) { | ||
if (this.length() === this.limit) | ||
throw ("Maximum queue size reached"); | ||
if (this.queue.length < this.limit) { | ||
this.queue.push(value); | ||
} | ||
else { | ||
this.queue[this.rearIdx] = value; | ||
} | ||
this.increaseRear(); | ||
} | ||
|
||
/** | ||
* @function pop | ||
* @description - remove an element the first element in the queue | ||
* @throws will throw an error if the queue is empty | ||
* @return {T} removed element | ||
*/ | ||
public pop(): T { | ||
if (this.length() === 0) { | ||
throw new Error('Empty queue'); | ||
} | ||
const frontValue = this.front(); | ||
this.increaseFront(); | ||
return frontValue; | ||
} | ||
|
||
/** | ||
* @function length | ||
* @description - number of elements in the queue | ||
* @return {number} the number of elements in the queue | ||
*/ | ||
public length(): number { | ||
return Math.abs(this.rearIdx - this.frontIdx); | ||
} | ||
|
||
/** | ||
* @function isEmpty | ||
* @description - check if the queue is empty | ||
* @return {boolean} returns true if the queue is empty, otherwise false | ||
*/ | ||
isEmpty(): boolean { | ||
return this.length() === 0; | ||
} | ||
|
||
/** | ||
* @function front | ||
* @description - return the first element in the queue | ||
* @return {T | null} return the first element in the queue or null if the queue is empty | ||
*/ | ||
public front(): T { | ||
return this.queue[this.frontIdx]; | ||
} | ||
|
||
/** | ||
* @function back | ||
* @description - return the last added in the queue | ||
* @return {T | null} return the last element or null if the queue is empty | ||
*/ | ||
public back(): T | null { | ||
return this.queue[this.rearIdx - 1]; | ||
} | ||
} |
This file contains hidden or 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,19 @@ | ||
import { CircularQueue } from "../CircularQueue"; | ||
|
||
describe('CircularQueue data structure', () => { | ||
test("push and pop", () => { | ||
const queue = new CircularQueue<number>(5); | ||
[10, 20, 30].forEach((testValue) => queue.push(testValue)); | ||
expect(queue.front()).toBe(10); | ||
expect(queue.pop()).toBe(10); | ||
expect(queue.front()).toBe(20); | ||
expect(queue.back()).toBe(30); | ||
}) | ||
test("Empty queue case", () => { | ||
expect(() => (new CircularQueue<number>(0)).pop()).toThrow('Empty queue'); | ||
}) | ||
|
||
test("Maximum size reached case", () => { | ||
expect(() => (new CircularQueue<number>(0)).push(10)).toThrow('Maximum queue size reached'); | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. That way you still get rid of the non-circular Queue implementation. I want there to be two Queue classes, one circular and the other one not circular. They may both implement a common interface.