-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path单链表的操作
145 lines (120 loc) · 3.81 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
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
141
142
143
144
class Node(object):
def __init__(self, item):
self.item = item
self.next = None
class SingleLinkList(Node):
#之前按照书上的代码,这个地方没有继承上面的类(Node),导致出现了AttributeError: 'NoneType' object has no attribute 'next'的错误,尝试了两
#种方法第一种是上面👆继承Node的方法,此时代码可以执行下去。第二种方法,我采用将while cur.next is not None:中的next改为_next,将其变成普通类
#成员的方法,代码也可以继续执行。
def __init__(self,node = None):
self.__head = node
def is_empty(self):
"""链表是否为空
:return 如果链表为空 返回真
"""
return self.__head is None
def length(self):
"""链表长度"""
cur = self.__head
count = 0
while cur is not None:
count += 1
cur = cur.next
return count
def travel(self):
"""遍历整个链表"""
cur = self.__head
while cur is not None:
print(cur.item, end=" ")
cur = cur.next
print("")
def add(self, item):
"""链表头部添加元素
:param item: 要保存的具体数据
"""
node = Node(item)
node.next = self.__head
self.__head = node
def append(self, item):
"""链表尾部添加元素"""
node = Node(item)
# 如果链表为空,需要特殊处理
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur.next is not None:
cur = cur.next
# 退出循环的时候,cur指向的尾结点
cur.next = node
def insert(self, pos, item):
"""指定位置添加元素"""
# 在头部添加元素
if pos <= 0:
self.add(item)
# 在尾部添加元素
elif pos >= self.length():
self.append(item)
else:
cur = self.__head
count = 0
while count < (pos - 1):
count += 1
cur = cur.next
# 退出循环的时候,cur指向pos的前一个位置
node = Node(item)
node.next = cur.next
cur.next = node
def remove(self, item):
"""删除节点"""
cur = self.__head
pre = None
while cur is not None:
# 找到了要删除的元素
if cur.item == item:
# 在头部找到了元素
if cur == self.__head:
self.__head = cur.next
else:
pre.next = cur.next
return
# 不是要找的元素,移动游标
pre = cur
cur = cur.next
def search(self, item):
"""查找节点是否存在"""
cur = self.__head
while cur is not None:
if cur.item == item:
return True
cur = cur.next
return False
def reveres(self):
"""反转元素节点"""
if self.is_empty() or self.length() <= 1:
return
j = 0
while j < self.length() - 1:
cur = self.__head
for i in range(self.length() - 1):
cur = cur.next
if cur.next is None:
x = cur.item
self.remove(cur.item)
self.insert(j, x)
j += 1
if __ name __ == "__ main __":
sll = SingleLinkList()
# ll.SingleLinkList()
print(sll.is_empty())
print(sll.length())
sll.add(8)
sll.append(1)
sll.append(2)
sll.append(3)
sll.append(4)
sll.append(5)
sll.append(6)
print(sll.is_empty())
print(sll.length())
print(sll.Travel())