This week, we learnt two new abstract data types: stack and sack. A stack contains items of various sorts. New items are usually added on to the top of the stack, items may only be removed from the top of the stack. It’s a LIFO (last in first out) structure. However, sack is different from stack. New items are added on to a random place in the sack, so the order items are removed from the sack is completely unpredictable.
From my perspective, I find stack a little bit hard to understand. I then use a stack of books as an example of stack in python to help better understand the concepts. Because when we have a stack of books, we add the new book to the top of the old books but remove the most recent added books first. So it is a last in first out order. Also, the order of the books in the stack is important. Here, from this graph, we can know more about the structure of stack using "push" and "pop":
This concept is different from a queue concept which is first in first out as the people waiting the first in the queue should be removed first. This algorithm of stack can also be used in different places like matching brackets in sentences. From this class, I learnt that linking abstract concepts to real world applications is a good way to learn new concepts.
No comments:
Post a Comment