Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize one-way linked list by python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to achieve one-way linked list in python". In daily operation, I believe many people have doubts about how to achieve one-way linked list in python. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to achieve one-way linked list in python". Next, please follow the editor to study!

What is a linked list

As its name implies, a linked list is a chain.

Linked list is a dynamic data structure, which is characterized by the use of a set of arbitrary storage units to store data elements. Each element in the linked list becomes a "node", and each node consists of a data field and a pointer field. Unlike arrays, linked lists do not have to be pre-defined and can be expanded indefinitely if supported by hardware.

The differences between linked lists and arrays:

The array needs a predefined size and cannot adapt to the dynamic increase or decrease of data. If the data is less than the defined length, memory will be wasted. If the data exceeds a predefined length, it cannot be inserted. Linked lists are dynamically added and deleted data that can be added at will.

The array is suitable for the operation of getting elements, and you can get the index directly. It is troublesome to get elements from a linked list, and you need to keep looking for them. But they are suitable for adding and deleting, so you can directly modify the direction of the node. But arrays are troublesome. For example, [1Jing 2jue 3jue 4] needs to insert-2 at subscript 1, so you need to move [2Jet 3P4] back and assign the value LS [1] =

The array allocates space from the stack, which is convenient and fast for programmers, but with little freedom. The linked list allocates space from the heap, which has a large degree of freedom, but the application management is troublesome.

Basic method implementation of linked list (Python) 1. Initialize linked list

'' Node Class''

Category node (object):

Def__init__ (self,data):

Self.data= data

Self.nex= none

Def__init__ (self):

'' initialize the link list''

Self. Head=None

two。 Get the list length

Def _ _ len _ (self):

Pre=self.head

Length = 0

While prep:

Length = 1

Pre=pre.nex

Return length

3. Additional node

Adding nodes is relatively easy. If the header node does not exist, the current node is the header node, otherwise, look for the tail node and point the next one of the tail node to the current node (you can add head and tail nodes without having to find the tail node recursively)

'' append node''

Defappend (itself, data):

''

1. The header is none: header node

2.tail.nex-node

: paramdata:

: return:

''

Node = node (data)

Ifself.headisNone:

Self.head=node

Else:

Pre=self.head

Whilepre.nex:

Pre=pre.nex

Pre.nex=node

4. Get Node

It is also relatively easy to obtain nodes, which is nothing more than judging the positive or negative of the index value.

Defget (itself, index):

''

: paramindex:

: return:

''

Index=indexifindex=0 elselen (self) index

Iflen (self) indexorindex0:

Return none

Pre=self.head

Whileindex:

Pre=pre.nex

Index-=1

Returnpre

5. Set up nod

Find the current node and assign it

'' set Node''

Defset (itself, index, data):

Node=self.get (index)

Ifnode:

Node.data= data

Returnnode

6. Insert Node

To insert a node, you need to find the previous node pre_node (the index is positive or negative, the previous node is different, you need to determine), and then point the pre_node.nex to the current node. Also, point the nex of the current node to pre_node.nex.nex

'' insert node''

Define inserts (self, index, data):

''

1.1. Index insertion node positions include positive and negative numbers

two。 Lookup index

-1Mel-> pre_node Node 3.predominantly node.nextMuray-> node node.next-- > pre_node.next.next 4.head: paramindex:: paramdata:: return: "" node=Node (data) ifabs (index+1) > len (self): returnFalse index=indexifindex > = 0elselen (self) + index+1 ifindex==0: node.nex=self.head self.head=node else: pre=self.get (index-1) ifpre: nex=pre.nex pre.nex=node node.nex=nex else: returnFalse returnnode

7. Delete nod

To delete a node, you should also distinguish between the positive and negative of the index. Find the previous node pre_node and the latter node next_node of the current node, and change pre_node.nex- > next_node.

Defdelete (self,index): f=indexifindex > 0elseabs (index+1) iflen (self) = 0elselen (self) + indexprep=Nonewhileindex:prep=prepre=pre.nexindex-=1ifnotprep:self.head=pre.nexelse:prep.nex=pre.nexreturnpre.data8. Reverse linked list

There are many ways to reverse a linked list, and it is relatively simple to generate a new linked list-- "you can use an array to store all nodes in reverse order to generate a new linked list."

It is produced here in the following way:

To reverse the linked list is to recursively implement node.nex- > pre_node, and then assign tail to head.

"" reverse the linked list "" def__reversed__ (self): "" 1.Premura-> next to next-- > pre2.pre if head, then pre.nex-- > None3.tail-- > self.head:return: "" defreverse (pre_node,node): ifpre_nodeisself.head:pre_node.nex=Noneifnode:next_node=node.nexnode.nex=pre_nodereturnreverse (node,next_node) else:self.head=pre_nodereturnreverse (self.head,self.head.nex) 9. Clear the linked list

Just give the head a blank.

Defclear (self): self.head=None at this point, the study on "how to implement an one-way linked list in python" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 228

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report