-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeQueWithStacks.js
More file actions
147 lines (126 loc) · 2.92 KB
/
MakeQueWithStacks.js
File metadata and controls
147 lines (126 loc) · 2.92 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
// --- Directions
// Implement a Queue datastructure using two stacks.
// *Do not* create an array inside of the 'Queue' class.
// Queue should implement the methods 'add', 'remove', and 'peek'.
// For a reminder on what each method does, look back
// at the Queue exercise.
// --- Examples
// const q = new Queue();
// q.add(1);
// q.add(2);
// q.peek(); // returns 1
// q.remove(); // returns 1
// q.remove(); // returns 2
class Stack {
constructor() {
this.data = [];
}
push(record) {
this.data.push(record);
}
pop() {
return this.data.pop();
}
peek() {
return this.data[this.data.length - 1];
}
}
class Queue {
constructor() {
this.first = new Stack();
this.second = new Stack();
}
add(item) {
this.first.push(item)
}
remove(){
while(this.first.peek() !== undefined) {
this.second.push(this.first.pop())
}
const item = this.second.pop()
while (this.second.peek() !== undefined){
this.first.push(this.second.pop())
}
return item;
}
peek() {
while (this.first.peek() !== undefined){
this.second.push(this.first.pop())
}
const item = this.second.peek()
while (this.second.peek() !== undefined){
this.first.push(this.second.pop())
}
return item;
}
}
// Works, but it's a bit hacky, and I want to re do it later.
class Node{
constructor(data,next = null){
this.data = data;
this.next = next;
}
}
class Stack{
constructor(){
this.first = null;
this.last = null;
this.size = 0;
}
push(data){
let node = new Node(data)
if (!this.first){
this.first = node;
this.last = node;
return this.size++
}
let temp = this.first;
this.first = node;
this.first.next = temp;
return this.size++
}
pop(){
if (!this.first)
return null;
var temp = this.first;
if (this.first==this.last) {
this.last=null;
}
this.first = this.first.next;
this.size--;
return temp.data
}
peek(){
return this.first
}
}
// var stack = new Stack();
// stack.push(1)
// stack.push(2)
// stack.push(3)
// stack.pop()
// stack.pop()
class myQueue {
constructor(){
this.head = new Stack();
this.second = new Stack();
}
enqueue(data){
this.head.push(data)
}
dequeue(){
while (this.head.peek() !== null){
this.second.push(this.head.pop())
}
let item = this.second.pop();
while(this.second.peek() !== null){
this.head.push(this.second.pop())
}
return item;
}
}
var Queue = new myQueue();
Queue.enqueue(1)
Queue.enqueue(2)
Queue.enqueue(3)
Queue.dequeue()