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

时间:2022-01-17 07:27:47

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

结果展示:

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

代码示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/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中顺序表的实现简单代码分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:https://www.cnblogs.com/xautxuqiang/p/6098137.html