File tree Expand file tree Collapse file tree 3 files changed +90
-1
lines changed Expand file tree Collapse file tree 3 files changed +90
-1
lines changed Original file line number Diff line number Diff line change
1
+ const LinkedList = require ( "./linked-list-tail" ) ;
2
+
3
+ class LinkedListQueue {
4
+ constructor ( ) {
5
+ this . list = new LinkedList ( ) ;
6
+ }
7
+
8
+ enqueue ( value ) {
9
+ this . list . append ( value ) ;
10
+ }
11
+
12
+ dequeue ( ) {
13
+ return this . list . removeFromFront ( ) ;
14
+ }
15
+
16
+ peek ( ) {
17
+ return this . list . head . value ;
18
+ }
19
+
20
+ isEmpty ( ) {
21
+ return this . list . isEmpty ( ) ;
22
+ }
23
+
24
+ getSize ( ) {
25
+ return this . list . getSize ( ) ;
26
+ }
27
+
28
+ print ( ) {
29
+ return this . list . print ( ) ;
30
+ }
31
+ }
32
+
33
+ const queue = new LinkedListQueue ( ) ;
34
+ console . log ( queue . isEmpty ( ) ) ;
35
+ queue . enqueue ( 10 ) ;
36
+ queue . enqueue ( 20 ) ;
37
+ queue . enqueue ( 30 ) ;
38
+ console . log ( queue . getSize ( ) ) ;
39
+ queue . print ( ) ;
40
+ console . log ( queue . dequeue ( ) ) ;
41
+ queue . print ( ) ;
42
+ console . log ( queue . peek ( ) ) ;
Original file line number Diff line number Diff line change
1
+ const LinkedList = require ( "./linked-list-tail" ) ;
2
+
3
+ class LinkedListStack {
4
+ constructor ( ) {
5
+ this . list = new LinkedList ( ) ;
6
+ }
7
+
8
+ push ( value ) {
9
+ this . list . prepend ( value ) ;
10
+ }
11
+
12
+ pop ( ) {
13
+ return this . list . removeFromFront ( ) ;
14
+ }
15
+
16
+ peek ( ) {
17
+ return this . list . head . value ;
18
+ }
19
+
20
+ isEmpty ( ) {
21
+ return this . list . isEmpty ( ) ;
22
+ }
23
+
24
+ getSize ( ) {
25
+ return this . list . getSize ( ) ;
26
+ }
27
+
28
+ print ( ) {
29
+ return this . list . print ( ) ;
30
+ }
31
+ }
32
+
33
+ const stack = new LinkedListStack ( ) ;
34
+ console . log ( stack . isEmpty ( ) ) ;
35
+ stack . push ( 20 ) ;
36
+ stack . push ( 10 ) ;
37
+ stack . push ( 30 ) ;
38
+ console . log ( stack . getSize ( ) ) ;
39
+ stack . print ( ) ;
40
+ console . log ( stack . pop ( ) ) ;
41
+ stack . print ( ) ;
42
+ console . log ( stack . peek ( ) ) ;
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ class Node {
5
5
}
6
6
}
7
7
8
- export class LinkedList {
8
+ class LinkedList {
9
9
constructor ( ) {
10
10
this . head = null ;
11
11
this . tail = null ;
@@ -103,6 +103,10 @@ export class LinkedList {
103
103
}
104
104
}
105
105
106
+ module . exports = LinkedList ;
107
+
108
+ /** Uncomment when testing only this file */
109
+ /**
106
110
const list = new LinkedList();
107
111
list.append(1);
108
112
list.append(2);
@@ -114,3 +118,4 @@ list.removeFromFront();
114
118
list.print();
115
119
list.removeFromEnd();
116
120
list.print();
121
+ */
You can’t perform that action at this time.
0 commit comments