Python中顺序表的实现简单代码分享

顺序表python版的实现(部分功能未实现)

结果展示:

代码示例:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

class SeqList(object):
 def __init__(self, max=8):
  self.max = max  #创建默认为8
  self.num = 0
  self.date = [None] * self.max
  #list()会默认创建八个元素大小的列表,num=0,并有链接关系
  #用list实现list有些荒谬,全当练习
  #self.last = len(self.date)
  #当列表满时,扩建的方式省略
 def is_empty(self):
  return self.num is 0

 def is_full(self):
  return self.num is self.max

 #获取某个位置的元素
 def __getitem__(self, key):
  if not isinstance(key, int):
   raise TypeError
  if 0<= key < self.num:
   return self.date[key]
  else:
   #表为空或者索引超出范围都会引发索引错误
   raise IndexError

 #设置某个位置的元素
 def __setitem__(self, key, value):
  if not isinstance(key, int):
   raise TypeError
  #只能访问列表里已有的元素,self.num=0时,一个都不能访问,self.num=1时,只能访问0
  if 0<= key < self.num:
   self.date[key] = value #该位置无元素会发生错误
  else:
   raise IndexError

 def clear(self):
  self.__init__()

 def count(self):
  return self.num

 def __len__(self):
  return self.num

 #加入元素的方法 append()和insert()
 def append(self,value):
  if self.is_full():
   #等下扩建列表
   print("list is full")
   return
  else:
   self.date[self.num] = value
   self.num += 1

 def insert(self,key,value):
  if not isinstance(key, int):
   raise TypeError
  if key<0: #暂时不考虑负数索引
   raise IndexError
  #当key大于元素个数时,默认尾部插入
  if key>=self.num:
   self.append(value)
  else:
   #移动key后的元素
   for i in range(self.num, key, -1):
    self.date[i] = self.date[i-1]
   #赋值
   self.date[key] = value
   self.num += 1

 #删除元素的操作
 def pop(self,key=-1):
  if not isinstance(key, int):
   raise TypeError
  if self.num-1 < 0:
   raise IndexError("pop from empty list")
  elif key == -1:
   #原来的数还在,但列表不识别他
   self.num -= 1
  else:
   for i in range(key,self.num-1):
    self.date[i] = self.date[i+1]
   self.num -= 1

 def index(self,value,start=0):
  for i in range(start, self.num):
   if self.date[i] == value:
    return i
  #没找到
  raise ValueError("%d is not in the list" % value)

 #列表反转
 def reverse(self):
  i,j = 0, self.num - 1
  while i<j:
   self.date[i], self.date[j] = self.date[j], self.date[i]
   i,j = i+1, j-1

if __name__=="__main__":
 a = SeqList()
 print(a.date)
 #num == 0
 print(a.is_empty())
 a.append(0)
 a.append(1)
 a.append(2)
 print(a.date)
 print(a.num)
 print(a.max)
 a.insert(1,6)
 print(a.date)
 a[1] = 5
 print(a.date)
 print(a.count())

 print("返回值为2(第一次出现)的索引:", a.index(2, 1))
 print("====")
 t = 1
 if t:
  a.pop(1)
  print(a.date)
  print(a.num)
 else:
  a.pop()
  print(a.date)
  print(a.num)
 print("========")
 print(len(a))

 a.reverse()
 print(a.date)
 """
 print(a.is_full())
 a.clear()
 print(a.date)
 print(a.count())
 """

有关于Python数据结构中顺序表的实现,大家也可以参考本站的另一篇文章Python数据结构之顺序表的实现代码示例,当中有对顺序表略微详细的介绍。小编对此知识点理解不够透彻,以后还会继续研究的。

以上就是本文关于Python中顺序表的实现简单代码分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。