You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//I use GPT to generate accurate comments and also as a reference.
3
+
4
+
//shorthand for console.log()
5
+
letlog=console.log.bind(console);
6
+
7
+
//The array structure can be useful in JavaScript to simulate stacks and queues.
8
+
9
+
/* A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. You can think of it like a stack of plates: you add plates to the top and also remove them from the top.
10
+
Key Operations:
11
+
12
+
Push: Add an element to the top of the stack.
13
+
Pop: Remove the element from the top of the stack.
14
+
Peek/Top: Retrieve the top element without removing it.
log('Pop from empty stack2:',stack2.pop());// Stack is empty. Cannot pop an element. & null
99
+
100
+
101
+
//Cola(Queue)
102
+
103
+
/* A queue is a linear data structure that follows the First In, First Out (FIFO) principle. This means that the first element added to the queue is the first one to be removed. You can think of it like a line of people waiting for service: the first person in line is the first to be served.
104
+
Key Operations:
105
+
106
+
Enqueue: Add an element to the end of the queue.
107
+
Dequeue: Remove the element from the front of the queue.
108
+
Front/Peek: Retrieve the front element without removing it.
log('Dequeue from empty queue2:',queue2.dequeue());// Queue is empty. Cannot dequeue an element. & null
191
+
192
+
193
+
//Additional exercises:
194
+
/* #1 Simulate the behavior of the back and forward buttons in a browser. */
195
+
196
+
letdocumentsQueue=[
197
+
{name: 'Tratado de Tantra.txt',content: 'Here comes the content of Tratado de Tantra.'},
198
+
{name: 'Nada Sagrado.doc',content: 'Here comes the content of Nada Sagrado.'},
199
+
{name: 'El Blanco Invisible.pdf',content: 'Here comes the content of El Blanco Invisible.'}
200
+
];
201
+
202
+
functionprintQueue(arr){
203
+
if(arr.length==0){
204
+
log('There no element to print in the queue!')
205
+
return;
206
+
}
207
+
while(arr.length>0){
208
+
constdocument=arr.shift();// Get the first document
209
+
log('Printing document:',document.name);
210
+
log('Content:',document.content);
211
+
}
212
+
log('There no more element to print in the queue!')
213
+
}
214
+
215
+
printQueue(documentsQueue);/* Printing document: Tratado de Tantra.txt
216
+
Content: Here comes the content of Tratado de Tantra.
217
+
Printing document: Nada Sagrado.doc
218
+
Content: Here comes the content of Nada Sagrado.
219
+
Printing document: El Blanco Invisible.pdf
220
+
Content: Here comes the content of El Blanco Invisible.
221
+
There no more element to print in the queue! */
222
+
223
+
/* To read and print the contents of documents in JavaScript, you would typically need to use a file reading mechanism. However, JavaScript running in a browser environment does not have direct access to the file system for security reasons. Instead, you can simulate reading the contents of the documents by using predefined content or by using the File API if you're working with file inputs. */
224
+
225
+
leturlStack=[];
226
+
letcurrentIndex=-1;// To keep track of the current position.
227
+
228
+
functionbrowseWeb(str=prompt('Enter a URL or the word "forward" or "back" to navigate in the browsing history.')){
229
+
constback=()=>{
230
+
if(currentIndex>0){
231
+
currentIndex--;
232
+
constpreviousUrl=urlStack[currentIndex];
233
+
log('Location: ',previousUrl);
234
+
// window.location = previousUrl; // Navigate to the previous URL.
235
+
}else{
236
+
log("There are no more pages back..");
237
+
}
238
+
};
239
+
240
+
constforward=()=>{
241
+
if(currentIndex<urlStack.length-1){
242
+
currentIndex++;
243
+
constnextUrl=urlStack[currentIndex];
244
+
log('Location: ',nextUrl);
245
+
// window.location = nextUrl; // Navigate to the next URL.
246
+
}else{
247
+
log("There are no more pages forward.");
248
+
}
249
+
};
250
+
251
+
if(str!=='back'&&str!=='forward'){
252
+
if(currentIndex<urlStack.length-1){
253
+
urlStack=urlStack.slice(0,currentIndex+1);// Clear forward if navigation has occurred.
254
+
}// the standard behavior of a web browser's navigation history,
255
+
urlStack.push(str);
256
+
currentIndex++;
257
+
// window.location = str; // Navigate to the new URL.
0 commit comments