Python语言例题集(014)

#!/usr/bin/python3

#建立双向链表。
	
class Node():
    def __init__(self,data=None):
        self.data=data
        self.next=None
        self.previous=None
        
class DoubleLinkedList():
    def __init__(self):
        self.head=None
        self.tail=None
        
    def addDoubleList(self,newNode):
        if isinstance(newNode,Node):
            if self.head==None:
                self.head=newNode
                newNode.previous=None
                newNode.next=None
                self.tail=newNode
            else:
                self.tail.next=newNode
                newNode.previous=self.tail
                self.tail=newNode
            return
        
    def printListFromHead(self):
        ptr=self.head 
        while ptr:
            print(ptr.data)
            ptr=ptr.next
            
    def printListFromTail(self):
        ptr=self.tail
        while ptr:
            print(ptr.data)
            ptr=ptr.previous
            
doubleLink=DoubleLinkedList()
n1=Node(5)
n2=Node(15)
n3=Node(25)
for n in [n1,n2,n3]:
    doubleLink.addDoubleList(n)
    print("从头部打印双向链表")
    doubleLink.printListFromHead()
print("从尾部打印双向链表")
doubleLink.printListFromTail()      
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值