From the course: C# Algorithms

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Stack algorithms: Theorizing an algorithm

Stack algorithms: Theorizing an algorithm - C# Tutorial

From the course: C# Algorithms

Stack algorithms: Theorizing an algorithm

- [Instructor] As a software engineer, you'll need to know how to leverage the built-in stack functionality when working with algorithms that keep track of state. In this lesson, we'll look at how to use the stack data structure to solve software problems. Let's say we want to print the next greater element for every element in the array. The next greater element for a given element is the first greater element on the right side of the array. If no greater elements exist, then the next greater element is a sentinel value, negative one. For example, if we were given an array with 5, 6, 3, and 50, the next greater element for 5 would be 6. The next greater element for 6 would be 50 and the same for 3. 50 does not have a next greater element to the right, so its value would be negative one. In another example, let's say we have 15, 8, 4, and 10. 15 does not have a next greater element, so it would be negative one. Eight's next greater element to the right is 10 and the same for 4. 15 is…

Contents