Professional Documents
Culture Documents
HCT 108 Handout 3
HCT 108 Handout 3
Data Structures
However, there are significant differences in the abstractions that these classes represent, and in the
way that instances of these classes are represented internally by Python. These classes will become
building blocks uponcwhich we will develop more complex data structures like stacks, queues, etc
In addition to the above array-based sequence types, Python has an array module which can be used
to instantiate array objects. This module defines an object type which can compactly represent an
array of basic values: characters, integers, floating point numbers. Arrays are sequence types and
behave very much like lists, except that the type of objects stored in them is constrained. The type is
specified at object creation time by using a type code, which is a single character. The following
type codes are defined:
Type code C Type Python Type Minimum size in bytes
'b' signed char int 1
'B' unsigned char int 1
'u' Py_UNICODE Unicode character 2
Type code C Type Python Type Minimum size in bytes
'h' signed short int 2
'H' unsigned short int 2
'i' signed int int 2
'I' unsigned int int 2
'l' signed long int 4
'L' unsigned long int 4
'q' signed long long int 8
'Q' unsigned long long int 8
'f' float float 4
'd' double float 8
Methods:
• append() -- append a new item to the end of the array
Attributes:
• typecode -- the typecode character used to create the array
Example:
from array import array
m = array('d',[10.4,11.4,8.90])
for x in m:
print (x)
l = array('l')
u = array('u', 'hello \u2641')
l = array('l', [1, 2, 3, 4, 5])
d = array('d', [1.0, 2.0, 3.14])
Stacks
A stack is a collection of objects that are inserted and removed according to the last-in, first-out
(LIFO) principle. A user may insert objects into a stack at any time, but may only access or remove
the most recently inserted object that remains (at the so-called “top” of the stack). The name “stack”
is derived from the metaphor of a stack of plates in a spring-loaded, cafeteria plate dispenser. In this
case, the fundamental operations involve the “pushing” and “popping” of plates on the stack.
When we need a new plate from the dispenser, we “pop” the top plate off the stack,
and when we add a plate, we “push” it down on the stack to become the new top
plate.
Besides the precondition assertions mentioned in the explanation of the stack ADT operations
above, we can also state some axioms to help us understand the stack ADT. For example, consider
the following axioms.
For any stack s and element e, pop(push(s,e)) = s.
For any stack s and element e, top(push(s,e)) = e.
For any stack s, and element e, empty?(push(s,e)) = false.
The first axiom tells us that the pop() operation undoes what the push() operation achieves.
The second tells us that when an element is pushed on a stack, it becomes the top element
on the stack.
The third tells us that pushing an element on an empty stack can never make it empty.
By convention, we assume that a newly created stack is empty, and that there is no a priori bound
on the capacity of the stack. Elements added to the stack can have arbitrary type.
Example:
Alternative:
Apart from implementing from first principles, we can also use the built in Stack class from the
pythonds module.
from pythonds.basic.stack import Stack
s=Stack()
print(s.isEmpty())
s.push(4)
s.push('dog')
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())