Abstract Data Type
Abstract Data Type
PVR Murthy
Contents:
i.Abstract Data Type for STACK
ii.About implementing STACK using Python List
iii. Function call and Return Mechanism using Stack
#ABSTRACTION:
# The notion of abstraction is to distill a complicated system down to its
#most fundamental parts.
#Describing the parts of a system involves naming them and explaining their
#functionality.
#An ADT specifies what each operation does , but not how it does it.
#We will refer to the collective set of behaviours supported by an ADT as its
#public interface.
The above diagram shows the result of the interpretation of the statement:
myStack = ArrayStack() .
An orbject instance of class Stack is created by the statement:
myStack = ArrayStack()
Python interpreter allocates space for an object instance of class ArrayStack
and initializes member names or identifiers or attributes of class ArrayStack.
(Figure 3)
Explicit parameter: self
Note that in the definition of class ArrayStack: , push(self,e) is the signature of
the method push; what this means is that self (current object instance) is
explicitly passed as a parameter. Note that self represents a reference to
current instance(object instantiated from the class).
Implicit parameter in call to push: myStack.push(“Ram”)
Note that in the call to push(self,e) method , only e is passed explicitly as in
myStack.push(“Ram”). A reference to the string “Ram” is passed as an
argument. Actual argument corresponding to formal parameter(argument) self
is not passed explicitly in the call. However, it may be noted that the call to
push is qualified by reference to the object ( myStack). Thus, push method is
executed on the instance(object) referred to by myStack.
Different Object instances and states of objects:
Multiple object instances may be created in a program:
myStack1 = ArrayStack()
myStack2 = ArrayStack()
The object referred to by reference myStack1 and the object referred to by
reference myStack2 have their own instance variables (_data in this case) and
their own respective states based on the calls through push(self,e) and pop().
(Question1: trace through the interpretation of the call to myStack.top() ;
definition of top() method can be found below in class ArrayQueue: use the
above diagram as a reference ; draw the diagram following the call to
myStack.top().
Question 2: Now trace through the call to myStack.pop() and draw the
diagram of the object).
The above discussion and questions indicate to us that an instance (object) of
class ArrayStack: is created to start with the interpretation of the statement
myStack = ArrayStack() which initializes myStack as hown in Figure 1 using the
method myStack.__init__(). The initial state of the object referred to by
myStack is shown in Figure 1. Subsequently, Figure 2 shows the state of the
object referred to by myStack, following the call to myStack.push(“Ram”).
Figure 3 shows the state of the object following the call to
myStack.push(“Lakshman”). Thus an instance (object) usually undergoes state
changes with each method call made. The object exists until the statement
myStack = None is interpreted by Python interpreter and subsequently,
Garbage Collector reclaims the memory occupied by the object to which
myStack no longer refers to (or points to).
“Ram”
TopOfStack
“Lakshman”
“Ram”
Question:
Indicate a sequence of operations on the above stack which transform the
stack with two elements present in it to an empty stack. How many such
sequences are possible?
NOTE:
What is just shown above is the way a stack is usually drawn and an accepted
way of showing stacks or illustrating stacks. However, in the notes, and the
text book, a stack may be shown in the form [“Ram”, “Lakshman”] but the
above is the preferred way.
class Empty(Exception):
pass
class ArrayStack:
# LIFO Stack implementation using Python List namely _data
def __init__(self):
#__init__ method serves as the constructor of the class(see page 71 of text
# book)
def __len__self(self):
#return number of elements in stack
return len(self._data)
def is_empty(self):
#boolean function that returns True, if stack is empty, else, False
return len(self._data)==0
def push(self,e):
#Push element e onto the top of the stack
self._data.append(e)
def top(self):
#return top element without removing it
# note the special feature of Python Lists, -1 index being used for
# last element; also note the condition on which an exception is raised
if self.is_empty():
raise Empty('Stack is empty')
return self._data[-1]
def pop(self):
#retrun top element , removing it from stack
# note that in both top and pop() methods, Python list related
# built-in functions are used (lists support pop() function, refer
# to python.org and raed documentation of list’s pop() function
if self.is_empty():
raise Empty('Stack is empty')
return self._data.pop()
Function g(y):
…
…
return(some other expression)
[Rm] push(Rm)
[Rm , Rf ] push(Rf)
[Rm] pop()
[] pop()