Write a one-way linked list and a doubly linked list in Python

Source: Internet
Author: User

A linked list is a data structure that is inefficient when looping through loops, but with great advantages when inserting and deleting. A linked list is made up of nodes. The nodes of a one-way list are divided into two parts: the stored object and the reference to the next node. Note refers to a node that is down. The doubly linked list differs from the one-way list in that it consists of three parts: the stored object, the reference to the next node, and the reference to the previous node, which can be traversed in both directions. The structure of a one-way list is the head node, tail is the tail node, and each node consists of the data storage object and next to the next node reference. The process of inserting and deleting one-way linked lists is explained below. Insert a new node: principle: Next of the previous node points to the current new node, and next to the new node points to the latter node where you want to insert the node. Note: It is important to consider the case where the insertion position is the head and tail nodes in the actual application. Delete a node: Principle: Locate the previous node of the node you want to delete, and next point directly to the next node in the delete location. Note: In practice, it is important to consider whether the deleted node is a head node or a tail node, taking into account the length of the linked list. Here is an example of a single-linked list written in Python.
Class Node:
Next = None
data = None
def __init__ (Self,nodedata):
Self.data = Nodedata

Class List:
Head = None
Size = 0
def __init__ (self):
Self.size = 0
Self.head = None

#遍历链表
def a (self):
Print ("AVX")
def printlist (self):
P=self.head
While (P was not None):
Print (P.data)
P=p.next
Print ("——————————————————————————————————————")

def insertlink (self, A, NewData):
NewNode = Node (NewData)
If self.size = = 0:
Print ("The link is None")
Self.head = NewNode
Self.size = self.size+1
Else
p = Self.head
While (P was not None) and (p.data! = a):
p = P.next
If P.next is None:
P.next = NewNode
Self.size = self.size + 1
Else
Newnode.next = P.next
P.next = NewNode
Self.size = self.size + 1

#删除链表中的节点
def deldata (self,a):
If self.size = = 0:
Print ("The link is None")
Elif self.size ==1:
Self.head = None
Self.size = self.size-1
Else
p = Self.head
While (P was not None) and (p.data! = a):
Q = P
p = P.next
If P is None:
Print ("Can ' t find a")
elif p = = Self.head:
Self.head = P.next
Elif P.data ==a and P.next are not None:
Q.next = Q.next.next
Self.size = self.size-1
Else
Q.next = None
Self.size = self.size-1


#修改链表中的指定节点
def updatelink (self,a,b):
p = Self.head
Print (P.data)
While (P was not None) and (p.data!=a):
p = P.next
If P is None:
Print ("Can ' t find a")
Else
P.data = b

If __name__== "__main__":
p = List ()
P.insertlink (a)
P.insertlink (ON)
P.insertlink (1,3)
P.insertlink (1,4)
Print ("_________________________---insertlink")
P.printlist ()
Print ("_________________________--chalink")
P.updatelink (2,5)
P.printlist ()
Print ("___________________________-----dellink")
P.deldata (5)
P.printlist ()
The structure of a doubly linked list is the head node, tail is the tail node, each node consists of a data storage object, next a reference to the next node, and a reference to the previous node from the pre. You can implement two-way traversal. Insert and delete the two-way list insert a new node: principle: Find the node location to be inserted, the next point of the new node points to the next node in the insertion position, the next node in the insertion position of the pre point to the new node. The left side of the insertion position node points to the new node, and the new node's pre points to the left node. Delete a node: Description: Locate the previous node of the node you want to delete, point to the next node of the previous node to remove the node and the pre of the next node to delete the node to point to the previous node on the node to go up the two-way list code: Class Node ():
    data = None
NextNode = None
Prenode = None
def __init__ (self, data):
Self.data = Data
Class Personchan ():
Size = 0
Head = None
Tail = None
def __init__ (self):
Self.head = None
Self.tail = None
Self.size = 0
#增加节点
def add_node (Self, a):
NewNode = Node (a)
if (Self.head = = None):
Self.head = NewNode
Self.head.prenode = None
Self.tail = NewNode
Self.tail.prenode = None
Self.tail.nextnode = None
Self.size = self.size+1
Else
temp = Self.head
While Temp.nextnode are not None: #返回尾节点tail
temp = Temp.nextnode
Temp.nextnode = NewNode
Self.tail = NewNode
Self.tail.prenode = Temp
Self.tail.nextnode = None
Self.size = self.size+1
#在查找到的a后面增加节点
def ins_node (self,a,b):
NewNode = Node (b)
If Self.head ==none:
Self.head = NewNode
Self.tail = NewNode
Print ("Insert success:", Newnode.data)
Self.size = Self.size +1
Else
temp = Self.head
while (temp was not None) & (Temp.data! = a):
temp = Temp.nextnode
if Temp.nextnode = = None:
Temp.nextnode = NewNode
Self.tail = NewNode
Self.tail.prenode = Temp
Self.tail.nextnode = None
temp = None
Print ("Insert success:", Newnode.data)
Self.size = self.size+1
Else
Newnode.prenode = Temp
Newnode.nextnode = Temp.nextnode
Temp.nextnode = NewNode
Print ("Insert success:", Newnode.data)
Self.size = self.size+1
#删除节点
def del_node (self,a):
if Self.head = = None:
Pass
elif Self.head.data = = A:
If Self.size ==1:
Self.head = None
Self.tail = None
Self.size = self.size-1
Else
Self.head = Self.head.nextnode
Self.size = self.size-1
Else
temp = Self.head.nextnode
while (temp was not None) and (temp.data! = a):
temp = Temp.nextnode
p = Temp.prenode
If temp! = None:
if Temp.nextnode = = None:

Self.tail = P
Self.tail.nextnod = None
Else
P.nextnode = Temp.nextnode
temp = None
Self.size = self.size-1
Print ("Delete is success:", a)
def listall (self): #正序排列
If self.size = = 0:
Print ("No data in the list")
Else
temp = Self.head
while (temp was not None):
Print (Temp.data)
temp = Temp.nextnode
def lista (self): #倒序排列
If self.size = = 0:
Print ("No data in the list")
temp = Self.tail
while (temp was not None):
Print (Temp.data)
temp = Temp.prenode

if __name__ = = ' __main__ ':
link = Personchan ()
Link.add_node (1)
Link.add_node (2)
Link.add_node (3)
Link.add_node (4)
Link.listall ()
Print ("The list ' s size is:", link.size)
Link.lista ()

Write a one-way linked list and a doubly linked list in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.