链表

  1. 链表

链表

  • 定义
    • 链表是一种常见的基础数据结构,是一种线性表,但是不像顺序表一样连续存储,而是在每一个节点里面存放下一个节点的位置信息
  • 单链表(当前节点只存储下一个节点的信息)
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
class Node():
"""节点类"""

def __init__(self, item, next=None):
self.item = item
self.next = next


class LinkList():
"""链表"""

def __init__(self, node=None):
self.head = node

def is_empty(self):
"""判断链表是否为空"""
return True if self.head else False

def length(self):
"""返回链表长度"""
num = 0
node = self.head
if node == None:
return num
num += 1
while node.next:
node = node.next
num += 1
return num

def add(self, item):
"""链表头部添加"""
self.head = Node(item, self.head)

def travel(self):
"""遍历整个链表"""
node = self.head
if node == None:
return
print(node.item)
while node.next:
node = node.next
print(node.item)

def addend(self, item):
"""链表尾部添加元素"""
node = self.head
if node == None:
self.head = Node(item)
return
while node.next:
node = node.next
node.next = Node(item)

def insert(self, index, item):
"""指定位置添加"""
if index == 0:
self.add(item)
return
node = self.head
try:
for i in range(index):
node = node.next
node.next = None(item, node.next)
except:
assert "索引超出范围"

def remove(self, index):
"""删除指定节点"""
if index == 0:
self.head = self.head.next
return
node = self.head
try:
for i in range(index-1):
node = node.next
node.next = node.next.next
except:
assert "索引超出范围"

def search(self, item):
"""查找节点是否存在"""
index = 0
node = self.head
while node.next:
if node.item==item:
return index
index += 1
node = node.next
return "该元素不存在"
  • 双向链表
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
class Node():
"""节点类"""
def __init__(self, item, per=None, next=None):
self.per = per
self.item = item
self.next = next


class LinkList():
"""双向链表"""

def __init__(self, node=None):
self.head = node

def is_empty(self):
"""判断链表是否为空"""
return True if self.head else False

def length(self):
"""返回链表长度"""
num = 0
node = self.head
if node == None:
return num
num += 1
while node.next:
node = node.next
num += 1
return num

def add(self, item):
"""链表头部添加"""
self.head = Node(item, next=self.head)

def travel(self):
"""遍历整个链表"""
node = self.head
if node == None:
return
print(node.item)
while node.next:
node = node.next
print(node.item)

def addend(self, item):
"""链表尾部添加元素"""
node = self.head
if node == None:
self.head = Node(item)
return
while node.next:
node = node.next
node.next = Node(item, node)

def insert(self, index, item):
"""指定位置添加"""
if index == 0:
self.add(item)
return
node = self.head
try:
for i in range(index):
node = node.next
node.next = None(item, node, node.next)
except:
assert "索引超出范围"

def remove(self, index):
"""删除指定节点"""
if index == 0:
self.head = self.head.next
return
node = self.head
try:
for i in range(index-1):
node = node.next
node.next = node.next.next
except:
assert "索引超出范围"

def search(self, item):
"""查找节点是否存在"""
index = 0
node = self.head
while node.next:
if node.item==item:
return index
index += 1
node = node.next
return "该元素不存在"
  • 循环链表
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
class Node():
"""节点类"""
def __init__(self, item, per=None, next=None):
self.per = per
self.item = item
self.next = next


class LinkList():
"""循环链表"""

def __init__(self, node=None):
self.head = node

def is_empty(self):
"""判断链表是否为空"""
return True if self.head else False

def length(self):
"""返回链表长度"""
num = 0
node = self.head
if node == None:
return num
num += 1
while node.next!=self.head:
node = node.next
num += 1
return num

def add(self, item):
"""链表头部添加"""
self.head = Node(item, per=self.head.per, next=self.head)

def travel(self):
"""遍历整个链表"""
node = self.head
if node == None:
return
print(node.item)
while node.next!=self.head:
node = node.next
print(node.item)

def addend(self, item):
"""链表尾部添加元素"""
node = self.head
if node == None:
node = Node(item)
node.per=node
node.next=node
self.head = node
return
while node.next:
node = node.next
node.next = Node(item, node, node.next)

def insert(self, index, item):
"""指定位置添加"""
if index == 0:
self.add(item)
return
node = self.head
try:
for i in range(index):
node = node.next
node.next = None(item, node, node.next)
except:
assert "索引超出范围"

def remove(self, index):
"""删除指定节点"""
if index == 0:
self.head = self.head.next
return
node = self.head
try:
for i in range(index-1):
node = node.next
node.next = node.next.next
node.next.per=node
except:
assert "索引超出范围"

def search(self, item):
"""查找节点是否存在"""
index = 0
node = self.head
while node.next:
if node.item==item:
return index
index += 1
node = node.next
return "该元素不存在"

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 zoubinbf@163.com

×

喜欢就点赞,疼爱就打赏