File tree Expand file tree Collapse file tree 4 files changed +57
-2
lines changed Expand file tree Collapse file tree 4 files changed +57
-2
lines changed Original file line number Diff line number Diff line change 8
8
3 . [ Сортировка подсчётом] ( https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/counting-sort.js )
9
9
10
10
## Структуры данных
11
- 1 . [ Стек] ( https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/structures/stack.js )
11
+ 1 . [ Стек] ( https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/structures/stack.js )
12
+ 2 . [ Очередь] ( https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/structures/queue.js )
Original file line number Diff line number Diff line change 13
13
< script src ="./source/sorts/counting-sort.js " type ="modul≠e "> </ script >
14
14
< script src ="./source/sorts/selection-sort.js " type ="module "> </ script >
15
15
< script src ="./source/structures/stack.js " type ="module "> </ script >
16
+ < script src ="./source/structures/queue.js " type ="module "> </ script >
16
17
< script src ="./source/main.js " type ="module "> </ script >
17
18
</ body >
18
19
Original file line number Diff line number Diff line change
1
+ class Node {
2
+ constructor ( data ) {
3
+ this . data = data ;
4
+ this . next = null ;
5
+ }
6
+ }
7
+
8
+ export default class Queue {
9
+ constructor ( data ) {
10
+ this . head = null ;
11
+ this . size = 0 ;
12
+ }
13
+ enqueue ( data ) {
14
+ const newNode = new Node ( data ) ;
15
+ if ( this . head === null && this . size < 1 ) {
16
+ this . head = newNode ;
17
+ } else {
18
+ newNode . next = this . head ;
19
+ this . head = newNode ;
20
+ }
21
+ this . size ++ ;
22
+ }
23
+ dequeue ( ) {
24
+ if ( this . size === 0 ) {
25
+ return ;
26
+ }
27
+ if ( this . size === 1 ) {
28
+ return this . head ;
29
+ }
30
+ let current = this . head ;
31
+ while ( current . next . next !== null ) {
32
+ current = current . next ;
33
+ }
34
+ this . last = current ;
35
+ let popElement = current . next ;
36
+ current . next = null ;
37
+ this . size -- ;
38
+ return popElement ;
39
+ }
40
+ peek ( ) {
41
+ let current = this . head ;
42
+ while ( current . next !== null ) {
43
+ current = current . next ;
44
+ }
45
+ return current ;
46
+ }
47
+ print ( ) {
48
+ let iterator = this . head ;
49
+ while ( iterator !== null ) {
50
+ console . log ( iterator . data ) ;
51
+ iterator = iterator . next ;
52
+ }
53
+ }
54
+ }
Original file line number Diff line number Diff line change @@ -33,7 +33,6 @@ export default class Stack {
33
33
if ( this . size === 1 ) {
34
34
return this . head ;
35
35
}
36
-
37
36
let current = this . head ;
38
37
while ( current . next . next !== null ) {
39
38
current = current . next ;
You can’t perform that action at this time.
0 commit comments