单链表

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。– 百度百科

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
class Node(object):
"""
单链表的节点定义
"""
def __init__(self, item):
self.item = item # 表示数据元素
self.next = None # 表示指针


class SingleLinkList(object):
"""
头结点记录链表的长度
"""
def __init__(self, li, method="tail"):
if method == "head":
self.create_link_head(li)
elif method == "tail":
self.create_link_tail(li)

def create_link_head(self, li):
"""
从头插入数据,所以输出的顺序是逆序的。
头插法的思路:从头节点开始让每一个元素的next指针,指向当前的节点
插入的过程如下:
A:[data][next]
\

head:[data][next] -> A:[data][next]

:param li:
:return:
"""
self.head = Node(0)
for item in li:
n = Node(item)
n.next = self.head.next
self.head.next = n
self.head.item += 1

def create_link_tail(self, li):
"""
从尾部插入数据,所以输出的顺序是正序的。
尾插法的思路:定义一个尾节点(这个尾结点等于头结点),让尾节点的指针指向每一个元素,然后更新这个尾节点为当前的节点
current -> current
tail:[data][next] -> A:[data][next]
:param li:
:return:
"""
self.head = Node(0)
self.tail = self.head
for item in li:
p = Node(item)
self.tail.next = p
self.tail = p
self.head.item += 1

def __len__(self):
return self.head.item


if __name__ == '__main__':
li = [1, 2, 3, 4, 5]
s = SingleLinkList(li)

单链表的基本操作

单链表的遍历

通过指针一直往后找:

1
2
3
4
5
p = self.head.next
while p:
data = p.item
print(data)
p = p.next
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
class Node(object):
"""
单链表的节点定义
"""
def __init__(self, item):
self.item = item # 表示数据元素
self.next = None # 表示指针


class SingleLinkList(object):
"""
头结点记录链表的长度
"""
def __init__(self, li, method="tail"):
if method == "head":
self.create_link_head(li)
elif method == "tail":
self.create_link_tail(li)

def create_link_head(self, li):
"""
从头插入数据,所以输出的顺序是逆序的。
头插法的思路:从头节点开始让每一个元素的next指针,指向当前的节点
插入的过程如下:
A:[data][next]
\

head:[data][next] -> A:[data][next]

:param li:
:return:
"""
self.head = Node(0)
for item in li:
n = Node(item)
n.next = self.head.next
self.head.next = n
self.head.item += 1

def create_link_tail(self, li):
"""
从尾部插入数据,所以输出的顺序是正序的。
尾插法的思路:定义一个尾节点(这个尾结点等于头结点),让尾节点的指针指向每一个元素,然后更新这个尾节点为当前的节点
current -> current
tail:[data][next] -> A:[data][next]
:param li:
:return:
"""
self.head = Node(0)
self.tail = self.head
for item in li:
p = Node(item)
self.tail.next = p
self.tail = p
self.head.item += 1

def travel_linklist(self):
"""
遍历单链表
:return:
"""
p = self.head.next
while p:
data = p.item
print(data)
p = p.next

def __len__(self):
return self.head.item


if __name__ == '__main__':
li = [1, 2, 3, 4, 5]
s = SingleLinkList(li)
s.travel_linklist()

单链表的插入

待插入节点的data连接待插入节点next,当前节点next指向插入节点的next

1
2
3
4
5
6
7
8
	insert_node[data][next]
/ \
current_node[data][next] current_node_next[data][next]

代码:
insert_node.next = current_node_next
current_node.next = insert_node

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
class Node(object):
"""
单链表的节点定义
"""
def __init__(self, item):
self.item = item # 表示数据元素
self.next = None # 表示指针


class SingleLinkList(object):
"""
头结点记录链表的长度
"""
def __init__(self, li, method="tail"):
if method == "head":
self.create_link_head(li)
elif method == "tail":
self.create_link_tail(li)

def create_link_head(self, li):
"""
从头插入数据,所以输出的顺序是逆序的。
头插法的思路:从头节点开始让每一个元素的next指针,指向当前的节点
插入的过程如下:
A:[data][next]
\

head:[data][next] -> A:[data][next]

:param li:
:return:
"""
self.head = Node(0)
for item in li:
n = Node(item)
n.next = self.head.next
self.head.next = n
self.head.item += 1

def create_link_tail(self, li):
"""
从尾部插入数据,所以输出的顺序是正序的。
尾插法的思路:定义一个尾节点(这个尾结点等于头结点),让尾节点的指针指向每一个元素,然后更新这个尾节点为当前的节点
current -> current
tail:[data][next] -> A:[data][next]
:param li:
:return:
"""
self.head = Node(0)
self.tail = self.head
for item in li:
p = Node(item)
self.tail.next = p
self.tail = p
self.head.item += 1

def travel_linklist(self):
"""
遍历单链表
:return:
"""
p = self.head.next
while p:
data = p.item
print(data)
p = p.next

def insert(self, curr_node, insert_node):
"""
单链表的插入
:param curr_node: 要插入的当前节点
:param insert_node: 插入的节点
:return:
"""
insert_node.next = curr_node.next
curr_node.next = insert_node

def __len__(self):
return self.head.item


if __name__ == '__main__':
li = [1, 2, 3, 4, 5]
s = SingleLinkList(li)

单链表的删除

1
2
3
4
5
6
					   		x 删除
删除前:current_node[data][next] -> node1[data][next] -> node[data][next]
删除后:current_node[data][next] -> node[data][next]
代码:
rm_node = curr_node.next
curr_node.next = curr_node.next.next