-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChain.py
More file actions
109 lines (94 loc) · 2.3 KB
/
Chain.py
File metadata and controls
109 lines (94 loc) · 2.3 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
# -*- coding: UTF-8
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class LinkedList(object):#链表
def __init__(self):
self.head = None
self.tail = None
def is_empty(self):
if self.head is None:
print('The linkedlist is empty')
else:
print('The linkedlist is not empty')
def append(self, data): #在末插入节点
node = Node(data)
if self.head is None:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
def iter(self): #遍历
if self.head is None:
print('The linkedlist is empty')
else:
cur = self.head
while cur is not None:
print(cur.data)
cur = cur.next
def insert(self, index, value): #在第n的位置插入节点
cur = self.head
cur_index = 0
# 空链表时
if cur is None:
print('List is empty')
return
while cur_index < index-1:
cur = cur.next
# 插入位置超出链表节点的长度时
if cur is None:
print('list length less than index')
return
cur_index += 1
node = Node(value)
node.next = cur.next
cur.next = node
# 插入位置是链表的最后一个节点时,需要移动tail
if node.next is None:
self.tail = node
def remove(self, index): #移除第n个节点
cur = self.head
cur_index = 0
if cur is None: # 空链表时
print('List is empty')
return
while cur_index < index-1:
cur = cur.next
if cur is None:
print('list length less than index')
cur_index += 1
if index == 0: # 当删除第一个节点时
self.head = cur.next
cur = cur.next
return
if self.head is self.tail: # 当只有一个节点的链表时
self.head = None
self.tail = None
return
cur.next = cur.next.next
if cur.next is None: # 当删除的节点是链表最后一个节点时
self.tail = cur
def search(self,item): #查找节点位置
cur = self.head
cur_index = 0
if cur is None: # 空链表时
print('List is empty')
return
while cur.data != item:
cur = cur.next
cur_index += 1
print("iterm index :" + str(cur_index))
if __name__ == '__main__':
linkedList = LinkedList()
linkedList.is_empty()
linkedList.append(1)
linkedList.append(2)
linkedList.is_empty()
linkedList.iter()
linkedList.insert(2,3)
linkedList.iter()
linkedList.remove(2)
linkedList.iter()
linkedList.search(2)