怎么使用python实现从尾到头打印单链表功能

  介绍

这篇文章将为大家详细讲解有关怎么使用python实现从尾到头打印单链表功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

python实现从尾到头打印单链表功能,具体如下:

#,编码为utf - 8   class  SingleNode:   ,   def 才能__init__(自我,,项):   ,,,self.item =条目   ,,,self.next =,没有   ,   class  SingleLinkedList:   “才能”;“   is_empty才能(),链表是否为空   print_end_to_head才能(),从尾到头打印单链表   append()项才能,链表尾部添加元素   “才能”;“   ,   def 才能__init__(自我):   ,,,self._head =,没有   ,   def 才能is_empty(自我):   ,,,return  self._head  is 没有   ,   def 才能添加(自我,,项):   ,,,if  self.is_empty ():   ,,,,,self._head =条目   ,,,:   ,,,,,cur =self._head   ,,,,,while  cur.next:   ,,,,,,,cur =cur.next   ,,,,,cur.next =条目   ,   def 才能print_end_to_head(自我):   ,,,,,,从尾到头打印单链表“““   ,,,if  self.is_empty ():   ,,,,,印刷(没有)   ,,,,,回来   ,,,tmp =, []   ,,,cur =self._head   ,,,while 坏蛋:   ,,,,,tmp.insert (0,, cur)   ,,,,,cur =cur.next   ,,,for 小姐:拷贝tmp:   ,,,,,印刷(i.item)   ,   if  __name__ ==, & # 39; __main__ # 39;:   时间=sl 才能;SingleLinkedList ()   sl.append才能(SingleNode (1))   sl.append才能(SingleNode (2))   sl.append才能(SingleNode (3))   sl.append才能(SingleNode (4))   sl.print_end_to_head才能()

运行结果:

4
3
2
1

PS:对象obj的打印,可使用如下语句实现:

print  & # 39; \ n # 39; . join ((& # 39; % s: % & # 39;, %, item  for  item 拷贝obj.__dict__.items ()))

关于“怎么使用python实现从尾到头打印单链表功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看的到。

怎么使用python实现从尾到头打印单链表功能