数据 结构之创建动态数组

抽象数据类型列表操作有:1.创建一个空列表 2.确定此列表是否为空 3.确定列表中的项目个数等。
指令有:
import ctypes
class DynamicArray
def init(self)
‘create an empty arraty’
self._n = 0 #size
self._capacity = 10
self._A = self._make_array(self._capacity)
#Len(list)
def len(self)
return self._n
def is_empty(self):
return self._n == 0

#O(1)
def getitem(self,k):
if not 0 <=k < self._n:
raise ValueError(‘invalid index’)
return self._A[k]
#O(1)
def append(self,obj):
if self._n ==self._capacity:
self._resize(2*self._capacity)
self._A[self._n]=obj
self._n + = 1
def _make_array(self,c):
return(c ctypes.py_object)( )
def _resize(self,c)
B = self._make_array©
for k in range(self._n):
B[k]=self._A[k]
self._A =B
self.capacity = c
#O(n)
def insert (self,k,value):
if self.
n =self._capacity:
self._resize(2
self._capacity)
for j in range(self._n,k,-1):
self._A[k] = value
self._n +=1
#o(n)
def remove(self,value):
for k in range(self._n):
if self.A[k] == value:
for j in range(k,self._n -1):
self.A[j] = self._A[j+1]
self._A[self._n -1] = None
self._n -= 1
return
raise ValueError(‘value not found’)

def _print(self):
     for i in range(self._n):
           print(self._A[i],end = ' ')
      print()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值