Stacks Flashcards

(5 cards)

1
Q

What is a stack?

A

A stack is like a stack of plates. The last item put on, is the first one to be removed (last in first out - LIFO). A stack uses a single pointer to the top of the stack. You can add items (push) and remove items (pop) to/from stacks).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are uses of stacks?

A
  • Back/forward button on browsers
  • Undo operations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to add data to a stack?

A

stack.push() - points to the next free space, adds the item and increments the stacker pointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to remove data from a stack?

A

stack.pop() - decrements the stack pointer and returns the item at the top of the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How are stacks implemented in code?

A
  1. Using a procedural approach - Define an array and pointer variable, and write functions/procedures to pop/push items.
  2. Using built in functionality from libraries - Use the built in stack class provided by a library
  3. Using an object oriented approach - Define your own stack class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly