鱼C论坛

 找回密码
 立即注册
查看: 1115|回复: 1

10鱼币:数据结构与算法——单链表数据添加问题

[复制链接]
发表于 2019-7-13 23:25:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 dj_wings 于 2019-7-13 23:27 编辑

此代码是写节点和单链表,及实现其功能 但是不知道为何我的和教程的代码相同(图片为教程的代码,我看了好几遍都没发现区别),结果却出现了bu'to问题,
请大神们帮我看看

class Node(object):
    def __init__(self,item):
        self.elem=item
        self.next=None
class SingleLinkList(object):
    def __init__(self,node=None):
        # 如果在没有传任何东西 应该是一个空链表 如果直接传数据为第一个节点
        self.__head=node
        
    def is_empty(self):
       return self.__head==None
    def length(self):
           # cur游标 count移动步数
        cur=self.__head
        count=0
        while cur!=None:
            count +=1
            cur=cur.next
        return count
    def travel(self):
        cur=self.__head
        # while cur!=None:
        while cur!=None:
            print(cur.elem,end=" ")
            cur=cur.next
    def add(self, item):
        pass
    def append(self,item):
        node=Node(item)
        if self.is_empty()==True:      
            self.__head=node
        else:
            cur=self.__head
            while cur.next!=None:
                 cur=cur.next
            cur.next=node
        # return   
    def insert(self,pos,item):
        
        pass
    def remove(self,item):
        pass
    def search(self,item):
       pass
if __name__=="__main__":
    l1=SingleLinkList()
    print(l1.travel())   #空表头 为空
    print(l1.is_empty())  #true
    print(l1.length())  #0
    l1.append(10)      #单链表 添加 10
    print(l1.travel())   #10
    print(l1.length())  #1
    print(l1.is_empty())  #false

#为理想输出  现实际输出为:
None
True
0
10 None    (??此处为何会出现None呢 我找不出原因 麻烦伙伴们能指正一下)
1
False


360截图20190713231311458.jpg 360截图20190713231500298.jpg 360截图20190713231216051.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-7-14 15:59:24 | 显示全部楼层
用pycharm调试出原因了 print(l1.travel())    这句话出问题   l1.travel() 是方法,方法中有打印输出,而外再加一个print(方法)就会打印none
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-16 21:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表