-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalg_course.js
More file actions
2827 lines (2724 loc) · 81.5 KB
/
alg_course.js
File metadata and controls
2827 lines (2724 loc) · 81.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
//Stack Data Structure
class firstStack {
constructor() {
this.storage = [];
this.count = 0;
}
//this method will adds element to the end of the stack
push(element) {
this.storage[this.count] = element;
this.count++;
}
//this method will check the stack is empty or not!
isEmpty() {
return this.count === 0;
}
//this method will removes element from the top of the stack
pop() {
if (this.isEmpty() === false) {
this.count = this.count - 1;
return this.storage.pop();
}
}
//this method will peek an element from the top of the stack
peek() {
return this.storage[this.count - 1];
}
//this method will returns the size of the stack
size() {
return this.count;
}
}
const firststack = new firstStack();
const secondstack = new firstStack();
firststack.push("Shawn");
firststack.push("Revu");
secondstack.push("Gias Uddin");
secondstack.push("Johora Akter");
console.log(firststack);
console.log(firststack.size());
console.log(firststack.pop());
console.log(firststack);
firststack.push("Torikul Islam");
firststack.push("Niamul Hakim");
console.log(firststack);
console.log(secondstack);
console.log(secondstack.size());
console.log(secondstack.pop());
console.log(secondstack.peek());
console.log(secondstack);
}
//creating one more stack data structure with function
{
//in a stack we need 4 things to make it such as "push, pop, peek, length"
let myStack = function () {
this.count = 0;
this.storage = {};
//Adds a value onto the end of the stack
this.push = function (value) {
this.storage[this.count] = value;
this.count++;
};
//Removes an element from the top of the stack
this.pop = function () {
if (this.count === 0) {
return undefined;
} else {
this.count--;
let result = this.storage[this.count];
delete this.storage[this.count];
return result;
}
};
//Peek an element from the top of the stack
this.peek = function () {
return this.storage[this.count - 1];
};
//Returns the size of the stack
this.size = function () {
return this.count;
};
};
const myS = new myStack();
myS.push("Apple");
myS.push("Orange");
myS.push("Banana");
//console.log(myS);
console.log(myS.pop());
//console.log(myS);
console.log(myS.peek());
console.log(myS.size());
}
{
//creating palindrome cheaker
const palindrome = function () {
let stack = [];
let word = "Bob";
let emptyWord = "";
let lengthOfWord = word.length;
for (let i = 0; i < lengthOfWord; i++) {
stack.push(word[i]);
}
for (let j = 0; j < lengthOfWord; j++) {
emptyWord += stack.pop();
}
if (word.toLowerCase() === emptyWord.toLowerCase()) {
console.log(word + " is a palindrome.");
} else {
console.log(`${word} is not a palindrome.`);
}
};
palindrome();
}
{
//Creating one more palindrome crecker
let palindrome = function (word) {
let stack = [];
let wordOfStack = word;
let emptyWord = "";
let lOfWord = wordOfStack.length;
for (let i = 0; i < lOfWord; i++) {
stack.push(wordOfStack[i]);
}
for (let j = 0; j < lOfWord; j++) {
emptyWord += stack.pop();
}
//Checking the condition for palindrome
if (wordOfStack === emptyWord) {
console.log(`${wordOfStack} is a palindrome.`);
} else {
console.log(`${wordOfStack} is not a palindrome`);
}
};
//Calling the 'palindrome' function and passing a value for checking is it palindrome or not!
palindrome("Racecar");
}
{
//Creating one more palindrome checker
let palindromeChecker = (val) => {
let empStack = [];
let word = val;
let empWord = "";
let lenWord = word.length;
for (let a = 0; a < lenWord; a++) {
empStack.push(word[a]);
}
for (let b = 0; b < lenWord; b++) {
empWord += empStack.pop();
}
if (word !== empWord) {
console.log(`${word} is not a palindrome.`);
} else {
console.log(`${word} is a palindrome`);
}
};
palindromeChecker("racecar");
}
//Creating the last palindrome checker
{
let myPalindrome = function (value) {
let emptyArray = [];
let word = value;
let emptyWord = "";
let lengthOfWord = word.length;
for (let i = 0; i < lengthOfWord; i++) {
emptyArray.push(word[i]);
}
for (let j = 0; j < lengthOfWord; j++) {
emptyWord += emptyArray.pop();
}
if (word === emptyWord) {
console.log(`${word} is a palindrome.`);
} else {
console.log(`${word} is not a palindrome.`);
}
};
myPalindrome("racecar");
}
//Creating stack data structure
{
//using function to create my first stack
let creatingStack = function () {
this.store = {};
this.count = 0;
//Adds a value at the end of the stack
this.push = function (element) {
this.store[this.count] = element;
this.count++;
};
//Removing an element from the top of the stack
this.pop = function () {
if (this.count === 0) {
return undefined;
} else {
this.count--;
let result = this.store[this.count];
delete this.store[this.count];
return result;
}
};
//Peeking an element from the top of the stack
this.peek = function () {
return this.store[this.count - 1];
};
//Checking the length of the stack
this.size = function () {
return this.count;
};
};
const mySTAck = new creatingStack();
mySTAck.push("Apple");
mySTAck.push("Banana");
mySTAck.push("Jack Fruits");
console.log(mySTAck);
console.log(mySTAck.size());
console.log(mySTAck.pop());
console.log(mySTAck.peek());
console.log(mySTAck);
}
//Creating one more stack using class
{
class mySt {
constructor() {
this.storage = [];
this.count = 0;
}
//adding an element of the end of the stack
push(value) {
this.storage[this.count] = value;
this.count++;
}
//checking the stack is empty or not!
isEmpty() {
return this.count === 0;
}
//Removing an element from the top of the stack
pop() {
if (this.isEmpty() === false) {
this.count = this.count - 1;
return this.storage.pop();
}
}
//Peeking an element from the top of the stack
peek() {
return this.storage[this.count - 1];
}
//Checking the length of the of the stack
size() {
return this.count;
}
}
//creating two variables
let mSTa = new mySt();
let MstA = new mySt();
//adding value to the first variable
mSTa.push("Shawn");
mSTa.push("Revu");
mSTa.push("Ziko");
console.log(mSTa);
console.log(mSTa.peek());
console.log(mSTa.size());
//adding value to the second stack
MstA.push("MD Niamul Hakim");
MstA.push("MD Tarikul Islam");
MstA.push("MD Zahidul Hassan");
console.log(MstA);
console.log(MstA.peek());
console.log(MstA.size());
}
//Creating one more 'stack data structure' using 'class data type'
{
class Mstack {
constructor() {
this.store = [];
this.count = 0;
}
//Adding an element at the end of the stack
push(element) {
this.store[this.count] = element;
this.count++;
}
//Checking the stack is empty or not!
isEmpt() {
return this.count === 0;
}
//Removing the top element from the stack
pop() {
if (this.isEmpt() === false) {
this.count = this.count - 1;
return this.store.pop();
}
}
//Peeking the top element from the stack
peek() {
return this.store[this.count - 1];
}
//Checking the size of the stack
size() {
return this.count;
}
}
//Creating one variable for checking the stack
let firstStack = new Mstack();
//Adding some values
firstStack.push("Marcedes Benz");
firstStack.push("Audi");
firstStack.push("Lamborgini");
console.log(firstStack);
console.log(firstStack.peek());
console.log(firstStack.size());
console.log(firstStack.pop());
console.log(firstStack);
}
{
//Creating palindrome checker
let palindrome = function (value) {
let emptyStack = [];
let word = value;
let empWord = "";
let len = word.length;
for (let i = 0; i < len; i++) {
emptyStack.push(word[i]);
}
for (let j = 0; j < len; j++) {
empWord += emptyStack.pop();
}
if (word === empWord) {
console.log(`${word} is a palindrome.`);
} else {
console.log(`${word} is not a palindrome.`);
}
};
palindrome("lol");
//Creating a stack data structure using "class data type"
//we know that we need "push, pop, peek, size" method for creating a stack
class st {
constructor() {
this.storage = [];
this.count = 0;
}
//this method will adds an element at the end of the stack
push(element) {
this.storage[this.count] = element;
this.count++;
}
//this method will check for the stack is empty or not!
isEmpty() {
return this.count === 0;
}
//this method will removes an element from the top of the stack
pop() {
if (this.isEmpty() === false) {
this.count = this.count - 1;
return this.storage.pop();
}
}
//this method will peek an element from the top of the stack
peek() {
return this.storage[this.count - 1];
}
//this method will returns the size of the stack
size() {
return this.count;
}
}
let firstSt = new st();
//pushing element to the stack
firstSt.push("shawn");
firstSt.push("unknown");
//poping the top element
console.log(firstSt.pop());
//peeking the top element from the stack
console.log(firstSt.peek());
//checking the length
console.log(firstSt.size());
console.log(firstSt);
//creating stack data structure using "function data type"
let ST = function () {
this.store = {};
this.count = 0;
//Adding an element to the end of the stack
this.push = function (element) {
this.store[this.count] = element;
this.count++;
};
//Removing an element from the top of the stack
this.pop = function () {
if (this.count === 0) {
return undefined;
} else {
this.count--;
let result = this.store[this.count];
delete this.store[this.count];
return result;
}
};
//peeking the top element from the stack
this.peek = function () {
return this.store[this.count - 1];
};
//this method will returns the length of the stack
this.size = function () {
return this.count;
};
};
//creating variables for stack
let secondStack = new ST();
//adding elements to the stack
secondStack.push("MD");
secondStack.push("Niamul");
secondStack.push("Hakim");
//poping the top element from the stack
console.log(secondStack.pop());
//peeking the top element from the stack
console.log(secondStack.peek());
//checking the size of the stack
console.log(secondStack.size());
//checking the full stack
console.log(secondStack);
//Creating "set" data structure
let mYSET = function () {
//the "let collection" will hold the set
let collection = [];
//this method will check for the presence of an element and returns 'true' or 'false'
this.has = function (element) {
return collection.indexOf(element) !== -1;
};
//this method will returns all the values of the set
this.values = function () {
return collection;
};
//this method will add element to the set
this.add = function (element) {
if (!this.has(element)) {
collection.push(element);
return true;
} else {
return false;
}
};
//this method will remove element from the set
this.remove = function (element) {
if (this.has(element)) {
let index = collection.indexOf(element);
collection.splice(index, 1);
return true;
} else {
return false;
}
};
//this method will returns the size of the set
this.size = function () {
return collection.length;
};
//this method will returns the uninon of two set as a new set
this.union = function (otherSet) {
let unionSet = new mYSET();
let firstSet = this.values();
let secondSet = otherSet.values();
firstSet.forEach(function (e) {
unionSet.add(e);
});
secondSet.forEach(function (e) {
unionSet.add(e);
});
return unionSet;
};
//this method will returns the intersection of two set as a new set
this.intersection = function (otherSet) {
let intersectionSet = new mYSET();
let firstSet = this.values();
firstSet.forEach(function (e) {
if (otherSet.has(e)) {
intersectionSet.add(e);
}
});
return intersectionSet;
};
//this method will return the difference of two set as a new set
this.difference = function (otherSet) {
let differenceSet = new mYSET();
let firstSet = this.values();
firstSet.forEach(function (e) {
if (!otherSet.has(e)) {
differenceSet.add(e);
}
});
return differenceSet;
};
//this method will test if a set is subset of otherset then it will return 'true' or 'false'
this.subset = function (otherSet) {
let firstSet = this.values();
return firstSet.every(function (value) {
return otherSet.has(value);
});
};
};
//Creating two set
let setA = new mYSET();
let setB = new mYSET();
//adding element to setA
setA.add("a");
setA.add("b");
setA.add("c");
setA.add("d");
console.log(setA.values());
//adding element to setB
setB.add("a");
console.log(setB.values());
console.log(setB.has("b"));
console.log(setB.has("a"));
//deleting a element from setA
console.log(setA.remove("d"));
console.log(setA.values());
//checking the subset
console.log(setB.subset(setA));
//checking the "intersection" of two set
console.log(setB.intersection(setA).values());
console.log(setA.intersection(setB).values());
//checking the "difference" of two set
console.log(setA.difference(setB).values()); //This will returns ['b','c']
console.log(setB.difference(setA).values()); //this will returns [] "empty set"
//checking the "union" of two set
console.log(setA.union(setB).values());
}
//Queue data structure
{
let myFirstQueue = function () {
//this "let collection" will hold all the queues value
let collection = [];
//this method will returns all the queues value
this.print = function () {
console.log(collection);
};
//this method will adds an element at the top of the queue
this.enqueue = function (element) {
return collection.push(element);
};
//this method will removes an element from the top of queue
this.dequeue = function () {
return collection.shift();
};
//this method will returns the top element but not gonna remove
this.front = function () {
return collection[0];
};
//this method will returns the length of the queue
this.size = function () {
return collection.length;
};
//this method will check if the queue is empty or not and returns true or false
this.isEmpty = function () {
return collection.length === 0;
};
};
//creating variable for queue set
let newQueue = new myFirstQueue();
newQueue.enqueue("Shawn");
newQueue.enqueue("Revu");
newQueue.enqueue("Shain");
newQueue.enqueue("Shakil");
newQueue.print();
newQueue.dequeue();
newQueue.print();
console.log(newQueue.size());
console.log(newQueue.isEmpty());
}
//creating one more 'queue data structure'
let queue = function () {
//The "let collection" will hold all the queue values
let collection = [];
//this method will returns full queue values
this.print = function () {
console.log(collection);
};
//this method will add an elements at top of the queue
this.enqueue = function (element) {
return collection.push(element);
};
//this method will removes an element from the top of the queue
this.dequeue = function () {
return collection.shift();
};
//this method will peek the top element from the queue
this.front = function () {
return collection[0];
};
//this method will returns the length of the queue
this.size = function () {
return collection.length;
};
//this method will check if the queue is empty or not and returns "true" or "false"
this.isEmpty = function () {
return collection.length === 0;
};
};
//Creating a variable for this queue
let newQue = new queue();
//adding values to the queue
newQue.enqueue("BMW");
newQue.enqueue("Audi");
newQue.enqueue("Ferari");
newQue.print();
newQue.dequeue();
newQue.print();
console.log(newQue.front());
console.log(newQue.size());
console.log(newQue.isEmpty());
newQue.print();
//Creating "priority queue"
let priorityQueue = function () {
"use strict";
//This will hold the priority queue values
let collection = [];
//this method will returns the full queue
this.printPriority = function () {
console.log(collection);
};
//this method will adds element to the queue according to the priority of the element
this.enqueue = function (element) {
//check the collection is empty or not, if empty then push the element
if (this.isEmpty()) {
collection.push(element);
} else {
//set a flag to check if element added
//this is to determine is the for loop found a match
let added = false;
for (let i = 0; i < collection.length; i++) {
//if the element at the index 'i' in the collection has
//higher priority you need the splice the array at that
//index and insert the element there
if (element[1] < collection[i][1]) {
//checking priorities
//splice the array at that index and insert the element
collection.splice(i, 0, element);
//satisfy the added condition
added = true;
//break the for loop
break;
}
}
//if not added in the loop
if (!added) {
//push to the end on the collection
collection.push(element);
}
}
};
//this method will removes element from the queue
this.dequeue = function () {
let value = collection.shift();
return value[0];
};
//this method will peeek the top element from the queue
this.front = function () {
return collection[0];
};
//this method will returns the size of the queue
this.size = function () {
return collection.length;
};
//this method will check if the queue is empty or not and returns "true" or "false"
this.isEmpty = function () {
return collection.length === 0;
};
};
//creating one variable for queue
let nQueue = new priorityQueue();
nQueue.enqueue(["Hello", 1]);
nQueue.enqueue(["I'm fine", 3]);
nQueue.enqueue(["How are you?", 2]);
nQueue.printPriority();
{
//creating a queue
let secondQueue = function () {
//this array will hold all the queues value
let arrayCollection = [];
//this method will returns the full queue elements
this.print = function () {
console.log(arrayCollection);
};
//this method will adds an element to the queue arrayCollection
this.enqueue = function (element) {
return arrayCollection.push(element);
};
//this method will removes element from the queue arrayCollection
this.dequeue = function () {
return arrayCollection.shift();
};
//this method will returns the size of the arrayCollection
this.size = function () {
return arrayCollection.length;
};
//this method will check if the arrayCollection is empty or not and returns "true" or "false"
this.isEmpty = function () {
return arrayCollection.length === 0;
};
};
//creating variable for the "queue"
let neWQ = new secondQueue();
//adding elements to the queue
neWQ.enqueue("Shawn");
neWQ.enqueue("Hao Jia");
neWQ.enqueue("Sword of peace");
neWQ.print();
console.log(neWQ.dequeue());
console.log(neWQ.size());
neWQ.print();
//Creating "Priority Queue" using "function data type"
let PriorityQueue = function () {
//this array will hold the queue values
let collection = [];
//this method will returns the full array values
this.print = function () {
console.log(collection);
};
//Here is the main part for "Priority queue" data structure
//In 'priority queue' you can add values to the collection array according to its priority
this.enqueue = function (element) {
//In this proccess, first of all we need to check if the collection is empty or not!
//If empty then push element to it
if (this.isEmpty()) {
collection.push(element);
} else {
//Set a flag to check if element is added
let added = false;
//this is to determine is the for loop found a match
for (let i = 0; i < collection.length; i++) {
//If the element at the index "i" in the collection has
//higher priority you need to splice the array at that
//index and insert the element there
if (element[1] < collection[i][1]) {
//Spliceing the array at that index and inserting the element
collection.splice(i, 0, element);
//Satisfy the added condition
added = true;
//breaking the for loop
break;
}
}
//if element not added in for loop
if (!added) {
collection.push(element);
}
}
};
//this method will removes element from the collection array
this.dequeue = function () {
let value = collection.shift();
return value[0];
};
//this method will peek the top element from the collection array
this.front = function () {
return collection[0];
};
//this method will returns the size of the array
this.size = function () {
return collection.length;
};
//this method will check if the collection array is empty or not!
this.isEmpty = function () {
return collection.length === 0;
};
};
//creating a queue
let neQueue = new PriorityQueue();
neQueue.enqueue(["Shawn", 2]);
neQueue.enqueue(["Hi", 1]);
neQueue.enqueue(["How are you?", 3]);
neQueue.print();
console.log(neQueue.dequeue());
console.log(neQueue.size());
console.log(neQueue.isEmpty());
}
{
//Creating a palindrome checker
let PALINDROME = function (value) {
let emptyStack = [];
let word = value;
let emptyREVword = "";
let lenOFword = word.length;
for (let i = 0; i < lenOFword; i++) {
emptyStack.push(word[i]);
}
for (let j = 0; j < lenOFword; j++) {
emptyREVword += emptyStack.pop();
}
if (word === emptyREVword) {
console.log(`${word} is a palindrome.`);
} else {
console.log(`${word} is not a palindrome`);
}
};
//Checking one word for palindrome
PALINDROME("BOB");
}
{
//Creating a "stack data structure" using "class data type"
//We need 4 things to create stack data structure such as "push, pop, peek, size"
class precticeStack {
constructor() {
this.storage = [];
this.count = 0;
}
//This method will check if the stack is empty or not!
ISempty() {
return this.count === 0;
}
//this method will adds an element to the end of the stack
push(element) {
this.storage[this.count] = element;
this.count++;
}
//this method will removes element from the top of the stack
pop() {
if (this.ISempty() === false) {
this.count = this.count - 1;
return this.storage.pop();
}
}
//this method will peek the top element from the stack
peek() {
return this.storage[this.count - 1];
}
//this method will returns the length of the stack
size() {
return this.count;
}
//this method will returns the full array
print() {
console.log(this.storage);
}
}
//Creating a variable to check this stack
let sOFStack = new precticeStack();
sOFStack.push("Shawn");
sOFStack.push("Revu");
sOFStack.push("Ziko");
sOFStack.print();
console.log(sOFStack);
console.log(sOFStack.pop());
console.log(sOFStack.peek());
console.log(sOFStack.size());
sOFStack.print();
}
{
//Creating one more stack using "function data type"
let stackofpra = function () {
this.store = [];
this.count = 0;
//this method will adds an element at the end of the stack
this.push = function (element) {
this.store[this.count] = element;
this.count++;
};
//this method will removes an element from the top of the stack
this.pop = function () {
if (this.count === 0) {
return undefined;
} else {
this.count--;
let result = this.store[this.count];
delete this.store[this.count];
return result;
}
};
//this method will peek the top element from the stack
this.peek = function () {
return this.store[this.count - 1];
};
//this method will returns the size of the stack
this.size = function () {
return this.count;
};
//this method will return the full array
this.print = function () {
return this.store;
};
};
//Creating variable to check this stack
let check = new stackofpra();
//checking other stack methods
check.push("Apple");
check.push("Banana");
check.push("Orange");
console.log(check.print());
// console.log(check);
console.log(check.pop());
console.log(check.size());
console.log(check);
console.log(check.peek());
}
{
//Creating "set data structure" using "function data type"
let SET = function () {
//this array will hold all the values
let collection = [];
//this method will check the presence of the element in the collection
//If found match then returns "true" otherwise returns "false"
this.has = function (element) {
return collection.indexOf(element) !== -1;
};
//this method will returns the full collection array
this.values = function () {
return collection;
};
//this method will adds an element to the set
this.add = function (element) {
if (!this.has(element)) {
collection.push(element);
return true;
} else {
return false;
}
};
//this method will removes elements from the set
this.remove = function (element) {
if (this.has(element)) {
let index = collection.indexOf(element);
collection.splice(index, 1);
return true;
} else {
return false;
}
};
//this method will check the length of the set
this.size = function () {
return collection.length;
};
//this method will check the union of two set and returns a new set
this.union = function (otherSet) {
let unionSet = new SET();
let firstSet = this.values();
let secondSet = otherSet.values();
firstSet.forEach(function (e) {
unionSet.add(e);
});
secondSet.forEach(function (e) {
unionSet.add(e);
});
return unionSet;
};
//this method will returns the intersection of two set as a new set
this.intersection = function (otherSet) {
let intersectionSet = new SET();
let firstSet = this.values();
firstSet.forEach(function (e) {
if (otherSet.has(e)) {
intersectionSet.add(e);
}
});
return intersectionSet;
};
//this method will returns the difference of two set as a new set
this.difference = function (otherSet) {
let differenceSet = new SET();
let firstSet = this.values();
firstSet.forEach(function (e) {
if (!otherSet.has(e)) {
differenceSet.add(e);
}
});
return differenceSet;
};
//this method will check if one set is the subset of otherset
//if the condition found match then returns "true" otherwise "false"
this.subset = function (otherSet) {
let firstSet = this.values();
return firstSet.every(function (value) {
return otherSet.has(value);
});
};