You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现LinkedList遭遇ModuleNotFoundError问题求助

问题描述

按照教程尝试在Python中实现LinkedList,但持续遇到ModuleNotFoundError,不清楚问题所在,因为完全遵循了教程步骤。预期程序能正常运行,输出遍历结果及删除指定元素后的遍历结果。

LinkedList代码
from LinkedListDS.Node import Node;

class LinkedList(object):

    def __init__(self):
        self.head = None;
        self.counter = 0;
    #O(N)
    def traverseList(self):
        actualNode = self.head;

        while actualNode is not None:
            print("%d " % actualNode.data);
            actualNode = actualNode.nextNode;
    #O(1)
    def insertStart(self, data):

        self.counter += 1;

        newNode = Node(data);

        if not self.head:
            self.head = newNode;
        else:
            newNode.NextNode = self.head;
            self.head = newNode;
    #O(1) instead of O(N)
    def size(self):
        return self.counter;

    #O(N) !!!
    def insertEnd(self, data):

        if self.head is None:
            self.insertStart(data);
            return;
        self.counter +=1;

        newNode = Node(data);
        actualNode = self.head;

        while actualNode.nextNode is not None:
            actualNode.nextNode = newNode;
        actualNode.nextNode = newNode;

    def remove(self,data):
        self.counter -=1;

        if self.head:
            if data == self.head.data:
                self.head = self.head.nextNode;
            else:
                self.head.remove(data, self.head);
Node代码
class Node(object):

    def __init__(self, data):
        self.data = data;
        self.nextNode = Node;

    def remove(self, data, previousNode):
        if self.data == data:
            previousNode.nextNode = self.nextNode;
            del self.data;
            del self.nextNode;
        else:
            if self.nextNode is not None:
                self.nextNode.remove(data, self);
测试代码
from LinkedListDS.LinkedList import LinkedList;

linkedList = LinkedList();

linkedList.insertEnd(12);
linkedList.insertEnd(122);
linkedList.insertEnd(3);
linkedList.insertEnd(31);

linkedList.traverseList();

linkedList.remove(12);
 
linkedList.traverseList();
错误信息
PS C:\Users\...\Documents\Python tut\python> & C:/Users/.../AppData/Local/Programs/Python/Python310/python.exe "c:/Users/.../Documents/Python tut/python/LinkedListDS/LinkedList.py"
Traceback (most recent call last):
  File "c:\Users\gbles\Documents\Python tut\python\LinkedListDS\LinkedList.py", line 1, in <module>
    from LinkedListDS.Node import Node;
ModuleNotFoundError: No module named 'LinkedListDS'
解决方法

1. 修正模块结构与导入路径

  • 先确保你的目录结构为:
    python/
      LinkedListDS/
        __init__.py
        LinkedList.py
        Node.py
    
    必须添加空的__init__.py文件,Python才会把LinkedListDS识别为可导入的模块包。
  • 修改LinkedList.py中的导入语句,因为两个文件在同一目录,直接相对导入即可:
    from Node import Node
    

2. 调整运行方式

  • 不要直接在LinkedListDS目录下运行LinkedList.py,回到父目录python/执行测试代码:
    cd C:\Users\...\Documents\Python tut\python
    python 你的测试文件名.py
    

3. 修复代码中的其他逻辑错误

  • Node类初始化错误:self.nextNode = Node应改为self.nextNode = None,避免将类本身赋值给节点指针。
  • LinkedList的insertEnd方法逻辑错误:循环内应该移动节点指针而非直接修改nextNode,正确代码为:
    while actualNode.nextNode is not None:
        actualNode = actualNode.nextNode
    actualNode.nextNode = newNode
    
  • LinkedList的remove方法错误:不要提前减计数器,应在确认元素存在后再修改;且调用head.remove时,传递的前置节点应为链表实例而非头节点本身:
    def remove(self, data):
        if self.head:
            if data == self.head.data:
                self.head = self.head.nextNode
                self.counter -=1
            else:
                self.head.remove(data, self)
    

内容的提问来源于stack exchange,提问作者ugonna chukwuemeka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 14:55:21