Chapter 19: Program Design

Example stack1.c, Page 488. Other files- stack.h

In [1]:
import sys
STACK_SIZE=100
contents=[None]*STACK_SIZE
top=0
def terminate(message):
    print message
    sys.exit()
def make_empty():
    top=0
def is_empty():
    if(top==0):
        return True
    else:
        return False
def is_full():
    if(top==STACK_SIZE):
        return True
    else:
        return False
def push(i):
    if(is_full()):
        terminate("Error in push: stack is full.")
    contents[top+1]=i
def pop():
    if(is_empty()):
        terminate("Error in push: stack is empty.")
    return contents[top-1]
    

Example stack2.c, Page 489

In [2]:
import sys
STACK_SIZE=100
contents=[None]*STACK_SIZE
top=0
def terminate(message):
    print message
    sys.exit()
def make_empty():
    top=0
def is_empty():
    if(top==0):
        return True
    else:
        return False
def is_full():
    if(top==STACK_SIZE):
        return True
    else:
        return False
def push(i):
    if(is_full()):
        terminate("Error in push: stack is full.")
    contents[top+1]=i
def pop():
    if(is_empty()):
        terminate("Error in push: stack is empty.")
    return contents[top-1]
    

Example stackclient.c, Page 494

In [6]:
class Stack:
     def __init__(self):
         self.items = []
     def is_empty(self):
         return self.items == []
     def push(self, item):
         self.items.append(item)
     def pop(self):
         return self.items.pop()
     def size(self):
         return len(self.items)
     def make_empty(self):
         while len(self.items) > 0 : self.items.pop()
s1=Stack()
s2=Stack()
s1.push(1)
s1.push(2)
n=s1.pop()
print "Popped %d from s1"%n
s2.push(n)
n=s1.pop()
print "Popped %d from s1"%n
s2.push(n)
while(s2.is_empty()!=True):
    print "Popped %d from s2"%s2.pop()
s2.push(3)
s2.make_empty()
if(s2.is_empty()):
    print "s2 is empty"
else:
    print "s2 is not empty"
    
Popped 2 from s1
Popped 1 from s1
Popped 1 from s2
Popped 2 from s2
s2 is empty

Example stackADT.c, Page 495

In [7]:
import sys
STACK_SIZE=100
contents=[None]*STACK_SIZE
top=0
def terminate(message):
    print message
    sys.exit()
def make_empty():
    top=0
def is_empty():
    if(top==0):
        return True
    else:
        return False
def is_full():
    if(top==STACK_SIZE):
        return True
    else:
        return False
def push(i):
    if(is_full()):
        terminate("Error in push: stack is full.")
    contents[top+1]=i
def pop():
    if(is_empty()):
        terminate("Error in push: stack is empty.")
    return contents[top-1]

Example stackADT2.c, Page 498

In [8]:
#similar as above since Python doesn't have pointers
import sys
STACK_SIZE=100
contents=[None]*STACK_SIZE
top=0
def terminate(message):
    print message
    sys.exit()
def make_empty():
    top=0
def is_empty():
    if(top==0):
        return True
    else:
        return False
def is_full():
    if(top==STACK_SIZE):
        return True
    else:
        return False
def push(i):
    if(is_full()):
        terminate("Error in push: stack is full.")
    contents[top+1]=i
def pop():
    if(is_empty()):
        terminate("Error in push: stack is empty.")
    return contents[top-1]

Example stackADT3.c, Page 500

In [9]:
#similar as above since Python doesn't have pointers
import sys
STACK_SIZE=100
contents=[None]*STACK_SIZE
top=0
def terminate(message):
    print message
    sys.exit()
def make_empty():
    top=0
def is_empty():
    if(top==0):
        return True
    else:
        return False
def is_full():
    if(top==STACK_SIZE):
        return True
    else:
        return False
def push(i):
    if(is_full()):
        terminate("Error in push: stack is full.")
    contents[top+1]=i
def pop():
    if(is_empty()):
        terminate("Error in push: stack is empty.")
    return contents[top-1]
In [ ]: