Skip to main content
added 2 characters in body
Source Link
Peter
  • 9.1k
  • 2
  • 23
  • 63

The use cases for life"real-life" examples are pretty clearly delineated here already, but since you asked for examples in programming as well, I'll add one to the list.

Stacks can be used to build calculators. There's a great assignment in CS50 AP called Calc 2.0. Students build a command-line calculator in C using a stack to store the numbers passed in by the user. Additionally, it's an opportunity to teach students about prefix notion as opposed to their normal use of infix notation.

The problem specification explains why this approach is worth pursuing:

Visually, this approach of "finding the rightmost operator and applying it to the two numbers to its right" is an intuitive way for humans to parse prefix notation, but computers can be a bit smarter about this, without ever having to look at each operand or operator more than once, if instead we store all the information in a stack as we see it.

If the computer parsed this input by starting at the right side (aka the last element of argv) and pushing numbers onto a stack as it came across them, then when it came upon an operation all that would need to happen is to pop the top two numbers off the stack, apply the operation, and push the result back on!

Here is the boilerplate they are given for building a stack:

typedef struct
{
   int size;
   float nums[MAXNUMS];
}
stack;

It's a simple struct, but since students can easily error-check their own work with simple math, this assignment provides a great way to actually start using a stack for something meaningful that also builds on students' prior knowledge.

The use cases for life are pretty clearly delineated here already, but since you asked for examples in programming as well, I'll add one to the list.

Stacks can be used to build calculators. There's a great assignment in CS50 AP called Calc 2.0. Students build a command-line calculator in C using a stack to store the numbers passed in by the user. Additionally, it's an opportunity to teach students about prefix notion as opposed to their normal use of infix notation.

The problem specification explains why this approach is worth pursuing:

Visually, this approach of "finding the rightmost operator and applying it to the two numbers to its right" is an intuitive way for humans to parse prefix notation, but computers can be a bit smarter about this, without ever having to look at each operand or operator more than once, if instead we store all the information in a stack as we see it.

If the computer parsed this input by starting at the right side (aka the last element of argv) and pushing numbers onto a stack as it came across them, then when it came upon an operation all that would need to happen is to pop the top two numbers off the stack, apply the operation, and push the result back on!

Here is the boilerplate they are given for building a stack:

typedef struct
{
   int size;
   float nums[MAXNUMS];
}
stack;

It's a simple struct, but since students can easily error-check their own work with simple math, this assignment provides a great way to actually start using a stack for something meaningful that also builds on students' prior knowledge.

The "real-life" examples are pretty clearly delineated here already, but since you asked for examples in programming as well, I'll add one to the list.

Stacks can be used to build calculators. There's a great assignment in CS50 AP called Calc 2.0. Students build a command-line calculator in C using a stack to store the numbers passed in by the user. Additionally, it's an opportunity to teach students about prefix notion as opposed to their normal use of infix notation.

The problem specification explains why this approach is worth pursuing:

Visually, this approach of "finding the rightmost operator and applying it to the two numbers to its right" is an intuitive way for humans to parse prefix notation, but computers can be a bit smarter about this, without ever having to look at each operand or operator more than once, if instead we store all the information in a stack as we see it.

If the computer parsed this input by starting at the right side (aka the last element of argv) and pushing numbers onto a stack as it came across them, then when it came upon an operation all that would need to happen is to pop the top two numbers off the stack, apply the operation, and push the result back on!

Here is the boilerplate they are given for building a stack:

typedef struct
{
   int size;
   float nums[MAXNUMS];
}
stack;

It's a simple struct, but since students can easily error-check their own work with simple math, this assignment provides a great way to actually start using a stack for something meaningful that also builds on students' prior knowledge.

Source Link
Peter
  • 9.1k
  • 2
  • 23
  • 63

The use cases for life are pretty clearly delineated here already, but since you asked for examples in programming as well, I'll add one to the list.

Stacks can be used to build calculators. There's a great assignment in CS50 AP called Calc 2.0. Students build a command-line calculator in C using a stack to store the numbers passed in by the user. Additionally, it's an opportunity to teach students about prefix notion as opposed to their normal use of infix notation.

The problem specification explains why this approach is worth pursuing:

Visually, this approach of "finding the rightmost operator and applying it to the two numbers to its right" is an intuitive way for humans to parse prefix notation, but computers can be a bit smarter about this, without ever having to look at each operand or operator more than once, if instead we store all the information in a stack as we see it.

If the computer parsed this input by starting at the right side (aka the last element of argv) and pushing numbers onto a stack as it came across them, then when it came upon an operation all that would need to happen is to pop the top two numbers off the stack, apply the operation, and push the result back on!

Here is the boilerplate they are given for building a stack:

typedef struct
{
   int size;
   float nums[MAXNUMS];
}
stack;

It's a simple struct, but since students can easily error-check their own work with simple math, this assignment provides a great way to actually start using a stack for something meaningful that also builds on students' prior knowledge.