-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03. Stack .js
More file actions
95 lines (70 loc) · 2.58 KB
/
03. Stack .js
File metadata and controls
95 lines (70 loc) · 2.58 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
// Stack in JavaScript
// A stack is a data structure that follows the Last In First Out (LIFO) principle.
// JavaScript does not have a built-in Stack class, but you can easily implement a stack using an Array
// The last element added to the stack is the first one to be removed.
// Basic operations of a stack include:
// 1. push: Add an element to the top of the stack.
// 2. pop: Remove the top element from the stack.
// 3. peek or top: Get the top element of the stack without removing it.
// 4. isEmpty: Check if the stack is empty.
// ----------------------------------------------------------
// Example of a simple stack implementation using an array
let stack = []; // Initialize an empty stack
// Push elements onto the stack
stack.push(10);
stack.push(20);
stack.push(30);
console.log("After pushing:", stack); // Output: [10, 20, 30]
// Popping elements from the stack
let popped = stack.pop();
console.log("Popped element:", popped); // Output: 30
console.log("Stack after pop:", stack); // Output: [10, 20]
// Peek at the top element of the stack
let top = stack[stack.length - 1];
console.log("Top element:", top); // Output: 20
// Check if the stack is empty
console.log("Is stack empty?", stack.length === 0); // Output: false
/*
Visual Representation :
Stack (Top ↓)
| 30 | ← Pushed last, removed first
| 20 |
| 10 | ← First pushed
Stack = last added is first removed (LIFO).
push() adds to top.
pop() removes from top.
*/
// Stack is used wherever you need to remember the history of operations, and reverse or replay actions.
// ----------------------------------------------------------
let undoStack = []; // Stack for undo operations
let redoStack = []; // Stack for redo operations
let content = ""; // Current content
function type(newText) {
undoStack.push(content); // Save current content to undo stack
content += newText; // Update content
redoStack = []; // Clear redo stack after new action
}
function undo() {
if (undoStack.length > 0) {
redoStack.push(content);
content = undoStack.pop(); // Restore last content from undo stack
} else {
console.log("Nothing to undo");
}
}
function redo() {
if (redoStack.length > 0) {
undoStack.push(content);
content = redoStack.pop(); // Restore last content from redo stack
} else {
console.log("Nothing to redo");
}
}
// Usage :
type("Hello");
type(" World");
console.log("Typed", content); // Output: "Hello World"
undo();
console.log("After undo:", content); // Output: "Hello"
redo();
console.log("After redo:", content); // Output: "Hello World"