Chapter 17: Advanced Pointers

Example 17.1, Page number: 331

In [ ]:
# Structure declaration
from ctypes import *

class person (Structure) :
_fields_ = [("name", c_wchar_p), ("address", c_wchar_p), ("city_state_zip", c_wchar_p), ("age", c_int), ("height", c_float)]

Example 17.2, Page number: 336

In [2]:
# Function declaration
def lookup (name) :
   lists = ['John', 'Jim', 'Jane', 'Clyde']
   result = 0

   for index in range (0, 4) :
      if (lists[index] == name) :
         result = 1
         break
   
   return result

# Calculation and result
name = 'Jane'

if (lookup (name)) :
   print ('%s is in the list\n' % name)
else :
   print ('%s is not in the list\n' % name)
Jane is in the list

Example 17.3, Page number: 338

In [3]:
# Declaration and result
aList = [45, 89, 123]

aList.insert(1, 53)

print 'Final List : ', aList
Final List :  [45, 53, 89, 123]

Example 17.4, Page number: 348

In [4]:
# Class declaration
class Node:
   def __init__(self, val):
      self.l_child = None
      self.r_child = None
      self.data = val

def binary_insert(root, node):
   if root is None:
      root = node
   else:
      if root.data > node.data:
         if root.l_child == None:
            root.l_child = node
         else:
            binary_insert(root.l_child, node)
      else:
         if root.r_child == None:
            root.r_child = node
         else:
            binary_insert(root.r_child, node)

def in_order_print(root):
   if not root:
      return
   in_order_print(root.l_child)
   print root.data
   in_order_print(root.r_child)

r = Node('Lemon')
binary_insert(r, Node('Plum'))
binary_insert(r, Node('Apple'))
binary_insert(r, Node('Orange'))
binary_insert(r, Node('Pear'))
binary_insert(r, Node('Grape'))


# Result
print "List of words in ASCII order:"
in_order_print(r)
List of words in ASCII order:
Apple
Grape
Lemon
Orange
Pear
Plum