2 Added array version
source | link

LKM simply means a manually handled stack, for example a linked list where you push elements at the head and then pop them out again. It'd be faster because it's smaller, less data to shuffle around.

It's simply implemented by having a field in your struct that's a pointer (or reference if you wish) to the next element, and a head variable that's the top of the stack. When you push, you set the next pointer of your pushed element to the current value of the head variable, and then set the head variable to reference the pushed element. Popping does the inverse.

Another way to implement it, if you know the maximum stack of the stack, is to have a normal array and an index variable which tells where in the array the head is. That's actually how the recursion call stack is implemented, if you simplify it.

I believe most of the languages you mention above also have special constructs to treat arrays as stacks, usually called push/pop or shift/unshift.

LKM simply means a manually handled stack, for example a linked list where you push elements at the head and then pop them out again. It'd be faster because it's smaller, less data to shuffle around.

It's simply implemented by having a field in your struct that's a pointer (or reference if you wish) to the next element, and a head variable that's the top of the stack. When you push, you set the next pointer of your pushed element to the current value of the head variable, and then set the head variable to reference the pushed element. Popping does the inverse.

I believe most of the languages you mention above also have special constructs to treat arrays as stacks, usually called push/pop or shift/unshift.

LKM simply means a manually handled stack, for example a linked list where you push elements at the head and then pop them out again. It'd be faster because it's smaller, less data to shuffle around.

It's implemented by having a field in your struct that's a pointer (or reference if you wish) to the next element, and a head variable that's the top of the stack. When you push, you set the next pointer of your pushed element to the current value of the head variable, and then set the head variable to reference the pushed element. Popping does the inverse.

Another way to implement it, if you know the maximum stack of the stack, is to have a normal array and an index variable which tells where in the array the head is. That's actually how the recursion call stack is implemented, if you simplify it.

I believe most of the languages you mention above also have special constructs to treat arrays as stacks, usually called push/pop or shift/unshift.

1
source | link

LKM simply means a manually handled stack, for example a linked list where you push elements at the head and then pop them out again. It'd be faster because it's smaller, less data to shuffle around.

It's simply implemented by having a field in your struct that's a pointer (or reference if you wish) to the next element, and a head variable that's the top of the stack. When you push, you set the next pointer of your pushed element to the current value of the head variable, and then set the head variable to reference the pushed element. Popping does the inverse.

I believe most of the languages you mention above also have special constructs to treat arrays as stacks, usually called push/pop or shift/unshift.