[LinkedList] Einfügen von Daten versagt zur Laufzeit



  • Hi,
    ich bin's nochmal ;). Wenn die Fragerei nervt tut es mir Leid, aber ich habe
    echt keine Ahnung wo hier der Fehler liegen könnte!
    Ich habe die einfach verkette Liste nun soweit fertig, dass zumindest der
    Compiler Ruhe gibt und Funktionen wie "getLength()", "initCursor()" etc (halt
    der Kleinkram) schon laufen. Nur schaffe ich es jetzt nicht dieses zum
    laufen zu bringen:
    Sobald ich versuche ein Objekt vom Typ Kontakt einzufügen schmiert die Anwendung
    mit einer Fehlermeldung ("Read konnte nicht im Speicherbereich xy ausgeführt
    werden") ab.

    Ich hänge hier mal den Sourcecode und die Test-Routine an, sollte das zu lang
    werden möge einer der Mods den Source kicken und ich schaue mal, dass ich den
    dann (ähnlich gut formatiert) auf irgendeiner Homepage publik mache.

    Hier nun der Quelltext:

    1. Der Knoten meiner Liste

    Node::Node()
    {
    }
    
    Node::~Node()
    {
    }
    
    Node::Node(Kontakt k)
    {
        content = k;
    }
    
    Node::Node(Kontakt k, Node & n)
    {
        content = k;
        nextNode = &n;
    }
    
    Node & Node::operator= (const Node & rhs)
    {
        content = rhs.content;
        nextNode = rhs.nextNode;
        return *this;
    }
    
    bool Node::operator == (const Node & rhs)
    {
        if ((content == rhs.content) && (nextNode == rhs.nextNode))
            return true;
        return false;
    }
    

    2. Die Liste selbst:

    LinkedList::LinkedList()
    {
        beginning = NULL;
        cursor = NULL;
    }
    
    LinkedList::~LinkedList()
    {
    }
    
    bool LinkedList::isEmpty()
    {
        return beginning == NULL;
    }
    
    void LinkedList::initCursor()
    {
        cursor = beginning;
    }
    
    int LinkedList::getLength()
    {
        initCursor();
        int l = 0;
        while (cursor != NULL)
        {
            l++;
            cursor = cursor->nextNode;
        }
        return l;
    }
    
    bool LinkedList::isValidPosition(int position)
    {
        return (position >= 1) && (position <= getLength());
    }
    
    void LinkedList::setCursor(int position)
    {
        if (isValidPosition(position))
        {
            initCursor();
            for (int i = 0; i < position; i++)
                cursor = cursor->nextNode;
        }
    }
    
    void LinkedList::insertAfter(Kontakt k, int position)
    {
        setCursor(position);
        if (cursor != NULL)
        {
            Node n(k, *cursor->nextNode);
            cursor->nextNode = &n;
        }
    }
    
    void LinkedList::insertBefore(Kontakt k, int position)
    {
        if (position > 1)
            insertAfter(k, position-1);
        else
        {
            if (isEmpty())
            {
                Node n(k);
                beginning = &n;
            }
            else
            {
                Node n(k, *beginning);
                beginning = &n;
            }
        }
    }
    
    Kontakt LinkedList::getContent(int position)
    {
        setCursor(position);
        if (cursor == NULL)
        {
            Kontakt k;
            return k;
        }
        return cursor->content;
    }
    
    void LinkedList::remove(int position)
    {
        if (isValidPosition(position))
        {
            if (position == 1)
                beginning = beginning->nextNode;
            else
            {
                setCursor(position-1);
                cursor->nextNode = cursor->nextNode->nextNode;
            }
        }
    }
    
    int LinkedList::find(Kontakt k)
    {
        int p = 0;
        int l = 0;
        initCursor();
        while (cursor != NULL)
        {
            l++;
            if (cursor->content == k)
            {
                p = l;
                break;
            }
            cursor = cursor->nextNode;
        }
        return p;
    }
    
    void LinkedList::add(Kontakt k)
    {
        if (isEmpty())
            insertBefore(k, 1);
        else
            insertAfter(k, getLength());
    }
    

    Und 3. die Test-Routine (soweit bisher fertig):

    void CMyAdressesDlg::OnTestbefehleListeMain() 
    {
        LinkedList l;
        Kontakt k;
        k.setzeKontakt("a", "b", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
        l.add(k);
        CString s;
        int i = l.getLength();
        s.Format("%d", i);
        i = MessageBox(s, "xx");
        Kontakt a;
        // a = l.getContent(l.getLength());
    }
    

    Es wäre echt nett wenn der ein oder andere Kenner sich dieses mal ansehen würde
    und mir eventuell sogar helfen könnte.

    Danke im Voraus,
    Khadgar



  • 2 Dinge, die mir auffallen:

    1. Im Konstruktor von Node solltest du immer nextNode initialisieren, das fehlt in einigen Versionen

    2. in insertAfter bzw. insertBefore:
    Node n(k);
    beginning = &n;
    Die Variable n wird nach Ende der Funktion zerstört, beginning zeigt dann bestenfalls auf Speichermüll!
    Du musst Node dynamisch anlegen:
    beginning = new Node(k);
    Du musst also generell mit Zeigern arbeiten, Referenzen sind hier wenig geeignet.



  • Hi tag,
    Danke, dass du dich des Problems angenommen hast!
    Die Liste läuft dank dir endlich (fast) so wie es sein sollte und ich denke
    den Rest kann ich nun auch hinbiegen 🙂

    Danke,
    Khadgar


Anmelden zum Antworten