数据结构:链表(二)

单链表的操作

  • is_empty() 链表是否为空
  • length() 链表长度
  • travel() 遍历整个链表
  • class SingleLinkList(object):
        #单向链表
        def __init__(self):
            self._head=None
        def is_empty(self):
            return self._head==None  #当头节点为None,说明链表就是空的
    
        def length(self):
            cur=self._head  #头节点需要一个存储,通过节点中的链接指向写一个节点
            count=0           
            while cur !=None: #循环记录所经历的节点数
                count=count+1
                cur=cur.next     #每次循环时,要指向下一个节点
        def travel(self):   
            cur=self._head    
            while cur !=None:
                print(cur.item)    #输出节点中的数据
                cur=cur.next
    

      

posted @ 2020-05-12 20:10  Roronoa-Zoro  阅读(127)  评论(0编辑  收藏  举报