This repository was archived by the owner on Aug 1, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 8 files changed +69
-78
lines changed Expand file tree Collapse file tree 8 files changed +69
-78
lines changed Load Diff This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ class ArrayQueue {
2
+ constructor ( capacity ) {
3
+ this . _arr = Array ( capacity || 100 ) ;
4
+ this . _size = 0 ;
5
+ this . _front = - 1 ;
6
+ }
7
+
8
+ enqueue ( data ) {
9
+ if ( this . isFull ( ) ) {
10
+ throw "Queue is full" ;
11
+ } else {
12
+ let position = ( this . _front + this . _size ) % this . _arr . length ;
13
+ this . _arr [ position ] = data ;
14
+ ++ this . _size ;
15
+ }
16
+ }
17
+
18
+ dequeue ( ) {
19
+ if ( this . isEmpty ( ) ) {
20
+ throw "Queue is Empty" ;
21
+ } else {
22
+ let data = this . _arr [ this . _front ] ;
23
+ this . _front = ( this . _front + 1 ) % this . _arr . length ;
24
+ -- this . _size ;
25
+ return data ;
26
+ }
27
+ }
28
+
29
+ front ( ) {
30
+ if ( this . isEmpty ( ) ) {
31
+ throw new Error ( "Queue is empty" ) ;
32
+ } else {
33
+ return this . _arr [ this . _front ] ;
34
+ }
35
+ }
36
+
37
+ isEmpty ( ) {
38
+ return this . _size === 0 ;
39
+ }
40
+
41
+ isFull ( ) {
42
+ return this . _size === this . _arr . length ;
43
+ }
44
+
45
+ size ( ) {
46
+ return this . _size ;
47
+ }
48
+ }
49
+
50
+ // main
51
+
52
+ // const q = new ArrayQueue(10);
53
+ // q.enqueue(1);
54
+ // q.enqueue(5);
55
+ // q.enqueue(10);
56
+ // q.enqueue(3);
57
+
58
+ // console.log(q.front());
59
+ // console.log(q.dequeue());
60
+ // console.log(q.dequeue());
61
+ // q.enqueue(50);
62
+ // console.log(q.dequeue());
63
+ // console.log(q.dequeue());
64
+ // console.log(q.size());
65
+ // console.log(q.front());
66
+ // console.log(q.dequeue());
67
+ // console.log(q.isEmpty());
Original file line number Diff line number Diff line change 1
- const LinkedList = require ( "./SinglyLinkedList2" ) ; // enhanced LinkedList with addBack in O(1)
1
+ const LinkedList = require ( "../LinkedList /SinglyLinkedList2" ) ; // enhanced LinkedList with addBack in O(1)
2
2
3
3
class Queue {
4
4
constructor ( ) {
File renamed without changes.
Original file line number Diff line number Diff line change 1
- const LinkedList = require ( "./SinglyLinkedList" ) ;
1
+ const LinkedList = require ( "../LinkedList /SinglyLinkedList" ) ;
2
2
3
3
class Stack {
4
4
constructor ( ) {
You can’t perform that action at this time.
0 commit comments