Skip to content

Commit

Permalink
added size getter
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Oct 2, 2023
1 parent 98bcee8 commit f73e937
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/structures/circular_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export class CircularQueue<T> extends Queue<T> {

enqueue(element: T) {
if (this.isFull) {
this._data[this._tail % this._size] = element
this._tail = (this._tail + 1) % this._size
this._head = this._tail === this._head ? (this._head + 1) % this._size : this._head
this._data[this._tail % this.size] = element
this._tail = (this._tail + 1) % this.size
this._head = this._tail === this._head ? (this._head + 1) % this.size : this._head
} else {
super.enqueue(element)
}
Expand All @@ -19,13 +19,13 @@ export class CircularQueue<T> extends Queue<T> {
if (this.isEmpty) throw new Error('Queue is empty')
const item = this._data[this._head]
delete this._data[this._head]
this._head = (this._head + 1) % this._size
this._head = (this._head + 1) % this.size
return item
}

get space() {
return this._tail >= this._head ?
this._size - (this._tail - this._head)
this.size - (this._tail - this._head)
:
this._head - this._tail
}
Expand All @@ -35,6 +35,6 @@ export class CircularQueue<T> extends Queue<T> {
}

get isEmpty() {
return this.space === this._size
return this.space === this.size
}
}
1 change: 1 addition & 0 deletions src/structures/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Structure {
}

export interface List<T> extends Structure {
size: number
peek: () => T | undefined
items: readonly T[]
}
8 changes: 4 additions & 4 deletions src/structures/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ export class Queue<T> implements List<T> {
protected _data: T[] = []
protected _head = 0
protected _tail = 0
protected _size = 0
size = 0

constructor(size: number | T[]) {
if (typeof size === 'number') {
this._size = size
this.size = size
this._data = new Array<T>(size)
} else {
this._size = size.length
this.size = size.length
this._data = [...size]
}
}
Expand Down Expand Up @@ -48,7 +48,7 @@ export class Queue<T> implements List<T> {
}

get space() {
return this._size - (this._tail - this._head)
return this.size - (this._tail - this._head)
}

get isEmpty() {
Expand Down
14 changes: 8 additions & 6 deletions src/structures/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ import { List } from "./interfaces"
export class Stack<T> implements List<T> {
protected _data: T[] = []
protected _capacity = 0
protected _size = 0
size = 0

constructor(size: number | T[]) {
if (typeof size === 'number') {
this._size = size
this.size = size
this._data = new Array<T>(size)
} else {
this._size = size.length
this.size = size.length
this._data = [...size]
}
}

push(element: T) {
if (this.isFull) throw new Error('Stack is full')
this._data.push(element)
this._capacity += 1
}

pop() {
if (this.isEmpty) throw new Error('Stack is empty')
this._capacity -= 1
return this._data.pop()
}

Expand All @@ -39,18 +41,18 @@ export class Stack<T> implements List<T> {
}

get space() {
return this._data.length
return this.size - this._capacity
}

get isEmpty() {
return this.space === 0
}

get isFull() {
return this.space === this._size
return this.space === this.size
}

get hasRoom() {
return this.space !== this._size
return this.space !== this.size
}
}

0 comments on commit f73e937

Please sign in to comment.