After completing the series about the Doubly Linked List,
we start with the Stack.
O(N)
O(N)
O(1)
O(1)
We will use a Singly Linked List to build our Stack.
A <== B <== C (last)
C
is the last node we pushed (= added) on top of the StackC
has a pointer (next
) to the next node (B
)C
, the next node on top of the Stack should be B
We need the following parts to build our Stack:
// a Node has a value (`value`) and a pointer to the next node (`next`)
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
// a Stack has a length and a last item (`last`)
class Stack {
constructor() {
this.length = 0;
this.last = null;
}
}
We set up our Stack. Now we need at least two methods within the Stack:
push
pop
We will implement our first method for the Stack.
If you want to get notified, subscribe!
Hi! I'm Michael 👋 I'm a Mentor & Senior Web Developer - I help you to reach your (career) goals.