{"id":1065,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u9ad8\u7ea7\u94fe\u63a5\u5217\u8868","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1065.html","title":{"rendered":"Python \u9ad8\u7ea7\u94fe\u63a5\u5217\u8868"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python \u9ad8\u7ea7\u94fe\u63a5\u5217\u8868<\/h1>\n

Python \u9ad8\u7ea7\u94fe\u63a5\u5217\u8868\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u6211\u4eec\u5df2\u7ecf\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\u770b\u5230\u4e86\u94fe\u8868\uff0c\u5176\u4e2d\u53ea\u80fd\u5411\u524d\u79fb\u52a8\u3002\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u5230\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u94fe\u8868\uff0c\u5176\u4e2d\u53ef\u4ee5\u5411\u524d\u548c\u5411\u540e\u79fb\u52a8\u3002\u8fd9\u6837\u7684\u94fe\u63a5\u5217\u8868\u79f0\u4e3a\u53cc\u91cd\u94fe\u63a5\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u53cc\u5411\u94fe\u8868\u7684\u529f\u80fd\u3002\n <\/div>\n

\u53cc\u94fe\u8868\u5305\u542b\u4e00\u4e2a\u540d\u4e3afirst\u548clast\u7684\u94fe\u63a5\u5143\u7d20\u3002<\/span>
\n \u6bcf\u4e2a\u94fe\u63a5\u5305\u542b\u4e00\u4e2a\u6570\u636e\u5b57\u6bb5\u548c\u4e24\u4e2a\u94fe\u63a5\u5b57\u6bb5\uff0c\u5206\u522b\u79f0\u4e3anext\u548cprev\u3002<\/span>
\n \u6bcf\u4e2a\u94fe\u63a5\u90fd\u4f7f\u7528\u5176\u4e0b\u4e00\u4e2a\u94fe\u63a5\u4e0e\u5176\u4e0b\u4e00\u4e2a\u94fe\u63a5\u94fe\u63a5\u3002<\/span>
\n \u6bcf\u4e2a\u94fe\u63a5\u90fd\u4f7f\u7528\u5176\u5148\u524d\u7684\u94fe\u63a5\u4e0e\u5176\u5148\u524d\u7684\u94fe\u63a5\u94fe\u63a5\u3002<\/span>
\n \u6700\u540e\u4e00\u4e2a\u94fe\u63a5\u5e26\u6709\u4e00\u4e2a\u7a7a\u94fe\u63a5\uff0c\u4ee5\u6807\u8bb0\u5217\u8868\u7684\u7ed3\u5c3e\u3002<\/span> <\/p>\n

\u521b\u5efa\u53cc\u5411\u94fe\u63a5\u5217\u8868<\/h2>\n
\n \u6211\u4eec\u4f7f\u7528Node\u7c7b\u521b\u5efa\u4e00\u4e2a\u53cc\u5411\u94fe\u63a5\u5217\u8868\u3002\u73b0\u5728\uff0c\u6211\u4eec\u4f7f\u7528\u4e0e\u201c\u5355\u94fe\u63a5\u5217\u8868\u201d\u4e2d\u76f8\u540c\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u9664\u4e86\u8282\u70b9\u4e2d\u5b58\u5728\u7684\u6570\u636e\u4e4b\u5916\uff0c\u8fd8\u5c06\u4f7f\u7528\u5934\u6307\u9488\u548c\u4e0b\u4e00\u4e2a\u6307\u9488\u8fdb\u884c\u6b63\u786e\u5206\u914d\uff0c\u4ee5\u5728\u6bcf\u4e2a\u8282\u70b9\u4e2d\u521b\u5efa\u4e24\u4e2a\u94fe\u63a5\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
class <\/span>Node:
    def <\/span>__init__(self, data):
      self.data = data
      self.next = None
      self.prev = None
class <\/span>doubly_linked_list:
    def <\/span>__init__(self):
      self.head = None
# Adding data elements
<\/span>     def <\/span>push(self, NewVal):
      NewNode = Node(NewVal)
      NewNode.next = self.head
       if <\/span>self.head is <\/span> not <\/span>None:
         self.head.prev = NewNode
      self.head = NewNode
# print <\/span>the Doubly Linked list
<\/span>     def <\/span>listprint(self, node):
       while <\/span>(node is <\/span> not <\/span>None):
         print(node.data),
         last = node
         node = node.next
dllist = doubly_linked_list()
dllist. push<\/span>(12)
dllist. push<\/span>(8)
dllist. push<\/span>(62)
dllist. listprint<\/span>(dllist.head)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 62 8 12
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u63d2\u5165\u53cc\u94fe\u8868<\/h2>\n
\n \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u770b\u5230\u5982\u4f55\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5e8f\u5c06\u8282\u70b9\u63d2\u5165\u5230\u201c\u53cc\u5411\u94fe\u63a5\u5217\u8868\u201d\u4e2d\u3002\u8be5\u7a0b\u5e8f\u4f7f\u7528\u4e00\u79cd\u540d\u4e3ainsert\u7684\u65b9\u6cd5\uff0c\u5c06\u65b0\u8282\u70b9\u63d2\u5165\u5230\u53cc\u5411\u94fe\u8868\u7684\u5f00\u5934\u7684\u7b2c\u4e09\u4e2a\u4f4d\u7f6e\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
# Create the Node class
<\/span> class <\/span>Node:
    def <\/span>__init__(self, data):
      self.data = data
      self.next = None
      self.prev = None
# Create the doubly linked list
<\/span> class <\/span>doubly_linked_list:
    def <\/span>__init__(self):
      self.head = None
# Define the push method to add elements
<\/span>     def <\/span>push(self, NewVal):
      NewNode = Node(NewVal)
      NewNode.next = self.head
       if <\/span>self.head is <\/span> not <\/span>None:
         self.head.prev = NewNode
      self.head = NewNode
# Define the insert method to insert the element
<\/span>     def <\/span>insert(self, prev_node, NewVal):
       if <\/span>prev_node is <\/span>None:
         return
      NewNode = Node(NewVal)
      NewNode.next = prev_node.next
      prev_node.next = NewNode
      NewNode.prev = prev_node
       if <\/span>NewNode.next is <\/span> not <\/span>None:
         NewNode.next.prev = NewNode
# Define the method to print <\/span>the linked list
<\/span>     def <\/span>listprint(self, node):
       while <\/span>(node is <\/span> not <\/span>None):
         print(node.data),
         last = node
         node = node.next
dllist = doubly_linked_list()
dllist. push<\/span>(12)
dllist. push<\/span>(8)
dllist. push<\/span>(62)
dllist. insert<\/span>(dllist.head.next, 13)
dllist. listprint<\/span>(dllist.head)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 62 8 13 12
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8ffd\u52a0\u5230\u53cc\u94fe\u8868<\/h2>\n
\n \u8ffd\u52a0\u5230\u53cc\u5411\u94fe\u8868\u5c06\u5728\u6700\u540e\u6dfb\u52a0\u5143\u7d20\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
\"prettyprint <\/span>notranslate\"<\/span>> \n
\n# Create the node class
<\/span> \nclass <\/span>Node: \n
    \ndef <\/span>__init__(self, data): \n
      self.data = data \n
      self.next = None \n
      self.prev = None \n
\n# Create the doubly linked list class
<\/span> \nclass <\/span>doubly_linked_list: \n
    \ndef <\/span>__init__(self): \n
      self.head = None \n
\n# Define the push method to add elements at the begining
<\/span>     \ndef <\/span>push(self, NewVal): \n
      NewNode = Node(NewVal) \n
      NewNode.next = self.head \n
       \nif <\/span>self.head \nis <\/span> \nnot <\/span>None: \n
         self.head.prev = NewNode \n
      self.head = NewNode \n
\n# Define the append method to add elements at the end
<\/span>     \ndef <\/span>append(self, NewVal): \n
      NewNode = Node(NewVal) \n
      NewNode.next = None \n
       \nif <\/span>self.head \nis <\/span>None: \n
         NewNode.prev = None \n
         self.head = NewNode \n
         return \n
      last = self.head \n
       \nwhile <\/span>(last.next \nis <\/span> \nnot <\/span>None): \n
         last = last.next \n
      last.next = NewNode \n
      NewNode.prev = last \n
      return \n
\n# Define the method to print
<\/span>     \ndef <\/span>listprint(self, node): \n
       \nwhile <\/span>(node \nis <\/span> \nnot <\/span>None): \n
         print(node.data), \n
         last = node \n
         node = node.next \n
dllist = doubly_linked_list() \n
dllist. \npush<\/span>(12) \n
dllist. \nappend<\/span>(9) \n
dllist. \npush<\/span>(8) \n
dllist. \npush<\/span>(62) \n
dllist. \nappend<\/span>(45) \n
dllist. \nlistprint<\/span>(dllist.head) \n
<\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 62 8 12 9 45
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8bf7\u6ce8\u610f\u9644\u52a0\u64cd\u4f5c\u7684\u5143\u7d209\u548c45\u7684\u4f4d\u7f6e\u3002\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u9ad8\u7ea7\u94fe\u63a5\u5217\u8868zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1065","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1065"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1065"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1065\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}