-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlist.py
More file actions
74 lines (60 loc) · 1.69 KB
/
list.py
File metadata and controls
74 lines (60 loc) · 1.69 KB
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
"""
Copyright © https://github.com/microwind All rights reserved.
@author: jarryli@gmail.com
@version: 1.0
@description: 列表数据结构 - Python实现
"""
class List:
def __init__(self):
self.capacity = 10
self.data = [None] * self.capacity
self.size = 0
def resize(self, new_capacity):
if new_capacity > self.capacity:
new_data = [None] * new_capacity
for i in range(self.size):
new_data[i] = self.data[i]
self.data = new_data
self.capacity = new_capacity
def add(self, value):
# Python 数组会自动扩容,实际无需resize,这里只是为了演示
if self.size == self.capacity:
self.resize(self.capacity * 2)
# self.data.append(value)
self.data[self.size] = value
self.size += 1
def remove(self):
if self.size > 0:
self.size -= 1
def get(self, index):
if index < 0 or index >= self.size:
raise IndexError("Index out of range")
return self.data[index]
def size(self):
return self.size
def capacity(self):
return self.capacity
def print_list(self):
print("List:", self.data[:self.size])
# 使用示例
lst = List()
lst.add(10)
lst.add(20)
lst.add(30)
lst.print_list() # List: [10, 20, 30]
print("Element at index 1:", lst.get(1)) # 20
lst.remove()
lst.print_list() # List: [10, 20]
print("Size:", lst.size) # 2
print("Capacity:", lst.capacity) # 10
lst.resize(20)
print("New Capacity:", lst.capacity) # 20
"""
jarry@MacBook-Pro list % python list.py
List: [10, 20, 30]
Element at index 1: 20
List: [10, 20]
Size: 2
Capacity: 10
New Capacity: 20
"""