Python single-chain table implementation

Source: Internet
Author: User
# Coding: utf-8classNode: def _ init _ (self, value): self. datavalueself. nextNoneclassLinkList: def _ init _ (self, data [0]): self. headNoneself. ini... # coding: UTF-8


Class Node:
Def _ init _ (self, value ):
Self. data = value
Self. next = None


Class LinkList:
Def _ init _ (self, data = [0]):
Self. head = None
Self. init_link_list (data)

# Initializing a linked list
# Data is an array
Def init_link_list (self, data ):
If len (data) = 0:
Print ("Initialization data is null ")
Return
Self. head = Node (data [0])
Current = self. head
For index in data [1:]:
Current. next = Node (index)
Current = current. next

# Getting the current node
Def get_node (self, index ):
If self. is_empty ():
Print ("link is empty ")
Return
If index> self. get_length () or index <= 0:
Print ("node is not exist ")
Return
Current = self. head
I = 0
While I <index:
If I = index-1:
Return current
Current = current. next
I + = 1

# Getting the value of the current element
Def get_data (self, index ):
Current = self. get_node (index)
If current is None:
Return "node is not exist"
Return current. data

# Print a linked list
Def print_link (self ):
If self. is_empty ():
Return
List = []
Current = self. head
While current. next is not None:
List. append (current. data)
Current = current. next
Else:
List. append (current. data)
Print (list)

# Getting the length of a linked list
Def get_length (self ):
If self. is_empty ():
Return 0
Current = self. head
Count = 0
While current. next is not None:
Count + = 1
Current = current. next
Else:
Count + = 1
Return count

# Determine whether the linked list is empty
# If it is null, true is returned.
# If it is not null, false is returned.
Def is_empty (self ):
Return self. head is None

# Insert an element after the current element
# Index element index
# Data insert value
Def add_after (self, index, data ):
Current = self. get_node (index)
If current is None:
Return "node is not exist"
Current_next = current. next
Current. next = Node (data)
Current = current. next
Current. next = current_next

# Insert an element before the current element
Def add_before (self, index, data ):
If index = 1:
Current = self. get_node (index)
Self. head = Node (data)
Self. head. next = current
Return
Pre = self. get_pre_node (index)
Current = pre. next
Pre. next = Node (data)
Pre = pre. next
Pre. next = current

# Obtain the first element of the current element
Def get_pre_node (self, index ):
If self. is_empty ():
Print ("link is empty ")
Return
If index> self. get_length () or index <= 1:
Print ("node is not exist ")
Return
Pre = self. head
I = 0
While I <index:
If I = index-2:
Return pre
Pre = pre. next
I + = 1

# Delete a specified element and return the value of the deleted element
Def remove (self, index ):
If index = 1 and not self. is_empty ():
Data = self. head. data
Self. head = self. head. next
Return data
Pre_node = self. get_pre_node (index)
Current = self. get_node (index)
If pre_node is None or current is None:
Print ("data is not exist ")
Pre_node.next = current. next
Return current. data

# Modify the value of the current node
Def update (self, index, data ):
Current = self. get_node (index)
If current is None:
Return "current node is none"
Current. data = data

# Merge the new linked list to the current linked list
Def merge (self, data ):
Size = self. get_length ()
Last_node = self. get_node (size)
Last_node.next = data. head
Return self

# Test
Y = (1, 2, 3, 4)
S = ["a", "B", "c", "d"]
LinkList = LinkList (y)
# LinkList. init_link_list (["a", "B", "c", "d"])
# Second = LinkList ()
# Second. init_link_list (["x", "y", "z"])
# LinkList. add_after (-1, "x ")
# LinkList. add_after (1, "y ")
# LinkList. init_link_list ([])
# LinkList. add_before (1, "x ")
# LinkList. print_link ()
# Print ("item:", linkList. get_data (2 ))
# Print ("length:", linkList. get_length ())
# Print ("is empty", linkList. is_empty ())
# Print (linkList. get_pre_node (3). data)
# Print ("remove node:", linkList. remove (2 ))
# LinkList. merge (second)


LinkList. print_link ()

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.