报错
class SqList1:
def __init__(self):
self.initcapacity = 10
self.capacity = self.initcapacity
self.data = [None] * self.capacity
self.size = 0
def Add(self, e):
if self.size == self.capacity:
self.resize(2 * self.size)
self.data[self.size] = e
self.size += 1
def CreateList(self, a):
for i in range(len(a)):
if self.size == self.capacity:
self.resize(2 * self.size)
self.data[self.size] = a[i]
self.size += 1
print(f"您输入的数组为{self.data[0:self.size]}")
def getsize(self):
return self.size
def traverse(self): # 遍历顺序表
for item in self.data:
print(item, end=' ')
print('\n')
def resize(self, newcapacity):
assert newcapacity >= 0
olddata = self.data
self.data = [None] * newcapacity
self.capacity = newcapacity
for i in range(self.size):
self.data[i] = olddata[i]
from SqList1 import SqList1
def count_x(A, x):
if A.getsize() == 0:
return 0
else:
result = count_x(A[:-1], x)
if A[-1] == x:
result += 1
return result
if __name__ == '__main__':
A = SqList1()
a=[1,2,34,5,5,2,3,5,7,5]
A.CreateList(a)
A.traverse()
print(count_x(A, 5))