What is abstract data types?Abstract data types (ADTs) are ways to store and retrieve data without detailed information about how they are done. That is, for example, what people need is a software that does something, but they don't need to or want to know how everything is put together to work.
One type of ADTs is stack. A stack is a list of data, which in real life can be a stack of books, but the only way a stack can be modified is through the top one (eg. you can only add or remove from the top).
For a class stack, we can often see methods like "push", which adds item on top of a stack; "pop", which removes the top item from a stack; "is_empty", which checks whether the stack is empty.
Another type of ADTs is queue. While other properties are similar to a stack, queue, instead of removing starts from the top, starts removing from the bottom.
The only difference between codes of stack and queue is the following:
in (stack) method pop(self):
return self._data.pop()
in (queue) method dequeue(self):
return self._data.pop(0)
The "_" before "data" is used as a way of hiding information (which is already set up in the __init__ method) so that user don't see it. The clients need only to know what needs to be known, and hiding the data can protect local data being modified either by accident or with intention.
No comments:
Post a Comment