python培训之创建链表的两种形式

发布时间:2022-12-21 09:36:40 人气:112 作者:多测师

  python创建链表的两种形式

  说明

  1、头插法将结点插入头结点后面,新加入的结点next指向原来head指向的结点。

  head改为新的结点。

  2、尾插法将结点插入尾点前,新节点的next指向tail,tail更新为新节点。

  实例

  class Node:

  def __init__(self,item):

  self.item = item

  self.next = None

  class HandleNode:

  def create_linklist_head(self,li):

  head = Node(li[0])

python培训之创建链表的两种形式

  for element in li[1:]:

  node = Node(element)

  node.next = head

  head = node

  return head

  def create_linklist_tail(self,li):

  head = Node(li[0])

  tail = head

  for element in li[1:]:

  node = Node(element)

  tail.next = node

  tail = node

  return head

  def print_linklist(self,head):

  while head:

  print(head.item,end=',')

  head=head.next

  以上就是python创建链表的两种形式,希望对大家有所帮助。更多Python学习指路:请关注多测师。https://www.e70w.com/xwzx/




返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线