鱼C论坛

 找回密码
 立即注册
查看: 1853|回复: 5

[已解决]代码出错帮忙看下

[复制链接]
发表于 2020-7-13 10:58:07 | 显示全部楼层 |阅读模式

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

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

x
  1. #先定义一个节点的类
  2. class Node():
  3.     #初始化
  4.     def __init__(self,elem):
  5.         self.elem=elem
  6.         self.next=[]#节点对象一开始的数据区是有的但是next不知道该指向谁先设置为空
  7. node=Node(100)

  8. #定义单链表的类
  9. class Singlelinklist():
  10.     def __init__(self,node=None):#链表当中必须得存在一个属性指向第一个节点将链表与节点关联起来,属于类属性,因为在使用链表的时候不是拿类来操作的
  11.         self.__head=node#自己内部的函数去使用,对外不暴露,所以将其私有化
  12.     def is_empty(self):
  13.        return self.__head==None
  14.     def length(self):
  15.       #cur游标用来移动遍历节点;将cur置于头一个头结点
  16.         cur=self.__head=None
  17.       #count用来记录数据
  18.         count=0
  19.         while cur!=None:
  20.             count+=1
  21.             cur=cur.next
  22.         return count
  23.     def travel(self):
  24.         """遍历整个列表"""
  25.         cur=self.__head
  26.         while cur!=None:
  27.             print(cur.elem)
  28.             cur=cur.next
  29.     def add(self,item):
  30.         """链表头部添加元素"""
  31.         pass
  32.     def append(self,item):
  33.         """链表尾部添加元素"""
  34.         node=Node(item)
  35.         if self.is_empty():
  36.             self.__head=node
  37.         else:
  38.             cur=self.__head
  39.             while cur.next!=None:
  40.                 cur=cur.next
  41.             cur.next=node
  42.     def insert(self,pos,item):
  43.         """在指定的位置添加元素"""
  44.         pass
  45.     def remove(self,item):
  46.         """删除节点"""
  47.         pass
  48.     def search(self,item):
  49.         """查找节点是否存在"""
  50.         pass
  51. if __name__=='__main__':
  52.     li=Singlelinklist()
  53.     print(li.is_empty())
  54.     print(li.length())
  55.     li.append(1)
  56.     print(li.is_empty())
  57.     print(li.length())
  58.     li.append(2)
  59.     li.append(3)
  60.     li.append(4)
  61.     li.append(5)
  62.     li.append(6)
  63.     li.travel()

复制代码


这个代码我要实现以上的功能但是出了点问题不知道错在哪里,麻烦帮忙指出来并修改一下
最佳答案
2020-7-13 11:09:26
错误有两处:
第6行,节点初始化的时候,self.next应该为None,因为你后面在俩女表中插入介电的时候都是这么用的。
第17行,cur=self.__head=None   后面的这个=None是个什么鬼?删掉
  1. #先定义一个节点的类
  2. class Node():
  3.     #初始化
  4.     def __init__(self,elem):
  5.         self.elem=elem
  6.         self.next=None#节点对象一开始的数据区是有的但是next不知道该指向谁先设置为空
  7. node=Node(100)

  8. #定义单链表的类
  9. class Singlelinklist():
  10.     def __init__(self,node=None):#链表当中必须得存在一个属性指向第一个节点将链表与节点关联起来,属于类属性,因为在使用链表的时候不是拿类来操作的
  11.         self.__head=node#自己内部的函数去使用,对外不暴露,所以将其私有化
  12.     def is_empty(self):
  13.        return self.__head==None
  14.     def length(self):
  15.       #cur游标用来移动遍历节点;将cur置于头一个头结点
  16.         cur=self.__head
  17.       #count用来记录数据
  18.         count=0
  19.         while cur!=None:
  20.             count+=1
  21.             cur=cur.next
  22.         return count
  23.     def travel(self):
  24.         """遍历整个列表"""
  25.         cur=self.__head
  26.         while cur!=None:
  27.             print(cur.elem)
  28.             cur=cur.next
  29.     def add(self,item):
  30.         """链表头部添加元素"""
  31.         pass
  32.     def append(self,item):
  33.         """链表尾部添加元素"""
  34.         node=Node(item)
  35.         if self.is_empty():
  36.             self.__head=node
  37.         else:
  38.             cur=self.__head
  39.             while cur.next!=None:
  40.                 cur=cur.next
  41.             cur.next=node
  42.     def insert(self,pos,item):
  43.         """在指定的位置添加元素"""
  44.         pass
  45.     def remove(self,item):
  46.         """删除节点"""
  47.         pass
  48.     def search(self,item):
  49.         """查找节点是否存在"""
  50.         pass
  51. if __name__=='__main__':
  52.     li=Singlelinklist()
  53.     print(li.is_empty())
  54.     print(li.length())
  55.     li.append(1)
  56.     print(li.is_empty())
  57.     print(li.length())
  58.     li.append(2)
  59.     li.append(3)
  60.     li.append(4)
  61.     li.append(5)
  62.     li.append(6)
  63.     li.travel()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-13 11:06:05 | 显示全部楼层
  1.         while cur!=None:
  2.             count+=1
  3.             cur=cur.next
复制代码


这是要干嘛...cur的类型是列表呀!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-13 11:09:26 | 显示全部楼层    本楼为最佳答案   
错误有两处:
第6行,节点初始化的时候,self.next应该为None,因为你后面在俩女表中插入介电的时候都是这么用的。
第17行,cur=self.__head=None   后面的这个=None是个什么鬼?删掉
  1. #先定义一个节点的类
  2. class Node():
  3.     #初始化
  4.     def __init__(self,elem):
  5.         self.elem=elem
  6.         self.next=None#节点对象一开始的数据区是有的但是next不知道该指向谁先设置为空
  7. node=Node(100)

  8. #定义单链表的类
  9. class Singlelinklist():
  10.     def __init__(self,node=None):#链表当中必须得存在一个属性指向第一个节点将链表与节点关联起来,属于类属性,因为在使用链表的时候不是拿类来操作的
  11.         self.__head=node#自己内部的函数去使用,对外不暴露,所以将其私有化
  12.     def is_empty(self):
  13.        return self.__head==None
  14.     def length(self):
  15.       #cur游标用来移动遍历节点;将cur置于头一个头结点
  16.         cur=self.__head
  17.       #count用来记录数据
  18.         count=0
  19.         while cur!=None:
  20.             count+=1
  21.             cur=cur.next
  22.         return count
  23.     def travel(self):
  24.         """遍历整个列表"""
  25.         cur=self.__head
  26.         while cur!=None:
  27.             print(cur.elem)
  28.             cur=cur.next
  29.     def add(self,item):
  30.         """链表头部添加元素"""
  31.         pass
  32.     def append(self,item):
  33.         """链表尾部添加元素"""
  34.         node=Node(item)
  35.         if self.is_empty():
  36.             self.__head=node
  37.         else:
  38.             cur=self.__head
  39.             while cur.next!=None:
  40.                 cur=cur.next
  41.             cur.next=node
  42.     def insert(self,pos,item):
  43.         """在指定的位置添加元素"""
  44.         pass
  45.     def remove(self,item):
  46.         """删除节点"""
  47.         pass
  48.     def search(self,item):
  49.         """查找节点是否存在"""
  50.         pass
  51. if __name__=='__main__':
  52.     li=Singlelinklist()
  53.     print(li.is_empty())
  54.     print(li.length())
  55.     li.append(1)
  56.     print(li.is_empty())
  57.     print(li.length())
  58.     li.append(2)
  59.     li.append(3)
  60.     li.append(4)
  61.     li.append(5)
  62.     li.append(6)
  63.     li.travel()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-13 11:11:42 | 显示全部楼层
本帖最后由 Stubborn 于 2020-7-13 11:16 编辑

问题比较多 ,参考下这个吧,比较小,找下自己的问题,还有疑问,再回复

  1. class Node:
  2.     def __init__(self, value):
  3.         self.val = value
  4.         self.next = None


  5. class SingleStrand:

  6.     def __init__(self):
  7.         self._head = None
  8.         self._length = 0

  9.     @property
  10.     def is_empty(self):
  11.         return bool(self._head)

  12.     @property
  13.     def length(self):
  14.         return self._length

  15.     def __len__(self):
  16.         return self._length

  17.     def __contains__(self, val):
  18.         cur = self._head
  19.         while cur is not None:
  20.             if cur.val == val:
  21.                 return True
  22.             cur = cur.next
  23.         return False

  24.     def insert(self, val, idx):
  25.         """指定位置插入"""
  26.         if idx == 0:
  27.             self.add(val)
  28.         elif idx >= self.length:
  29.             self.append(val)
  30.         else:
  31.             cur = self._head
  32.             n = 1
  33.             while cur.next:
  34.                 if n == idx: break
  35.                 cur = cur.next
  36.                 n += 1
  37.             node = Node(val)
  38.             node.next = cur.next
  39.             cur.next = node
  40.             self._length += 1

  41.     def add(self, val):
  42.         """头部添加"""
  43.         node = Node(val)
  44.         node.next = self._head
  45.         self._head = node

  46.         self._length += 1

  47.     def append(self, val):
  48.         """尾部添加"""
  49.         cur = self._head
  50.         if cur is None:
  51.             self.add(val)
  52.         else:
  53.             node = Node(val)
  54.             while cur.next:
  55.                 cur = cur.next
  56.             cur.next = node

  57.             self._length += 1

  58.     def travel(self, cur=None):
  59.         """单向链表的遍历
  60.             递归演示
  61.         """
  62.         cur = self._head if cur is None else cur
  63.         if cur.next is not None:
  64.             return f"-->\t{cur.val}\t" + self.travel(cur.next)
  65.         else:
  66.             return f"-->{cur.val} --> <<travel over>>"

  67.     def travel_(self):
  68.         """单向链表的遍历
  69.             非递归演示
  70.         """
  71.         cur = self._head
  72.         string = f"<<travel start>>\t{cur.val}\t"
  73.         while cur.next:
  74.             cur = cur.next
  75.             string += f"-->\t{cur.val}\t"
  76.         return string + "<<travel over>>"

  77.     def delete(self, val):
  78.         if not self.is_empty:
  79.             raise IndexError("Strand is empty")
  80.         cur = self._head
  81.         if cur.val == val:
  82.             self._head = cur.next
  83.         while cur.next:
  84.             pre = cur
  85.             cur = cur.next
  86.             if cur.val == val:
  87.                 pre.next = cur.next


  88. if __name__ == '__main__':
  89.     t = SingleStrand()
  90.     t.add(1)
  91.     t.add(2)
  92.     t.append(3)
  93.     print(t.travel_())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-13 11:11:58 | 显示全部楼层
1、不要用cur == None,要用cur is None,同理,cur != None应改为 cur is not None
2、node中的next应初始化为None:
  1. class Node:
  2.     # 初始化
  3.     def __init__(self, elem):
  4.         self.elem = elem
  5.         self.next = None
复制代码

3、在length()方法中
def length(self):
      #cur游标用来移动遍历节点;将cur置于头一个头结点
        cur=self.__head=None
      #count用来记录数据
        count=0
        while cur!=None:
            count+=1
            cur=cur.next
        return count
cur 都初始化为None了,后面就没有意义了
改成这样:
cur=self.__head
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-13 11:14:28 | 显示全部楼层
  1. self.next=None
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-23 11:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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