python: Linked List

 

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# encoding: utf-8
# 版权所有 2024 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# Datetime  : 2024/10/12 6:29
# User      : geovindu
# Product   : PyCharm
# File      : common/LinkedList.py
 
 
 
class Node:
    """
    Create a Node class to create a node
    """
    def __init__(self, data):
        """
         
        :param data:
        """
        self.data = data  # 资料
        self.next = None  # 下一条 指标
 
 
 
class LinkedList:
    """
    Create a LinkedList class
    """
    def __init__(self):
        """
 
        """
        self.head = None
 
    
    def insertAtBegin(self, data: object):
        """
        Method to add a node at begin of LinkedList
        最前端插入节点
        :param data:
        :return:
        """
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        else:
            new_node.next = self.head
            self.head = new_node
 
 
    def insertAtIndex(self, data: object, index:int):
        """
        Method to add a node at any index
        Indexing starts from 0.
        在指定索引位置插入节点
        :param data:
        :param index:
        :return:
        """
        if (index == 0):
            self.insertAtBegin(data)
 
        position = 0
        currentNode = self.head
        while (currentNode != None and position + 1 != index):
            position = position + 1
            currentNode = currentNode.next
 
        if currentNode != None:
            newNode = Node(data)
            newNode.next = currentNode.next
            currentNode.next = newNode
        else:
            print("Index not present")
 
  
    def insertAtEnd(self, data: object):
        """
        Method to add a node at the end of LinkedList
        在最后位置插入节点
        :param data:
        :return:
        """
         
        newNode = Node(data)
        if self.head is None:
            self.head = newNode
            return
 
        currentNode = self.head
        while (currentNode.next):
            currentNode = currentNode.next
 
        currentNode.next = newNode
 
   
    def updateNode(self, val:object, index:int):
        """
        Update node of a linked list
        at given position
        在指定索引位置添加节点
        :param val:
        :param index:
        :return:
        """
         
        currentNode = self.head
        position = 0
        if position == index:
            currentNode.data = val
        else:
            while (currentNode != None and position != index):
                position = position + 1
                currentNode = currentNode.next
 
            if currentNode != None:
                currentNode.data = val
            else:
                print("Index not present")
 
 
 
    def removeFirstNode(self):
        """
        Method to remove first node of linked list
        删除第一个节点
        :return:
        """
        if (self.head == None):
            return
 
        self.head = self.head.next
 
  
    def removeLastNode(self):
        """
        Method to remove last node of linked list
        删除最后一个节点
        :return:
        """
         
        if self.head is None:
            return
 
        currentNode = self.head
        while (currentNode != None and currentNode.next.next != None):
            currentNode = currentNode.next
 
        currentNode.next = None
 
 
    def removeAtIndex(self, index:int):
        """
        Method to remove at given index
        删除指定索引位置的节点
        :param index:
        :return:
        """
        if self.head == None:
            return
 
        currentNode = self.head
        position = 0
        if position == index:
            self.removeFirstNode()
        else:
            while (currentNode != None and position + 1 != index):
                position = position + 1
                currentNode = currentNode.next
 
            if currentNode != None:
                currentNode.next = currentNode.next.next
            else:
                print("Index not present")
 
 
    def removeNode(self, data: object):
        """
        Method to remove a node from linked list
        移除所有节点
        :param data:
        :return:
        """
        currentNode = self.head
 
        if currentNode.data == data:
            self.removeFirstNode()
            return
 
        while (currentNode != None and currentNode.next.data != data):
            currentNode = currentNode.next
 
        if currentNode == None:
            return
        else:
            currentNode.next = currentNode.next.next
 
 
    def sizeOfLenght(self):
        """
        Print the size of linked list
        :return:
        """
         
        size = 0
        if (self.head):
            currentNode = self.head
            while (currentNode):
                size = size + 1
                currentNode = currentNode.next
            return size
        else:
            return 0
 
 
    def printList(self):
        """
        print method for the linked list
        :return:
        """
        currentNode = self.head
        while (currentNode):
            print(currentNode.data)
            currentNode = currentNode.next
 
# test
''''''
# create a new linked list
llist = LinkedList()
 
# add nodes to the linked list
llist.insertAtEnd('GeovinDu')
llist.insertAtEnd('SibodDu')
llist.insertAtBegin('GinhonZhao')
llist.insertAtEnd('WeiTu')
llist.insertAtIndex('HongTu', 2)
 
# print the linked list
print("Node Data")
llist.printList()
 
# remove a nodes from the linked list
print("\nRemove First Node")
llist.removeFirstNode()
print("Remove Last Node")
llist.removeLastNode()
print("Remove Node at Index 1")
llist.removeAtIndex(1)
 
# print the linked list again
print("\nLinked list after removing a node:")
llist.printList()
 
print("\nUpdate node Value")
llist.updateNode('z', 0)
llist.printList()
 
print("\nSize of linked list :", end=" ")
print(llist.sizeOfLenght())

  

posted @   ®Geovin Du Dream Park™  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2023-10-12 typescript: Observer Pattern
2023-10-12 typescript: Mediator pattern
2023-10-12 typesciprt: Command Pattern
2022-10-12 CSharp: Simple Factory Pattern in donet core 3
2022-10-12 CSharp: Interpreter Pattern in donet core 3
2022-10-12 CSharp: Chain of Responsibility Pattern in donet core 3
2019-10-12 css: hide or dispaly div
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示