闵溪端 2022-11-26 16:07 采纳率: 91.7%
浏览 4
已结题

python中的无序数据链问题

问题遇到的现象和发生背景 在移除数据链中没有的元素时,运行结果没有输出“未找到该元素

class Unorderedlist:
    def __init__(self):
        self.head = None

    def isEmpty(self):
        return self.head == None

    def addHead(self, data):  #头插法
        temp = Node(data)
        temp.setNext(self.head)
        self.head = temp


    def addTail(self, data):    #尾插法
        temp = Node(data)
        current = self.head
        while current.getNext() != None:
            current = current.getNext()
        current.setNext(temp)


    def printList(self):
        current = self.head
        while current != None:
            print(current.data, end="")
            current = current.getNext()


    def length(self):
        index = 0
        current = self.head
        while current:
            index += 1
            current = current.getNext()
        return index


    def search(self, data):
        current = self.head
        found = False
        while current and not found:
            if current.getData() == data:
                found = True
            current = current.getNext()
        return found

    def remove(self, item):
        current = self.head
        previous = None
        found = False

        while not found and current:
            if current.getData() == item:
                found = True
            else:
                previous = current
                current = current.getNext()
        if found == False:
            return "未找到该元素"
        else:
            if previous == None:
                self.head = current.getNext()

            else:
                previous.setNext(current.getNext())


mylist = Unorderedlist()
mylist.addHead(31)
mylist.addHead(77)
mylist.addHead(17)
mylist.addHead(93)
mylist.addHead(26)
mylist.addHead(54)
mylist.addTail(14)

mylist.printList()
print(mylist.search(54))
mylist.remove(11)
mylist.printList()

我的解答思路和尝试过的方法 缩进也缩进过了,没啥用
我想要达到的结果 当输入链表中没有的数据时,如“1”,可以输出“未找到该元素”
  • 写回答

2条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-11-26 17:03
    关注

    你是return "未找到该元素",又不是print("未找到该元素"),当然没有打印出来了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月4日
  • 已采纳回答 11月26日
  • 创建了问题 11月26日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。