-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimalShelter.js
102 lines (86 loc) · 2 KB
/
animalShelter.js
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
class AnimalShelter {
constructor() {
this.size = 0;
this.head = null;
this.tail = null;
}
enqueue(animal, category) {
let node = new Node(animal, category);
if (this.tail != null) {
node.prev = this.tail;
node.prev.next = node;
this.tail = node;
} else {
this.head = this.tail = node;
}
this.size++;
}
dequeueAny() {
let animal = this.tail.data;
let prev = this.tail.prev;
this.tail = prev;
prev.next = null;
return console.log(animal);
}
dequeueDog() {
let curr = this.head;
let currDog = null;
while (curr != null) {
if (curr.category == "Dog") {
currDog = curr;
}
curr = curr.next;
}
if (currDog != null) {
if (this.tail == currDog) {
let prev = this.tail.prev;
this.tail = prev;
prev.next = null;
return console.log(currDog.data);
} else {
currDog.prev.next = currDog.next;
return console.log(currDog.data);
}
} else {
return console.log("No Dogs Availaible");
}
}
dequeueCat() {
let curr = this.head;
let currCat = null;
while (curr != null) {
if (curr.category == "Cat") {
currCat = curr;
}
curr = curr.next;
}
if (currCat != null) {
if (this.tail == currCat) {
let prev = this.tail.prev;
this.tail = prev;
prev.next = null;
return console.log(currCat.data);
} else {
currCat.prev.next = currCat.next;
return console.log(currCat.data);
}
} else {
return console.log("No Cats Availaible");
}
}
}
class Node {
constructor(data, category, prev, next) {
this.category = category;
this.data = data;
this.prev = prev || null;
this.next = next || null;
}
}
let shelter = new AnimalShelter();
shelter.enqueue("D1", "Dog");
shelter.enqueue("D2", "Dog");
shelter.enqueue("C1", "Cat");
shelter.enqueue("C2", "Cat");
shelter.enqueue("C3", "Cat");
shelter.dequeueDog();