File tree Expand file tree Collapse file tree 8 files changed +32
-153
lines changed
Expand file tree Collapse file tree 8 files changed +32
-153
lines changed Original file line number Diff line number Diff line change @@ -73,25 +73,16 @@ class SinglyCircularLinkedList {
7373 this . size --
7474 }
7575
76- printData ( ) {
76+ printData ( output = value => console . log ( value ) ) {
7777 let count = 0
7878 let current = this . head
7979
80- while ( current !== null && count !== this . size ) {
81- console . log ( current . data + '\n' )
80+ while ( current !== null && count < this . size ) {
81+ output ( current . data )
8282 current = current . next
8383 count ++
8484 }
8585 }
8686}
8787
88- const ll = new SinglyCircularLinkedList ( )
89-
90- ll . insert ( 10 )
91- ll . insert ( 20 )
92- ll . insert ( 30 )
93- ll . insert ( 40 )
94- ll . insert ( 50 )
95- ll . insertAt ( 5 , 60 )
96- ll . remove ( 5 )
97- ll . printData ( )
88+ export { SinglyCircularLinkedList }
Original file line number Diff line number Diff line change @@ -180,12 +180,12 @@ const LinkedList = (function () {
180180 }
181181
182182 // Function to view the LinkedList
183- LinkedList . prototype . view = function ( ) {
183+ LinkedList . prototype . view = function ( output = value => console . log ( value ) ) {
184184 let currentNode = this . head
185185 let count = 0
186186 while ( count < this . length ) {
187187 count ++
188- console . log ( currentNode . element )
188+ output ( currentNode . element )
189189 currentNode = currentNode . next
190190 }
191191 }
@@ -194,16 +194,4 @@ const LinkedList = (function () {
194194 return LinkedList
195195} ( ) )
196196
197- // Implementation of LinkedList
198- const linklist = new LinkedList ( )
199- linklist . add ( 2 )
200- linklist . add ( 5 )
201- linklist . add ( 8 )
202- linklist . add ( 12 )
203- linklist . add ( 17 )
204- console . log ( linklist . size ( ) )
205- console . log ( linklist . removeAt ( 4 ) )
206- linklist . addAt ( 4 , 15 )
207- console . log ( linklist . indexOf ( 8 ) )
208- console . log ( linklist . size ( ) )
209- linklist . view ( )
197+ export { LinkedList }
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ class CircularQueue {
2828 // REMOVES ELEMENTS
2929 dequeue ( ) {
3030 if ( this . checkEmpty ( ) ) {
31- console . log ( ' UNDERFLOW' )
31+ // UNDERFLOW
3232 return
3333 }
3434 const y = this . queue [ this . front ]
@@ -62,15 +62,15 @@ class CircularQueue {
6262 // Checks if max capacity of queue has been reached or not
6363 checkOverflow ( ) {
6464 if ( ( this . front === 1 && this . rear === this . maxLength ) || ( this . front === this . rear + 1 ) ) {
65- console . log ( ' CIRCULAR QUEUE OVERFLOW' )
65+ // CIRCULAR QUEUE OVERFLOW
6666 return true
6767 }
6868 }
6969
70- // Prints the entire array
71- display ( ) {
70+ // Prints the entire array ('*' represents blank space)
71+ display ( output = value => console . log ( value ) ) {
7272 for ( let index = 1 ; index < this . queue . length ; index ++ ) {
73- console . log ( this . queue [ index ] )
73+ output ( this . queue [ index ] )
7474 }
7575 }
7676
@@ -85,24 +85,4 @@ class CircularQueue {
8585 }
8686}
8787
88- function main ( ) {
89- // Star represents blank space
90- const queue = new CircularQueue ( 6 ) // Enter Max Length
91- queue . enqueue ( 1 )
92- queue . enqueue ( 15 )
93- queue . enqueue ( 176 )
94- queue . enqueue ( 59 )
95- queue . enqueue ( 3 )
96- queue . enqueue ( 55 )
97-
98- queue . display ( )
99-
100- queue . dequeue ( )
101- queue . dequeue ( )
102- queue . dequeue ( )
103- queue . display ( )
104-
105- console . log ( queue . peek ( ) )
106- }
107-
108- main ( )
88+ export { CircularQueue }
Original file line number Diff line number Diff line change @@ -43,38 +43,11 @@ const Queue = (function () {
4343 }
4444
4545 // List all the items in the queue
46- Queue . prototype . view = function ( ) {
47- console . log ( this . queue )
46+ Queue . prototype . view = function ( output = value => console . log ( value ) ) {
47+ output ( this . queue )
4848 }
4949
5050 return Queue
5151} ( ) )
5252
53- // Implementation
54- const myQueue = new Queue ( )
55-
56- myQueue . enqueue ( 1 )
57- myQueue . enqueue ( 5 )
58- myQueue . enqueue ( 76 )
59- myQueue . enqueue ( 69 )
60- myQueue . enqueue ( 32 )
61- myQueue . enqueue ( 54 )
62-
63- myQueue . view ( )
64-
65- console . log ( `Length: ${ myQueue . length ( ) } ` )
66- console . log ( `Front item: ${ myQueue . peek ( ) } ` )
67- console . log ( `Removed ${ myQueue . dequeue ( ) } from front.` )
68- console . log ( `New front item: ${ myQueue . peek ( ) } ` )
69- console . log ( `Removed ${ myQueue . dequeue ( ) } from front.` )
70- console . log ( `New front item: ${ myQueue . peek ( ) } ` )
71- myQueue . enqueue ( 55 )
72- console . log ( 'Inserted 55' )
73- console . log ( `New front item: ${ myQueue . peek ( ) } ` )
74-
75- for ( let i = 0 ; i < 5 ; i ++ ) {
76- myQueue . dequeue ( )
77- myQueue . view ( )
78- }
79-
80- // console.log(myQueue.dequeue()); // throws exception!
53+ export { Queue }
Original file line number Diff line number Diff line change @@ -45,28 +45,14 @@ const Stack = (function () {
4545 }
4646
4747 // To see all the elements in the stack
48- Stack . prototype . view = function ( ) {
49- for ( let i = 0 ; i < this . top ; i ++ ) { console . log ( this . stack [ i ] ) }
48+ Stack . prototype . view = function ( output = value => console . log ( value ) ) {
49+ for ( let i = 0 ; i < this . top ; i ++ ) {
50+ output ( this . stack [ i ] )
51+ }
5052 }
5153
5254 return Stack
5355} ( ) )
5456
55- // Implementation
56- const myStack = new Stack ( )
5757
58- myStack . push ( 1 )
59- myStack . push ( 5 )
60- myStack . push ( 76 )
61- myStack . push ( 69 )
62- myStack . push ( 32 )
63- myStack . push ( 54 )
64- console . log ( myStack . size ( ) )
65- console . log ( myStack . peek ( ) )
66- console . log ( myStack . pop ( ) )
67- console . log ( myStack . peek ( ) )
68- console . log ( myStack . pop ( ) )
69- console . log ( myStack . peek ( ) )
70- myStack . push ( 55 )
71- console . log ( myStack . peek ( ) )
72- myStack . view ( )
58+ export { Stack }
Original file line number Diff line number Diff line change @@ -53,16 +53,5 @@ class Stack {
5353 return el instanceof Stack
5454 }
5555}
56- const newStack = new Stack ( )
57- console . log ( 'Is it a Stack?,' , Stack . isStack ( newStack ) )
58- console . log ( 'Is stack empty? ' , newStack . isEmpty )
59- newStack . push ( 'Hello world' )
60- newStack . push ( 42 )
61- newStack . push ( { a : 6 , b : 7 } )
62- console . log ( 'The length of stack is ' , newStack . length )
63- console . log ( 'Is stack empty? ' , newStack . isEmpty )
64- console . log ( 'Give me the last one ' , newStack . last )
65- console . log ( 'Pop the latest ' , newStack . pop ( ) )
66- console . log ( 'Pop the latest ' , newStack . pop ( ) )
67- console . log ( 'Pop the latest ' , newStack . pop ( ) )
68- console . log ( 'Is stack empty? ' , newStack . isEmpty )
56+
57+ export { Stack }
Original file line number Diff line number Diff line change @@ -32,13 +32,13 @@ const Node = (function Node () {
3232 }
3333
3434 // Visit a node
35- Node . prototype . visit = function ( ) {
35+ Node . prototype . visit = function ( output = value => console . log ( value ) ) {
3636 // Recursively go left
3737 if ( this . left !== null ) {
3838 this . left . visit ( )
3939 }
4040 // Print out value
41- console . log ( this . value )
41+ output ( this . value )
4242 // Recursively go right
4343 if ( this . right !== null ) {
4444 this . right . visit ( )
@@ -115,7 +115,7 @@ const Tree = (function () {
115115 // Inorder traversal
116116 Tree . prototype . traverse = function ( ) {
117117 if ( ! this . root ) {
118- console . log ( ' No nodes are there in the tree till now' )
118+ // No nodes are there in the tree till now
119119 return
120120 }
121121 this . root . visit ( )
@@ -124,11 +124,11 @@ const Tree = (function () {
124124 // Start by searching the root
125125 Tree . prototype . search = function ( val ) {
126126 const found = this . root . search ( val )
127- if ( found === null ) {
128- console . log ( val + ' not found' )
129- } else {
130- console . log ( 'Found:' + found . value )
127+ if ( found !== null ) {
128+ return found . value
131129 }
130+ // not found
131+ return null
132132 }
133133
134134 // Add a new value to the tree
@@ -151,16 +151,4 @@ const Tree = (function () {
151151 return Tree
152152} ( ) )
153153
154- // Implementation of BST
155- const bst = new Tree ( )
156- bst . addValue ( 6 )
157- bst . addValue ( 3 )
158- bst . addValue ( 9 )
159- bst . addValue ( 2 )
160- bst . addValue ( 8 )
161- bst . addValue ( 4 )
162- bst . traverse ( )
163- bst . search ( 8 )
164- bst . removeValue ( 3 )
165- bst . removeValue ( 8 )
166- bst . traverse ( )
154+ export { Tree }
Original file line number Diff line number Diff line change @@ -118,20 +118,4 @@ Trie.prototype.findOccurences = function (word) {
118118 return node . count
119119} ;
120120
121- // To test
122- ( function demo ( ) {
123- const x = new Trie ( )
124- x . insert ( 'sheldon' )
125- x . insert ( 'hello' )
126- x . insert ( 'anyword' )
127- x . insert ( 'sheldoncooper' )
128- console . log ( x . findOccurences ( 'sheldon' ) )
129- x . remove ( 'anything' )
130- x . insert ( 'sheldon' )
131- console . log ( x . findOccurences ( 'sheldon' ) )
132- console . log ( x . findAllWords ( 'sheldon' ) )
133- x . insert ( 'anything' )
134- x . remove ( 'sheldoncooper' )
135- console . log ( x . contains ( 'sheldoncooper' ) )
136- console . log ( x . findAllWords ( 'sheldon' ) )
137- } ) ( )
121+ export { Trie }
You can’t perform that action at this time.
0 commit comments