class IList:
def __init__(self, m):
self.A = [None] * m
self.capacity = m
self.size = 0
def get(self, i):
return self.A[i]
def set(self, i, e):
f = self.A[i]
self.A[i] = e
return f
def add(self, i, e):
for j in range(self.size, i, -1):
self.A[j] = self.A[j-1]
self.A[i] = e
self.size += 1
# Handle the case if size equals capacity
def remove(self, i):
for j in range(i, self.size-1):
self.A[j] = self.A[j+1]
self.size -= 1
class Node:
def __init__(self, dataval=None):
self.data = dataval
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def get(i):
return getr(i,self.head)
def getr(i,v):
if t is None:
raise Exception("index out of bounds")
if i is 0:
return v.data
else:
return getr(i-1,v.next)
def set(i):
return setr(i,e,self.head)
def setr(i,e,v):
if t is None:
raise Exception("index out of bounds")
if i is 0:
t = v.data
v.data = e
return t
else:
return setr(i-1,e,v.next)
def addr(i,e,v):
if v is None and i is 0: #end of list
t2 = Node(num)
return t2
if v is None and i != 0: #end of list, still trying to go further
raise Exception("index out of bounds")
if i is 0: #found the spot
t2 = Node(num)
t2.next = v
return t2
else: #recursive case
v.next = addr(i-1,e,v.next)
return v
def add(i,e):
self.head = insert(i,e,self.head)
def remove(i):
remover(i,self.head)
def remover(i,v):
if t is None:
raise Exception("index out of bounds")
if i is 0:
return v.next
else: #recursive case
v.next = remover(i-1,v.next)
return v
| push('a'); | 'a' |
| push('b'); | 'a','b' |
| push('c'); | 'a','b','c' |
| push('d'); | 'a','b','c','d' |
| pop(); | 'a','b','c' |
| pop(); | 'a','b' |
| push('e'); | 'a','b','e' |
| push('f'); | 'a','b','e','f' |
| pop(); | 'a','b','e' |
| pop(); | 'a','b' |
| pop(); | 'a' |
| pop(); |   |
def is_palindrome(c):
l = len(c)
s = []
for i in range(l // 2):
s.push(c[i])
k = -(-l // 2) # Ceiling division
for j in range(k,l):
if s.pop() != c[j]:
return False
return True
def RPNEval(s) #s is a stack
n = s.pop()
if (isinstance(n,(int,float))
return n
else //n is operator
right = RPNEval(s)
left = RPNEval(s)
return n(left,right)
//for simplicity, we will assume all operators are left associative.
Stack shuntingYard(inputExpression)
while (there still are items in inputExpression)
n ← remove next item from inputExpression
if (n is number)
output.push(n)
else if (n is operator)
while (precidence of n is ≤ shunt.top())
output.push(shunt.pop())
shunt.push(n)
else if (n is left paren)
shunt.push(n)
else //n is right paren
o ← shunt.pop()
while (o ≠ left paren)
output.push(o)
o ← stack.pop()
while (!shunt.isEmpty)
output.push(shunt.pop())
return output
| size | cost for each push |
| 1 | 1 2 (second push requires a copy, then a push) |
| 2 | 3 0 (amortize some cost earlier) |
| 2 | 3 0 3 (push requires 2 copies plus push) |
| 4 | 3 3 0 (amortize) |
| 4 | 3 3 0 1 (simple push) |
| 4 | 3 3 0 1 5 (copies + push) |
| 8 | 3 3 3 3 0 (amortize) |
| 8 | 3 3 3 3 0 1 1 1 9 |
| 16 | 3 3 3 3 3 3 3 3 0 (amortize) |
| 16 | 3 3 3 3 3 3 3 3 0 1 1 1 1 1 1 1 17 |
| 32 | 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 |
| 32 | 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 33 |
| 64 | 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 |
| enqueue('a'); | 'a' |
| enqueue('b'); | 'a','b' |
| enqueue('c'); | 'a','b','c' |
| enqueue('d'); | 'a','b','c','d' |
| dequeue(); | 'b','c','d' |
| dequeue(); | 'c','d' |
| enqueue('e'); | 'c','d','e' |
| enqueue('f'); | 'c','d','e','f' |
| dequeue(); | 'd','e','f' |
| dequeue(); | 'e','f' |
| dequeue(); | 'f' | >
| dequeue(); |   |
| a |
| a | b |
| a | b | c |
| b | c |
def enqueue(i):
A[r] = i
r = (r+1)%s
if ((r = f)
#expand array
`
dequeue(i)
if (f is r) raise Exception("Empty queue")
tmp = A[f]
f = (f+1)%s
return tmp