Вы находитесь на странице: 1из 1

As mentioned on the powerpoint slide, stacks are an ordered collection of data items from which

only the most recently inserted can be deleted. And new items can only be inserted with respect
to that most recently inserted item. There is no limitations on the number of items in the stack
and no limitations on the nature of the items in the stack. Yet the implementations will place
limits on the stacks. Stacks follow a LIFO ordering – last in, first out. Standard stack operations
include push, pop and is_empty. Push is when we insert an item onto the top of the stack, and
pop is when we delete the item from the top of the stack and returns that item. Stack supports
recursion, and help facilitates program execution. We use stacks when we need a simple
structure and we don’t care about the processing order.

The required methods to implement a stack is Push(insert an item on the top of the stack),
Pop(delete item on the top of the stack and return the deleted item) and Is_Empty (check whether
we have an empty stack). We also need to have something that initializes the stack (constructor)
and in the data portion, we will need to have a pointer reference to the top of the stack, and some
space to store the actual data values.

Some optional methods can be copy, display, and peak function. Copy could be deep or shallow,
where for deep copy, we truly replicated all the contents of the stack, and shallow copy is when
we copy a reference and does not copy the entire structure. Peak function would return the top
item of a stack. It doesn’t change the stack at all. We can also replace it with a combination of a
pop followed by a push. We can also have a method to clear stack, it helps us throw away all the
contents of the stack and start over again. At the beginning, we should have a very generous and
comprehensive wish list of possible functions for the stack. And as the video lecture mentioned,
we can always choose not to implement something, but if we don’t consider it when we first
started the implementation, it will be difficult to add it in later.

I really like the example of the PEZ dispenser from the lectures – it’s a very simple yet effective
way to explain stack with LIFO characteristics – I especially enjoys the demo given in the video-
it’s a very good visualization of stacks. Some applications could be vending machines. When we
fill up the vending machines with snacks and drinks, we would put those items in the front of the
stack of items, and when we insert coins to purchase those items, the machine would
automatically pop the first item in the front of whichever item we picked. This simulation would
closely resemble the push and pop function, as we would only insert items to the front of the row
and the machine would only spit out the item from the front of the row. It will never go to the
end or the middle of the row and select an item from there. And similarly, in grocery stores, the
store worker would keep on adding new products onto the shelf, and when customers come and
purchase these items, they would just take the one on the outside – which is the last one added to
the shelf. No one will go dig into the back of the shelf and grab the last item in the back. This
would be another implementation of the LIFO structure.

Вам также может понравиться