Python3单链表简单操作

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

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
class Node():
    # 定义头结点
    def __init__(self, data):
        self.data = data
        # 头指针为空
        self.next = None
 
 
# 头插法
class SNode():
    def __init__(self):
        self.current_node = None
 
    def add_node(self, data):
        node = Node(data)
        node.next = self.current_node
        self.current_node = node
 
    def append_node(self, data):
        # 尾插法插入节点
        node = Node(data)
        cur = self.current_node
        # 遍历链表直到头节点处停止遍历
        while cur:
            if cur.next == None:
                break
            cur = cur.next
        cur.next = node
 
    def travel(self):
        '''
        遍历链表
        :return:
        '''
        cur = self.current_node
        while cur:
            print(cur.data)
            cur = cur.next
 
    def is_empty(self):
        '''
        判断链表非空
        :return:
        '''
        return self.current_node == None
 
    def get_lenth(self):
        '''
        获取链表的长度
        :return:
        '''
        cur = self.current_node
        count = 0
        while cur:
            count += 1
            cur = cur.next
        return count
 
    def insert_node(self, index, data):
        '''
        指定位置插入节点
        :param index:
        :param data:
        :return:
        '''
        link_len = self.get_lenth()
        if index == 0:
            self.add_node(data)
        elif index >= link_len:
            self.append_node(data)
        else:
            cur = self.current_node
            for i in range(1, index):
                cur = cur.next
            node = Node(data)
            node.next = cur.next
            cur.next = node
 
    def del_node(self, index):
        '''
        根据索引删除节点
        :param index:
        :return:
        '''
 
        # 找到前节点
        cur = self.current_node
        # 前驱节点
        pre = None
        count = 1
        len_num = self.get_lenth()
        while cur:
            if index == 1:
                self.current_node = cur.next
                break
            if count == index and count < len_num:
                pre.next = cur.next
                break
            if count >= len_num:
                pre.next = None
                break
            count += 1
            pre = cur
            cur = cur.next
 
 
 
if __name__ == "__main__":
    test = SNode()
    list_data = [1, 2, 3]
    for i in list_data:
        test.add_node(i)
    test.travel()
    # print(test.is_empty())
    # print (test.get_lenth())
    # test.append_node(4)
    # test.insert_node(1, 4)
    # test.travel()
    # test.del_node(4)
    # test.travel()
 
    '''
    单链表的操作
is_empty() 链表是否为空
length() 链表长度
travel() 遍历整个链表
add(item) 链表头部添加元素
append(item) 链表尾部添加元素
insert(pos, item) 指定位置添加元素
remove(item) 删除节点
search(item) 查找节点是否存在
    '''

  

posted @   太阳i  阅读(2318)  评论(0编辑  收藏  举报
(评论功能已被禁用)
点击右上角即可分享
微信分享提示